首页 > 编程知识 正文

was jdbc连接池配置,was数据库连接池配置

时间:2023-05-04 09:43:16 阅读:174481 作者:3180

1 importjava.beans.BeanInfo; 2 importjava.beans.Introspector; 3 import Java.beans.property descriptor; 4 importjava.sql.Connection; 5 import Java.SQL.prepared statement; 6 importjava.sql.ResultSet; 7 import Java.SQL.resultset metadata; 8 importjava.sql.SQLException; 9 importjava.sql.Statement; 10 importjava.util.ArrayList; 11 importjava.util.List; 12

13 importjavax.sql.DataSource; 14

15 import com.m change.v2.C3 P0.combopooleddatasource; 16

17公共类jdbcutils { 18

使用19//C3P0数据库连接池管理数据库连接

20私有数据资源=newcombopooleddatasource (21

22 //取得连接

23公共连接连接(throws sqlexception ) 24返回数据源. getconnection ); 25 ) 26

27 //关闭连接

28公共连接29if ) conn!=null ) {30 try{31 conn.close (; 32 )缓存(sqlexception e ) 33 ) 34 ) 35 ) 36

37//关闭语句

38公共状态卷类(statement stmt ) 39if ) stmt!=null ) {40 try{41 stmt.close (; 42 )缓存(sqlexception e ) 43 ) 44 ) 45 ) 46

47 //关闭结果集

48 publicstaticvoidclose (resultset RS ) 49if ) RS!=null ) {50 try{51 rs.close (; 52 )缓存(sqlexception e ) 53 ) 54 ) 55 ) 56

57 //关闭结果集、语句和连接

58 publicstaticvoidcloseall (resultset RS ) 59if ) RS==null ) {60 return; 61 ) 62try(63close ) RS; 关闭64//statement,然后关闭conn,即可显示“youcannotoperateonaclosed 65//statement! ”被扔出去。 由于的异常,conn没有关闭

66close(RS.getstatement ) (.getConnection ) ); 67close(RS.getstatement ) ); 68

69 ) catch(sqlexceptione ) {70 e.printStackTrace ); 71(72 ) 73

74 //结果集,关闭语句

75 publicstaticvoidcloseresultsetandstatement (resultset RS ) 76if ) RS==null ) {77 return; 78 ) 79try(80close ) RS; 81close(RS.getstatement ); 82 ) catch(sqlexceptione ) 83

84 ) 85 ) 86

执行sql语句,如87//insert、update和delete

88公共静态执行更新(干净的草莓,Object. parameters ) throwssqlexception ) 89connectionconn=null; 90 try{91 conn=getConnection (; 92returnexecuteupdate(Conn,sql,parameters; 93 ) finally{94close(Conn ); 95 ) 96 ) 97

执行sql语句,如98//insert、update和delete

99 publicstaticintexecuteupdate (connection conn,清秀的草莓,Object. parameters ) throws

100 sqlexception { 101 preparedstatementps=null; 102 try { 103 PS=conn.preparestatement (SQL ); 104for(intI=0; i parameters.length; I ) 105PS.setobject(I1,parameters[i]; 106 ) 107返回PS.execute update (; 108 ) finally{109close(PS ); 十一一二十二

执行113 //查询

14 publicstaticresultsetexecutequery (清纯草莓,Object. parameters ) throwssqlexception ) 115connectionconn=null; 116 try{117 conn=getConnection (; 18返回执行查询(conn,sql,parameters ); 19 ) catch(sqlexceptionex ) (120close ) Conn; 121 throwex; 122 ) 123 ) 124

执行125 //查询

126 publicstaticresultsetexecutequery (connection conn,清纯草莓,Object. parameters ) throws

127 sqlexception { 128 preparedstatementps=null; 129 try{130 ResultSet rs=null; 131 PS=conn.preparestatement (SQL; 132for(intI=0; i parameters.length; I ) 133PS.setobject(I1,parameters[i]; 134 ) 135RS=PS.executequery (; 136返回RS; 137 ) catch(sqlexceptionex ) (138close ) PS; 139 throwex; 140 141 142

143 //回滚

144公共连接备份(connection conn ) 145try ) 146conn.rollback ); 147 )缓存(sqlexception e ) 148

149 }150 }151 }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。