eclipse建的web工程连接数据库mysql的问题
我在网上查了许多资料,连接方式大同小异,我按照其中一个方法测试连接,老失败,求大神解决一下。eclipse。java文件如下packagesql;importjava.s...
我在网上查了许多资料,连接方式大同小异,我按照其中一个方法测试连接,老失败,求大神解决一下。
eclipse。java文件如下
package sql;
import java.sql.*;
public class Mysql {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println("加载驱动程序异常。");
}
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root"," 123456");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from admin");
while (rs.next()) {
System.out.println(rs.getString("name"));}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println("连接数据库异常。");
}
}
}
数据库如图
错误代码如下
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
......
at java.sql.DriverManager.getConnection(Unknown Source)
at sql.Mysql.main(Mysql.java:18)
连接数据库异常。
解决了
把
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root"," 123456");
改成
Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","123456");就不报错了。 展开
eclipse。java文件如下
package sql;
import java.sql.*;
public class Mysql {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println("加载驱动程序异常。");
}
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root"," 123456");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from admin");
while (rs.next()) {
System.out.println(rs.getString("name"));}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println("连接数据库异常。");
}
}
}
数据库如图
错误代码如下
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
......
at java.sql.DriverManager.getConnection(Unknown Source)
at sql.Mysql.main(Mysql.java:18)
连接数据库异常。
解决了
把
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root"," 123456");
改成
Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","123456");就不报错了。 展开
推荐于2016-11-23
展开全部
1. 检查用户名密码(你已经确认过了)
2. 检查用户登录权限和连接配置方式(比如是否允许用户名密码,是否允许非windows账号验证)
3. 检查数据库是不是在本机(localhost)上,如果不是,将localhost改为数据库所在服务器IP
4. 用mysql管理工具或telnet localhost 3306,看能否打开登录端口
2. 检查用户登录权限和连接配置方式(比如是否允许用户名密码,是否允许非windows账号验证)
3. 检查数据库是不是在本机(localhost)上,如果不是,将localhost改为数据库所在服务器IP
4. 用mysql管理工具或telnet localhost 3306,看能否打开登录端口
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询