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]