1 . 直接看代码
自定义数据源类
package com.qf.ds;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.qf.utils.DBUtils;
/**
* 数据源的目的是: 为了得到连接。而且这些连接可以复用
*
* 数据源中要模拟数据库连接池 (LinkedList)
* @author Administrator
*/
public class MyDataSource implements DataSource{
//模拟就是一个连接池 ,默认放10个
private static LinkedList<Connection> pool =new LinkedList<Connection>();
static {
for (int i = 0; i < 10; i++) {
Connection connection = DBUtils.getConnection();
pool.add(connection);
}
}
@Override
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
if (pool!=null&&pool.size()>0) {
System.out.println("=====getConnection=====");
Connection connection = pool.removeFirst();
return connection;
}
return null;
}
/**
* 连接使用完毕直接放回到连接池
*/
public void release(Connection connection ) {
try {
System.out.println("=====release=====");
pool.addLast(connection);
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
2 . 工具类
package com.qf.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtils {
private static String driverClass = "";
private static String url = "";
private static String user = "";
private static String pwd = "";
static {
ResourceBundle resourceBundle = ResourceBundle.getBundle("dbinfo");
driverClass = resourceBundle.getString("driverClass");
url = resourceBundle.getString("url");
user = resourceBundle.getString("user");
pwd = resourceBundle.getString("password");
}
public static Connection getConnection() {
Connection connection = null;
// 1.注册驱动
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库驱动异常");
}
// 2.得到连接
try {
connection = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接异常");
}
return connection;
}
public static void closeAll(ResultSet resultSet,Statement statement, Connection connection) {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
properties配置文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/1713_transaction
user=root
password=123
3 . 测试类
package com.qf.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.qf.ds.MyDataSource;
public class Test01 {
@Test
public void test01() {
//1.从数据源的连接池中获取连接
MyDataSource ds = new MyDataSource();
Connection connection = null ;
try {
connection = ds.getConnection();
System.out.println(connection);
PreparedStatement preparedStatement = connection.prepareStatement("select * from user");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.println(resultSet.getString("name"));;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (connection!=null) {
ds.release(connection);
}
}
}
}
这个只需要添加数据库驱动jar包