在使用eclipse连接mysql数据库时报异常:
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 com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:233)
at cn.itcast.mybatis.jdbc.jdbcTest.main(jdbcTest.java:36)
jdk版本:1.7.0_79,mysql为5.7,使用mysql-connector-java-5.1.18.jar。
代码如下:
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;/***
*@authorlilia
**/
public classjdbcTest {//mysql数据库地址
private static final String url = "jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8";//mysql数据库用户名
private static final String username = "root";//myslq数据库密码
private static final String password = "root";public static voidmain(String[] args) {//数据库连接
Connection connection = null;//预编译的statement(使用预编译的statement可以提高数据库的性能)
PreparedStatement preparedStatement = null;//结果集对象
ResultSet resultSet = null;try{//加载数据驱动
Class.forName("com.mysql.jdbc.Driver");//通过驱动管理类获取数据库连接
connection =DriverManager.getConnection(url, username, password);//定义sql语句
String sql = "select * from user where username = ?";//获取预处理statement,并把sql放入到statement中。
preparedStatement =connection.prepareStatement(sql);//参数赋值,序号从1开始
preparedStatement.setString(1, "王五");//向数据库发出sql执行查询,并返回查询结果集
resultSet =preparedStatement.executeQuery();while(resultSet.next()) {
System.out.println(resultSet.getString("id") + ":" + resultSet.getString("username"));
}
}catch(Exception e) {
e.printStackTrace();
}finally{//释放资源
if (resultSet != null) {try{
resultSet.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(preparedStatement != null){try{
preparedStatement.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(connection != null){try{
connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
这就是微学网-程序员之家为你提供的"Eclipse使用jdbc连接MySql数据库报:java.sql.SQLException: Access denie"希望对你有所帮助.本文来自网络,转载请注明出处:http://www.weixuecn.cn/article/10318.html