Database links and database connection tools

Source: Internet
Author: User

Database links and database connection tools

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;

 

Public class DBUtils {
Private static final String URL = "jdbc: mysql: // localhost: 3306/jdbc? CharacterEncoding = UTF-8 ";
Private static final String UER_NAME = "root ";
Private static final String PDW = "123123 ";

Private static DBUtils me = new DBUtils ();


Public static DBUtils getInstance (){
Return me;
}

// To make the singleton mode the only way to obtain this object, set the non-argument constructor of this class to private
Private DBUtils (){}

/*
* Database connection
*/
Public Connection getConn (){
Connection conn = null;
Try {
Class. forName ("com. mysql. jdbc. Driver ");
Conn = DriverManager. getConnection (URL, UER_NAME, PDW );
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return conn;

}

/*
* Release resources
*/
Public void releaseRes (Connection conn, PreparedStatement pstmt, ResultSet rs ){

Try {
If (conn! = Null) conn. close ();
If (pstmt! = Null) pstmt. close ();
If (rs! = Null) rs. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

}

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.