时间:2021-07-01 10:21:17 帮助过:27人阅读
1 //DBCP通过配置文件代码演示: 2 //配置文件: 3 driverClassName=com.mysql.jdbc.Driver 4 username=root 5 password=root 6 url=jdbc:mysql://localhost:3306/tes
1 //演示代码: 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.sql.Connection; 6 import java.sql.SQLException; 7 import java.util.Properties; 8 9 import javax.sql.DataSource; 10 11 import org.apache.commons.dbcp.BasicDataSourceFactory; 12 /** 13 * 通过配置文件使用连接池 14 * @author zxy 15 * 16 */ 17 public class Demo { 18 public static void main(String[] args) { 19 //创建properties对象 20 Properties prop = new Properties(); 21 Connection conn=null; 22 try { 23 //加载配置文件,传入输入流 24 prop.load(new FileInputStream("src/dbcp.properties")); 25 //获取链接 26 DataSource bs = BasicDataSourceFactory.createDataSource(prop); 27 conn = bs.getConnection(); 28 //测试输出 29 System.out.println(conn); 30 } catch (FileNotFoundException e) { 31 e.printStackTrace(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 }finally { 37 //关闭链接 38 if (conn!=null) { 39 try { 40 conn.close(); 41 } catch (SQLException e) { 42 e.printStackTrace(); 43 } 44 } 45 } 46 } 47 }
DBCP连接池的使用
标签:stat apach file factor inpu 1.5 dbcp nal ring