Housekeeper items (tools layer)
package cn.kgc.gjp.tools;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author liurui
* @date $ {DATE} 15:21
* Create a connection pool C3P0 xml tools
* Connection pool DataSource (javax.sql) Interface
* getConnection()
* Steps for usage
* 1 Create a static object member position ComboPooleDataSource
* Copy the two files to the src directory c3p0xml
*. ComboPooleDataSource Object 3 from the definition of static methods to obtain and return Connection
* 4 defines a method to release resources
* /
Public class JDBCUtils {
// . 1 creates a static member in the position of the object ComboPooleDataSource
Private static ComboPooledDataSource the dataSource = new new ComboPooledDataSource ();
// Get Connection ComboPooleDataSource static methods defined in the object 3 and returns from the
public static Connection getConnection () {
the try {
return dataSource.getConnection ();
} The catch (SQLException E) {
the throw new new a RuntimeException ( "connection failed" + E);
}
}
// define a method can be returned to the pool
public static the DataSource GetDataSource () {
return the dataSource;
}
// 4 define a method for releasing resources
public static void Close (the ResultSet RES, the Statement State, Conn Connection) {
IF (RES! = Null ) {
the try {
res.close();
} catch (SQLException e) {
e.printStackTrace ();
}
}
if(state!=null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace ();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace ();
}
}
}
}