使用apacha的basicdatasource连接池,写了一个spring小程序,测试获得mySQL数据库连接,set注入数据库连接的基本参数后,提示错误
Exception in thread "main" java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2023)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1897)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at com.alex.util.MySQLDataSource.getConnection(MySQLDataSource.java:43)
at com.alex.util.test.TestCase.main(TestCase.java:16)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2007)
... 4 more
后多次修改调试,发现问题出在
public MySQLDataSource() {
bds = new BasicDataSource();
bds.setDriverClassName(driver);
bds.setUrl(url);
bds.setUsername(username);
bds.setPassword(password);
}
修改成
public MySQLDataSource() {
bds = new BasicDataSource();
}
@Override
public Connection getConnection() throws SQLException{
bds.setDriverClassName(driver);
bds.setUrl(url);
bds.setUsername(username);
bds.setPassword(password);
return bds.getConnection();
}
提示可以获得链接,请问这是为什么啊,是注入顺序问题吗,我百度过之后,还是无解,本人小白,求指教。附所有源代码。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
">
<bean id="mydatasource" class="com.alex.util.MySQLDataSource">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/jsd1407?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
interface:
package com.alex.util;
import java.sql.Connection;
import java.sql.SQLException;
public interface DataSource {
public abstract Connection getConnection() throws SQLException;
public abstract void closeConnection() throws SQLException;
}
MySQLDataSource:
package com.alex.util;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
public class MySQLDataSource implements DataSource {
private String driver;
private String url;
private String username;
private String password;
private BasicDataSource bds;
public void setDriver(String driver) {
this.driver = driver;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public MySQLDataSource() {
bds = new BasicDataSource();
}
@Override
public Connection getConnection() throws SQLException{
bds.setDriverClassName(driver);
bds.setUrl(url);
bds.setUsername(username);
bds.setPassword(password);
return bds.getConnection();
}
@Override
public void closeConnection() throws SQLException{
bds.close();
}
}