Connect to and release a JDBC connection

Source: Internet
Author: User
Tags driver database

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 tool class * @ author Administrator **/public class DBUtil {/*** get database Connection * @ return */public static Connection getConnection () {Connection conn = null; try {// obtain jdbc configuration information JdbcInfo jdbcInfo = nul L; // 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 (preprocessing execution statement) Objective: it can prevent SQL injection. It is relatively efficient (not absolute) in a specific driver database and does not require frequent compilation. because pre-added * @ Param pstmt */public static void close (PreparedStatement pstmt) {if (pstmt! = Null) {try {pstmt. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** close Connection * @ param conn */public static void close (Connection conn) {if (conn! = Null) {try {conn. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** close 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 transaction * @ param conn */public static void commit (Connection conn) {if (conn! = Null) {try {conn. commit ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** rollback transaction * @ param conn */public static void rollback (Connection conn) {if (conn! = Null) {try {conn. rollback ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** automatically submit transaction * @ param conn * @ param autoCommit */public static void setAutoCommit (Connection conn, boolean autoCommit) {if (conn! = Null) {try {conn. setAutoCommit (autoCommit);} catch (SQLException e) {e. printStackTrace () ;}}/ *** close the execution Statement * @ param stmt */public static void close (Statement stmt) {if (stmt! = Null) {try {stmt. close () ;}catch (SQLException e) {e. printStackTrace ();}}}}

 

This is just a tool class for creating JDBC: The following describes how to use it for writing:
Public static void main (String [] args) throws Exception {Connection conn = null; PreparedStatement pstmt = null; try {conn = DBUtil. getConnection (); // create a connection String SQL = ""; // write the SQL pstmt = conn. prepareStatement (SQL); // pre-compiled SQL pstmt. setString (1 ,"");//? The parameter value is pstmt.exe cuteUpdate (); // Execute SQL} catch (Exception e) {throw e; // if this Exception is intercepted, throws must be displayed so that the Exception can be thrown into the servlet, alternatively, you do not need to intercept} finally {DBUtil. close (pstmt); DBUtil. close (conn );}}

 

 

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.