Package COM.ATGUIGU.JDBC;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
public class Jdbctools {
Processing of database transactions
Commit a transaction
public static void Commit (Connection Connection) {
if (connection!=null) {
try {
Connection.commit ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
Rolling back a transaction
public static void rollback (Connection Connection) {
if (connection!=null) {
try {
Connection.rollback ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
Start a transaction
public static void Begintx (Connection Connection) {
if (connection!=null) {
try {
Connection.setautocommit (FALSE);
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
The database connection pool should be initialized only once, because a project requires only one connection pool, so create a static property and initialize it from a static block of code.
private static DataSource Datasource=null;
static{
Datasource=new Combopooleddatasource ("helloc3p0");
}
public static Connection getconnection () throws sqlexception{
return Datasource.getconnection ();
}
/**
* Get Database connection method
*
* @return
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection GetConnection1 () throws IOException,
ClassNotFoundException, SQLException {
//1, Four strings required to read the connection data source
//1) Create a Properties object
Properties Properties = new properties ();
2) Gets the input stream of the Properties object
InputStream InputStream = JDBCTools.class.getClassLoader ()
. getResourceAsStream (" Jdbc.properties ");
//3) load input stream
Properties.load (inputstream);
4) Gets the string
string driverclass = Properties.getproperty ("Driverclass");
String url = properties.getproperty ("Jdbcurl");
String user = Properties.getproperty ("user");
String password = properties.getproperty ("password");
//2, load driver
Class.forName (driverclass);
3. Create a Connection data source
Connection connection=drivermanager.getconnection (URL, using the DriverManager getconnection () method. user, password);
return connection;
}
/**
* Release Database Resource method
*
* @param resultSet
* @param statement
* @param connection
*/
public static void Release (ResultSet ResultSet, PreparedStatement PreparedStatement,
Connection Connection) {
if (ResultSet! = null) {
try {
Resultset.close ();
} catch (SQLException e) {
}
}
if (PreparedStatement! = null) {
try {
Preparedstatement.close ();
} catch (SQLException e) {
}
}
if (connection! = null) {
try {
The database connection pool's connection object is not actually closed when it is close, but instead the database connection is returned to the connection pool
Connection.close ();
} catch (SQLException e) {
}
}
}
C3P0 database Connection pool Use--Create Jdbctools public class