1 import java.sql.*; 2 3 import utils.GlobalConstant; 4 5 public class JDBCUtil { 6 7 private JDBCUtil() { 8 9 } 10 11 /** 12 * 获取JDBC的数据库连接 13 */ 14 public static Connection getConnection() { 15 Connection conn = null; 16 try { 17 Class.forName(GlobalConstant.CONNECTION_DRIVER); 18 conn = DriverManager.getConnection(GlobalConstant.CONNECTION_URL, GlobalConstant.CONNECTION_USER, 19 GlobalConstant.CONNECTION_PASSWORD); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 /* 24 * try { conn = DataSourceService.getConnection(); } catch (SQLException 25 * e) { e.printStackTrace(); } 26 */ 27 return conn; 28 } 29 30 private static void close(ResultSet rs, Statement sm, Connection conn) { 31 try { 32 if (rs != null) 33 rs.close(); 34 if (sm != null) 35 sm.close(); 36 if (conn != null) 37 conn.close(); 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } 41 } 42 43 /** 44 * 关闭数据库的结果集 45 * 46 * @param rs 47 * 需要关闭的结果集 48 */ 49 public static void close(ResultSet rs) { 50 try { 51 if (rs != null) 52 rs.close(); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 } 56 } 57 58 /** 59 * 将一个标准的sql查询语句在数据库中查询,并且返回一个对应的结果集。 60 * 61 * @param sql 62 * 标准的sql查询语句 63 */ 64 public static ResultSet doQuery(String sql) { 65 Connection conn = getConnection(); 66 if (conn == null) 67 return null; 68 Statement sm = null; 69 ResultSet rs = null; 70 try { 71 sm = conn.createStatement(); 72 rs = sm.executeQuery(sql); 73 return rs; 74 } catch (SQLException e) { 75 e.printStackTrace(); 76 } finally { 77 if (rs == null && sm != null) { 78 close(null, sm, conn); 79 } 80 // close(null, sm, conn);这句肯定不行 81 } 82 return rs; 83 } 84 85 /** 86 * 将一个更新语句和对应的参数进行查询,并返回一个整形结果 87 * 88 * @param sql 89 * 数据库中的更新语句 90 * @param paras 91 * sql中对应的参数 92 */ 93 public static int doUpdate(String sql, String[] paras) { 94 Connection conn = getConnection(); 95 if (conn == null) 96 return 0; 97 PreparedStatement psm = null; 98 int result = 0; 99 try { 100 psm = conn.prepareStatement(sql); 101 for (int i = 0; i < paras.length; i++) { 102 psm.setString(i + 1, paras[i]); 103 } 104 result = psm.executeUpdate(); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } finally { 108 close(null, psm, conn); 109 } 110 return result; 111 } 112 }JDBCUtils
1 import java.beans.PropertyVetoException; 2 import java.sql.Connection; 3 import javax.sql.DataSource; 4 import com.mchange.v2.c3p0.ComboPooledDataSource; 5 6 import utils.GlobalConstant; 7 8 public class DataSourceUtils { 9 private static DataSource ds; 10 11 static { 12 /* 13 * 从配置文件读取配置信息 <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 14 * 默认配置,如果没有指定则使用这个配置 --> <default-config> <property 15 * name="checkoutTimeout">30000</property> <property 16 * name="idleConnectionTestPeriod">30</property> <property 17 * name="initialPoolSize">3</property> <property 18 * name="maxIdleTime">30</property> <property 19 * name="maxPoolSize">10</property> <property 20 * name="minPoolSize">3</property> <property 21 * name="maxStatements">50</property> <property 22 * name="acquireIncrement">3</property><!-- 如果池中数据连接不够时一次增长多少个 --> 23 * <property name="driverClass">com.mysql.jdbc.Driver</property> 24 * <property name="jdbcUrl"> 25 * <![CDATA[jdbc:mysql://127.0.0.1:3306/project?useUnicode=true& 26 * characterEncoding=UTF-8]]> </property> <property 27 * name="user">root</property> <property 28 * name="password">789123</property> </default-config> </c3p0-config> 29 * 30 * ds = // 默认的读取c3p0-config.xml中默认配置 new ComboPooledDataSource(); 31 */ 32 ComboPooledDataSource cpds = null; 33 try { 34 cpds = new ComboPooledDataSource(); 35 cpds.setCheckoutTimeout(30000); 36 cpds.setIdleConnectionTestPeriod(30); 37 cpds.setInitialPoolSize(3); 38 cpds.setMaxIdleTime(30); 39 cpds.setMaxPoolSize(70); 40 cpds.setMaxStatementsPerConnection(100); 41 cpds.setMinPoolSize(3); 42 cpds.setMaxStatements(75); 43 cpds.setAcquireIncrement(3); 44 cpds.setDriverClass(GlobalConstant.CONNECTION_DRIVER); 45 cpds.setJdbcUrl(GlobalConstant.CONNECTION_URL); 46 cpds.setUser(GlobalConstant.CONNECTION_USER); 47 cpds.setPassword(GlobalConstant.CONNECTION_PASSWORD); 48 ds = cpds; 49 } catch (PropertyVetoException e) { 50 System.out.println("与MySQL数据库连接失败!"); 51 } 52 } 53 54 private DataSourceUtils() { 55 56 } 57 58 public static DataSource getDatasSource() { 59 return ds; 60 } 61 62 public static Connection getConnection() { 63 Connection con = null; 64 try { 65 con = ds.getConnection();// 每一次从ds中获取一个新的连接 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 return con; 70 } 71 }DataSourceUtils
如果要验证其可用性,你们可以修改下以上的用户名,密码,数据库名
之后在自己的MySQL建立自己相应的数据库即可测试。