使用idea创建SSM项目时数据源配置异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用idea创建SSM项目时数据源配置异常相关的知识,希望对你有一定的参考价值。

异常:org.springframework.jdbc.CannotGetJdbcConnectionException

  jdbc.properties关于DB的配置(数据库名是liaody):


driverClassName=com.mysql.jdbc.Driver
password=root
username=root
url=jdbc:mysql://localhost:3306/liaody?characterEncoding=UTF8
 applicationContext.xml关于datasource的配置:
 <!-- 配置dataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>${driverClassName}</value>
        </property>
        <property name="username">
            <value>${username}</value>
        </property>
        <property name="password">
            <value>${password}</value>
        </property>
        <property name="url">
            <value>${url}</value>
        </property>
    </bean>
启动Tomcat,访问DB,抛出异常:
 1 严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
 2 ### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user ‘Administrator‘@‘localhost‘ (using password: YES))
 3 ### The error may exist in UserInfo-mapper.xml
 4 ### The error may involve com.springmvc.service.UserInfoService.getUser
 5 ### The error occurred while executing a query
 6 ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user ‘Administrator‘@‘localhost‘ (using password: YES))] with root cause
 7 java.sql.SQLException: Access denied for user ‘Administrator‘@‘localhost‘ (using password: YES)
 8     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
 9     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
10     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
11     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
12     at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750)
13     at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
14     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
15     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
16     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
17     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
18     at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
19     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
20     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
21     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
22     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
23     at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
24     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
25     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
26     at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
27     at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
28     at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
29     at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
30     at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
31     at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
32     at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
33     at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81)
34     at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)
35     at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)
36     at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72)
37     at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59)
38     at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
39     at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
40     at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
41     at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
42     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
43     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
44     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
45     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
46     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
47     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
48     at java.lang.reflect.Method.invoke(Method.java:498)
49     at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
50     at com.sun.proxy.$Proxy35.selectOne(Unknown Source)
51     at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)
52     at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
53     at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
54     at com.sun.proxy.$Proxy36.getUser(Unknown Source)
55     at com.springmvc.controller.LoginAndRegistController.loginExec(LoginAndRegistController.java:38)
56     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
57     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
58     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
59     at java.lang.reflect.Method.invoke(Method.java:498)
60     at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
61     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
62     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
63     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
64     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
65     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
66     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
67     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
68     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
69     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
70     at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
71     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
72     at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
73     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
74     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
75     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
76     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
77     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
78     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
79     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
80     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
81     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
82     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
83     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
84     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
85     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
86     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
87     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
88     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
89     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
90     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
91     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
92     at java.lang.Thread.run(Thread.java:745)
 

  异常的解决:jdbc.properties中关于DB的配置部分,加上jdbc.:

 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.password=root
jdbc.username=root
jdbc.url=jdbc:mysql://localhost:3306/liaody?characterEncoding=UTF8

 

 

 

        修改applicationContext.xml:

  <!-- 配置dataSource -->
    <bean    id="dataSource"     class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="url">
            <value>${jdbc.url}</value>
        </property>
    </bean>

 

  异常原因:数据源配置错误。参考Spring官方文档的数据源配置后解决。

  虽然解决了该错误,但仍然有一个疑问,异常时试图登录DB的用户名是Administrator而不是root,这一点百思不得其解。有哪位大神知道什么回事么。

   

以上是关于使用idea创建SSM项目时数据源配置异常的主要内容,如果未能解决你的问题,请参考以下文章

使用idea搭建一个简单的SSM框架:配置spring+mybatis

IntelliJ IDEA搭建简单的SSM框架入门教程---初学者使用详解

IntelliJ IDEA搭建简单的SSM框架入门教程---初学者使用详解

IDEA搭建Maven Web(SSM)项目——jar包引入jetty服务器配置和ssm配置

使用idea搭建一个简单的SSM框架:使用idea创建maven项目

idea使用maven整合ssm框架