刚学springmvc,原理不难,但是精通确实不容易,刚学就卡在了配置文件这块。
解决千难万险后终于走到数据库连接池这块,也是各种百度各种尝试:
遇到的问题:
1.启动server时抛出的:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through method 'setStudentService' parameter 0;
最后发现是C3P0的jar包没给,下载的c3p0-0912.jar,重启服务正常。
2.接着访问controller时抛出:
Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract] with root cause。
百度说要更改c3p0版本到0.9.5.2,换c3p0-0.9.5.2.jar,一并下载了mchange-commons-java-0.2.15.jar。
3.再执行controller,抛出:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.,
将原来的jdbc.properties文件里的driver=com.mysql.jdbc.Driver修改为driver=com.mysql.cj.jdbc.Driver
4.继续执行业务,抛出:
java.sql.SQLException: Access denied for user 'Administrator'@'localhost' (using password: YES)
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
百度搜了很多都没能解决,知道时连接数据库失败了,应该是配置文件的问题,我当前的jdbc.properties的配置
是:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xx
username=root
password=12345
对比网上一些配置后修改为
datasource.driver=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/xx
datasource.username=root
datasource.password=12345
相应地修改spring的连接池参数配置,再执行竟然通了。究竟是为什么?没能明白,最后我逐个尝试,发现只要用username就出错,用user或者name都没问题,用xusername或datasource.username或jdbc.username都没问题,所以最后我改成了
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xx
user=root
password=12345
好了,
结束。