import java.io.FileNotFoundException; import java.io.FileReader; import
java.io.IOException; import java.sql.Connection; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.SQLException; import
java.sql.Statement; import java.util.Properties; /* * EstablishJDBC Tool class *
Define aproperties file, Storage database4 Big information * Defined at member location5 Variables to store database information * Define a static code block *
a. UseProperties aggregate+IO Read information from configuration file, Save information to a collection * b. Get database connection information in the collection, Assign value to member variable *
c. Register to drive and get database connection objects * Define a static method, Used to get and return database connection objectsConnection * Define a static method, Used to release resources */
public class JDBCUtils { /** * Defined at member location5 Variables to store database information */ private static String
driver; private static String url; private static String user; private static
String password; /** * Define aConnection The variable of type is used to store the acquiredConnection Instanced object */ private
static Connection conn; /** * Private constructor, Prevent users from creating objects, Waste memory space */ private JDBCUtils() { }
static { try { /** * Useproperties Collection read configuration information */ FileReader fr = new
FileReader("data.properties"); Properties p = new Properties(); p.load(fr); /**
* Get database connection information in the collection, Assign value to member variable */ driver = p.getProperty("driver"); url =
p.getProperty("url"); user = p.getProperty("user"); password =
p.getProperty("password"); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /** *
Create driver and database connection objects */ try { Class.forName(driver); conn =
DriverManager.getConnection(url, user, password); } catch
(ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) {
e.printStackTrace(); /** * If the database connection fails, You should not go on, Throw runtime exception to virtual machine, Termination procedure */ throw new
RuntimeException(" Database connection failed!"); } } /** * Define a static method, Used to get database connection objectsConnection */
public static Connection getConnection() { return conn; } /** * Define a static method, Used to release resources
*/ public static void close(ResultSet rs, Statement stat, Connection conn) { if
(rs != null) { try { rs.close(); } catch (SQLException e) {
e.printStackTrace(); } } if (stat != null) { try { stat.close(); } catch
(SQLException e) { e.printStackTrace(); } } if (conn != null) { try {
conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }




import java.sql.Connection; import java.sql.ResultSet; import
java.sql.Statement; /* * testJDBCUtils Tool class */ public class Test { public static
void main(String[] args) { show(); } private static void show() { /** *
Get database connection objectConnection */ Connection conn = JDBCUtils.getConnection(); Statement
stat = null; ResultSet rs = null; try { /** * Get executionsql Statement's performer object */ stat =
conn.createStatement(); /** * implementsql Sentence */ String sql = "SELECT * FROM Table name"; rs =
stat.executeQuery(sql); /** * Process result set, ergodicSet aggregate */ if (rs.next()) {
System.out.println(rs.getInt("cid") + "\t" + rs.getString("cname")); } else {
System.out.println(" The specified data is not queried!"); } } catch (Exception e) {
e.printStackTrace(); } finally { /** * UseJDBCUtilsConfig Methods in tool classesclose Release resources */
JDBCUtils.close(rs, stat, conn); } } }