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 );}}