C3P0 database Connection pool Use--Create Jdbctools public class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.