Connect to and release a JDBC connection

Source: Internet
Author: User
Tags driver database
Welcome to the Java Community Forum and interact with 2 million technical staff for a long time. hibernate is now dedicated to summarizing the knowledge about JDBC, the purpose is to remind you that many basic Java knowledge needs to be improved. Packagecom. ssh. action; importjava. SQL. Connection;

Welcome to the Java Community Forum, and interact with 2 million technical staff> after a long time, hibernate now has a special summary of how JDBC achieves database connection and release, the purpose is to remind you that many basic Java knowledge needs to be improved. Package com. ssh. action; import java. SQL. Connection;

Welcome to the Java Community Forum and interact with 2 million technical staff> enter

After using hibernate for a long time, I have summarized the knowledge about JDBC on how to implement database connection and release. The purpose is to remind myself that many basic Java knowledge needs to be improved.

Package com. ssh. action;

Import java. SQL. Connection;

Import java. SQL. DriverManager;

Import java. SQL. PreparedStatement;

Import java. SQL. ResultSet;

Import java. SQL. SQLException;

Import java. SQL. Statement;

/**

* Database tools

* @ Author Administrator

*

*/

Public class DBUtil {

/**

* Obtain the database connection

* @ Return

*/

Public static Connection getConnection (){

Connection conn = null;

Try {

// Obtain jdbc configuration information

JdbcInfo jdbcInfo = null; // ConfigReader. getInstance (). getJdbcInfo ();

Class. forName (jdbcInfo. getDriverName ());

Conn = DriverManager. getConnection (jdbcInfo. getUrl (), jdbcInfo. getUsername (), jdbcInfo. getPassword ());

} Catch (ClassNotFoundException e ){

E. printStackTrace ();

} Catch (SQLException e ){

E. printStackTrace ();

}

Return conn;

}

/**

* Disable PreparedStatement (pre-Execute statement) Objective: To prevent SQL injection, it is relatively efficient (not absolute) in a specific driver database, and does not need to be compiled frequently because it has been preloaded.

* @ Param pstmt

*/

Public static void close (PreparedStatement pstmt ){

If (pstmt! = Null ){

Try {

Pstmt. close ();

} Catch (SQLException e ){

E. printStackTrace ();

}

}

}

/**

* Close the connection.

* @ Param conn

*/

Public static void close (Connection conn ){

If (conn! = Null ){

Try {

Conn. close ();

} Catch (SQLException e ){

E. printStackTrace ();

}

}

}

/**

* Disable the data table of the database result set.

* @ Param rs

*/

Public static void close (ResultSet rs ){

If (rs! = Null ){

Try {

Rs. close ();

} Catch (SQLException e ){

E. printStackTrace ();

}

}

}

/**

* Submit a transaction.

* @ Param conn

*/

Public static void commit (Connection conn ){

If (conn! = Null ){

Try {

Conn. commit ();

} Catch (SQLException e ){

E. printStackTrace ();

}

}

}

[1] [2]

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.