A: Extract two methods, one to get Connection object, one to release the resource import Java.io.filereader;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.resultset;import Java.sql.sqlexception;import Java.sql.Statement;import Java.util.Properties; public class Jdbcutil {private Jdbcutil () {}//Privatization construction method Defines the member variable private static String jdbcurl= null; private static String userName = null; private static String password = NULL; Load data Static {try { Properties prop = new properties (); Prop.load (New FileReader ("Jdbc.properties")); Jdbcurl = Prop.getproperty ("Jdbcurl"); UserName = Prop.getproperty ("UserName"); Password = prop.getproperty ("password"); Class.forName (Prop.getProperty ("Driverclassname")); } catch (Exception e) {e.printstacktrace (); }}/** * get connection */public static Connection getconnection () t Hrows SQLException {return drivermanager.getconnection (jdbcurl, userName, password); }/** * Frees resources */public static void Close (Connection conn, Statement St, ResultSet rs) t Hrows SQLException {if (conn! = null) conn.close (); if (st! = NULL) st.close (); if (rs! = null) rs.close (); }}
JDBC's Tool classes