java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)异常是因为访问数据库密码不正确而抛出的。
解决思路提示:
第一步:查看你的数据库配置文件是否正确,下面就是我的mysql数据库属性配置文件:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis_test username=root password=aaaaaa
主要查看你的url、username和password书写是否有误,其中是否有空格。
注意:确保你的数据库能够正常访问。
第二步:仔细检查你的mybatis-cfg.xml配置文件中数据库配置成功。如下:
<properties resource="database.properties" /> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments>
其中password、username、url和driver等一定要和数据库属性配置文件中的key保存一致。
第三步:如果两步都没有问题,则你可以进行调试。我的代码如下:
// 配置文件 String cfgName = "mybatis/config/mybatis-cfg-file-test.xml"; InputStream input = Resources.getResourceAsStream(cfgName); SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder(); // 根据配置文件获取SqlSessionFactory,因此在build这里打断点(即在这里打断点) SqlSessionFactory sqlSessionFactory = factoryBuilder.build(input); SqlSession sqlSession = sqlSessionFactory.openSession(true); Map<String, String> params = new HashMap<String, String>(); String sql = "mybatis.config.StudentForConfigMapper.selectStudentListSimple"; sqlSession.select(sql, params, new ResultHandler() { @Override public void handleResult(ResultContext context) { System.out.println( "Data: " + context.getResultObject() ); } });
调试主要的类时XMLConfigBuilder.java类:
private void parseConfiguration(XNode root) { try { //issue #117 read properties first propertiesElement(root.evalNode("properties")); typeAliasesElement(root.evalNode("typeAliases")); pluginElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); settingsElement(root.evalNode("settings")); // 主要调试这个方法,因为这个方法里面可以查看配置文件中 // 数据库信息的替换是否为你所需要的数据库信息 // read it after objectFactory and objectWrapperFactory issue #631 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); typeHandlerElement(root.evalNode("typeHandlers")); mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
这是个人总结,希望对你有帮助!!!
错误详细信息:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See https://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ### The error may exist in mybatis/config/StudentForConfigMapper.xml ### The error may involve mybatis.config.StudentForConfigMapper.selectStudentListSimple ### The error occurred while executing a query ### Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) at org.apache.ibatis.session.defaults.DefaultSqlSession.select(DefaultSqlSession.java:126) at org.apache.ibatis.session.defaults.DefaultSqlSession.select(DefaultSqlSession.java:114) at mybatis.config.ClientForConfig.invoke(ClientForConfig.java:34) at mybatis.config.ClientForConfig.main(ClientForConfig.java:22) Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910) at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:154) at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:180) at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:175) at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:79) at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:373) at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:82) at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:131) at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:58) at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:271) at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:69) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:56) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:259) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) at org.apache.ibatis.session.defaults.DefaultSqlSession.select(DefaultSqlSession.java:124) ... 3 more
点击学习 MyBatis 教程,了解更多的 MyBatis 知识!