JDBC tool and javajdbc Tool

Source: Internet
Author: User

JDBC tool and javajdbc Tool
Database Connection toolkit under Util package

1 package util; 2 3 import java. SQL. connection; 4 import java. SQL. driverManager; 5 import java. SQL. resultSet; 6 import java. SQL. SQLException; 7 import java. SQL. statement; 8 import java. util. resourceBundle; 9 10 public class JdbcUtils {11 12 static final String DRIVERCLASS; 13 static final String URL; 14 static final String USER; 15 static final String PASSWORD; 16 17 static {18 19 // get Resource Bundle 20/* 21 * jdbc is jdbc under src. properties 22 **/23 ResourceBundle bundle = ResourceBundle. getBundle ("jdbc"); 24 25 // get the specified Element 26 DRIVERCLASS = bundle. getString ("driverClass"); 27 URL = bundle. getString ("url"); 28 USER = bundle. getString ("user"); 29 PASSWORD = bundle. getString ("password"); 30} 31 32 static {33 try {34 // register the driver 35 Class. forName (DRIVERCLASS); 36} catch (ClassNotFoundException E) {37 e. printStackTrace (); 38} 39} 40 41 // get Connection 42 public static Connection getConnection () throws SQLException {43 return DriverManager. getConnection (URL, USER, PASSWORD ); 44} 45 46/** 47 * release resources 48 * @ param conn 49 * @ param st 50 * @ param rs 51 */52 public static void CloseResource (Connection conn, statement st, ResultSet rs) {53 closeResultSet (rs); 54 closeStaement (st); 55 closeConn (conn); 56} 57 58/** 59 * release Connection 60 * @ param conn 61 * Connection 62 */63 public static void closeConn (Connection conn) {64 if (conn! = Null) {65 try {66 conn. close (); 67} catch (SQLException e) {68 e. printStackTrace (); 69} finally {70 conn = null; 71} 72} 73} 74 75/** 76 * release Statement performer 77 * @ param st 78 * Statement performer 79 */80 public static void closeStaement (Statement st) {81 if (st! = Null) {82 try {83 st. close (); 84} catch (SQLException e) {85 e. printStackTrace (); 86} finally {87 st = null; 88} 89} 90} 91 92/** 93 * release result set 94 * @ param rs 95 * result set 96 */97 public static void closeResultSet (ResultSet rs) {98 if (rs! = Null) {99 try {100 rs. close (); 101} catch (SQLException e) {102 e. printStackTrace (); 103} finally {104 rs = null; 105} 106} 107 108}
Jdbc. properties under src
1 driverClass = com. mysql. jdbc. Driver2 url = jdbc: mysql: // localhost: 3306/Database Name 3 user = username 4 password = password

 

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.