1 导入 jia 包: c3p0-0.9.1.2.jar mchange-commons-java-0.2.11.jar mysql-connector-java-5.1.37-bin.jar
2 without using the configuration file c3p0-0.9.1.2.jar writing tools
public class C3P0Utils { Private static ComboPooledDataSource DS = new new ComboPooledDataSource (); // static database connection block disposed four elements of static { the try { ds.setDriverClass ( "com.mysql.jdbc.Driver" ); ds.setJdbcUrl ( "JDBC : MySQL: // localhost: 3306 / day04 " ); ds.setUser ( " the root " ); ds.setPassword ( " 123 " ); } the catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace ( ); } } public static Connection getConnection () throws SQLException { // get a connection, do not get themselves to DriverManager, but taken from the C3P0 connection pool return ds.getConnection (); } // close all the resources of the Uniform Code public static void closeAll (Connection conn , the Statement ST, the ResultSet RS) { // is responsible for closing IF (Conn =! null ) { the try { conn.Close (); } the catch (SQLException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } } if(st != null){ try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
3 using the configuration file c3p0-0.9.1.2.jar writing tools (Note: The configuration file name can not be changed)
public class C3P0Utils02 { Private static ComboPooledDataSource DS = new new ComboPooledDataSource (); // DS objects directly to load C3P0-config.xml file, which automatically loads the four elements is obtained with a loading database connection public static Connection the getConnection () throws SQLException { // get a connection, do not get themselves to DriverManager, but taken from the C3P0 connection pool return ds.getConnection (); } // close all the resources of the uniform Code public static void closeAll (connection conn, ST of Statement, ResultSet rs) { / / responsible for closing IF (conn! = null ) { the try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(st != null){ try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
4 C3P0 test class
public class C3P0UtilsTestDemo { public static void main (String [] args) throws SQLException { // INSERT (); // Delete (); Query (); } // insert public static void INSERT () throws SQLException { // . 1. obtaining a connection connection Conn = C3P0Utils02.getConnection (); // 2. Get the object performs the Statement ST = conn.createStatement (); // 3. SQL execution int rows = st.executeUpdate("insert into users (username,password) values ('王八','123321')"); System.out.println(rows); //4. C3P0Utils02.closeAll(conn, st, null); } //删除 public static void delete() throws SQLException{ //1.获取连接 Connection conn = C3P0Utils02.getConnection(); //2.获取执行对象 Statement st = conn.createStatement(); //3.执行sql int rows = st.executeUpdate("delete from users where uid = 7"); System.out.println(rows); //4. C3P0Utils02.closeAll(conn, st, null); } //查询 public static void query() throws SQLException{ //1.获取连接 Connection conn = C3P0Utils02.getConnection(); //2.获取执行对象 Statement st = conn.createStatement(); //3.执行sql ResultSet rs = st.executeQuery("select * from users"); while(rs.next()){ System.out.println(rs.getObject("uid")+"\t"+rs.getObject("username")+"\t"+rs.getObject("password")); } //4. C3P0Utils02.closeAll(conn, st, null); } }