JDBCTools and jdbcjdbctools for operating Mysql
The following uses a unified tool class to encapsulate a set of JDBC operations for databases: 1) Obtain database link Resources 2) Release Database Link resources, including Connection, Statement, PreparedStatement, and ResultSet. 3) database update operations, including insert, delete, and modify 4) database query operations
First, 1) Obtain database link Resources
/*** The static method for getting the database link ensures that only one file is loaded. * @ return * @ throws Exception */public static Connection getConn () throws Exception {String jdbcDriver = null; String url = null; String user = null; String password = null; Properties p = new Properties (); InputStream is = JDBCTools. class. getClassLoader (). getResourceAsStream ("jdbc. properties "); p. load (is); jdbcDriver = p. getProperty ("jdbcDriver"); url = p. getProperty ("url"); user = p. getProperty ("user"); password = p. getProperty ("password"); Class. forName (jdbcDriver); return DriverManager. getConnection (url, user, password );}
Among them, jdbc. properties is the property configuration file, because it is obtained through the class name. class. getClassLoader (). getResourceAsStream ("jdbc. properties");, so
The property configuration file must be placed in the src directory. Example of its content:
JdbcDriver = com. mysql. jdbc. Driver
Url = jdbc: mysql: // localhost: 3306/test
User = root
Password = root
2) release database linked Resources
/*** Shut down the resources requested from the database server. * @ param conn * @ param pstmt */public static void closeResource (Connection conn, Statement stmt, resultSet rs) {if (rs! = Null) {try {rs. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (stmt! = Null) {try {stmt. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {e. printStackTrace ();}}
Because PreparedStatement is a sub-interface of Statement, this method is also suitable for passing in PreparedStatement objects.
3) database update operations, including insert, delete, and modify
/*** Unified update operation Statement: insert update delete * @ param conn * @ param SQL * @ throws Exception */public void update (String SQL) {Connection conn = null; statement stmt = null; try restarting conn1_jdbctools.getconn(1_stmt1_conn.createstatement(1_1_stmt.exe cuteUpdate (SQL);} catch (Exception e) {e. printStackTrace ();} finally {closeResource (conn, stmt, null );}} /*** applies to PreparedStatment * @ param SQL * @ param args */public void update2 (String SQL, Object... args) {Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try {conn = JDBCTools. getConn (); pstmt = conn. prepareStatement (SQL); for (int I = 0; I <args. length; I ++) {pstmt. setObject (I + 1, argspolici####pstmt.exe cuteUpdate ();} catch (Exception e) {e. printStackTrace ();} finally {JDBCTools. closeResource (conn, pstmt, rs );}}
In this Code, for (int I = 0; I <args. length; I ++) {pstmt. setObject (I + 1, args [I]);} indicates: Set the placeholder in the SQL statement? .
Finally, 4) database query operations: a general method is written here to encapsulate the query result set in a unified object. Here, a generic type is used, reflection mechanism knowledge
/*** Generic method reflection mechanism generic query method storage entity * @ param clazz * @ param SQL * @ param args * @ return */public <T> T getT (Class <T> clazz, string SQL, Object... args) {T t = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; ResultSetMetaData rsmd = null; try {conn = JDBCTools. getConn (); pstmt = conn. prepareStatement (SQL); for (int I = 0; I <args. length; I ++) {pstmt. setObject (I + 1, args‑i‑encoded parameters rs1_pstmt.exe cuteQuery (); if (rs. next () {t = clazz. newInstance (); Map <String, Object> map = new HashMap <String, Object> (); // parse SQL to obtain the Object rsmd = rs. getMetaData (); int numberOfColumns = rsmd. getColumnCount (); for (int I = 0; I <numberOfColumns; I ++ ){
// Obtain the column name. If an alias exists, the alias String columnName = rsmd is obtained. getColumnLabel (I + 1); map. put (columnName, rs. getObject (columnName);} if (map. size ()> 0) {for (Map. entry <String, Object> entry: map. entrySet () {String columnName = entry. getKey (); Object columnValue = entry. getValue (); Field field = t. getClass (). getDeclaredField (columnName); field. setAccessible (true); field. set (t, columnValue) ;}}} catch (Exception e) {e. printStackTrace ();} finally {JDBCTools. closeResource (conn, pstmt, rs);} return t ;}