package dbcpool;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 适用于多线程数据库操作
* @author Fly
*
*/
public class DBUtils {
private static String driver ;
private static String url;
private static String user;
private static String password;
private static int initSize;
private static int maxActive;
private static BasicDataSource bs;//管理数据库连接的实现类
//静态方法创建连接池
static{
bs = new BasicDataSource();
Properties pro = new Properties();
try {
InputStream in = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
pro.load(in);
driver = pro.getProperty("jdbc.driver");
url = pro.getProperty("jdbc.url");
user = pro.getProperty("jdbc.user");
password = pro.getProperty("jdbc.password");
initSize = Integer.parseInt(pro.getProperty("initSize"));
maxActive = Integer.parseInt(pro.getProperty("maxActive"));
in.close();
bs.setDriverClassName(driver);//设置管理类的相关属性
bs.setUrl(url);
bs.setUsername(user);
bs.setPassword(password);
bs.setInitialSize(initSize);
bs.setMaxActive(maxActive);
} catch (Exception e) {
}
}
public static Connection getConnection() {
try {
//bs.getConnection()从连接池中获取连接
Connection conn = bs.getConnection();
return conn;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
public static void close(Connection conn) {
try {
if(conn!=null){
//归还连接池
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package dbcpool;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Demo1 {//多线程的模拟效果
public static void main(String[] args) throws Exception {
Thread thread1 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
Connection conn = dbcpool.DBUtils.getConnection();
System.out.println("开始调用第1个线程");
Statement st = conn.createStatement();
String sql = "select 'hello' as a from dual ";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
String string = rs.getString("a");
System.out.println(string);
}
st.close();
rs.close();
conn.close();
System.out.println("第1条线程归还");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread thread2= new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
Connection conn = dbcpool.DBUtils.getConnection();
System.out.println("开始调用第2个线程");
Statement st = conn.createStatement();
String sql = "select 'hello' as a from dual ";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
String string = rs.getString("a");
System.out.println(string);
}
st.close();
rs.close();
conn.close();
System.out.println("第2条线程归还");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread thread3 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
Connection conn = dbcpool.DBUtils.getConnection();
System.out.println("开始调用第3个线程");
Statement st = conn.createStatement();
String sql = "select 'hello' as a from dual ";
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
String string = rs.getString("a");
System.out.println(string);
}
st.close();
rs.close();
conn.close();
System.out.println("第3条线程归还");
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread1.start();
thread2.start();
thread3.start();
}
}