dbcp
先导入所需的jar包:commons.pool.jar,commons-dbcp.jar和数据库驱动包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private static BasicDataSource datasoure = new BasicDataSource(); 2 3 private static final String url = "jdbc:sqlserver://localhost:1433;databaseName = StuDB;"; 4 5 private static final String name = "sa"; 6 7 private static final String pwd = "123456"; 8 9 static { 10 11 datasoure.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 12 13 datasoure.setUrl(url); 14 15 datasoure.setUsername(name); 16 17 datasoure.setPassword(pwd); 18 19 } 20 /** 21 * 声明调用连接的方法 22 * @return 23 */ 24 public static Connection getConn(){ 25 26 Connection conn = null; 27 28 try { 29 30 conn = datasoure.getConnection(); 31 32 } catch (Exception e) { 33 // TODO: handle exception 34 e.printStackTrace(); 35 } 36 37 38 return conn; 39 40 } 41 42 43 /** 44 * 声明关闭连接的方法 45 * @param pstmt 46 * @param conn 47 * @param rs 48 */ 49 public static void close(PreparedStatement pstmt,Connection conn,ResultSet rs){ 50 51 try{ 52 53 if(pstmt!=null){ 54 55 pstmt.close(); 56 57 } 58 59 }catch(Exception e){ 60 61 e.printStackTrace(); 62 63 } 64 try{ 65 66 if(conn!=null){ 67 68 conn.close(); 69 70 } 71 72 }catch(Exception e){ 73 74 e.printStackTrace(); 75 76 } 77 try{ 78 79 if(rs!=null){ 80 81 rs.close(); 82 83 } 84 85 }catch(Exception e){ 86 87 e.printStackTrace(); 88 89 } 90 91 }