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; /* * establish JDBC Tools *
Define a properties file , Storage database 4 Big message * Defined at member location 5 Variables to store database information * Define a static code block *
a. use Properties 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 objects Connection * Define a static method , Used to release resources */
public class JDBCUtils { /** * Defined at member location 5 Variables to store database information */ private static String
driver; private static String url; private static String user; private static
String password; /** * Define a Connection The variable of type is used to store the acquired Connection Instanced object */ private
static Connection conn; /** * Private constructor , Prevent users from creating objects , Waste memory space */ private JDBCUtils() { }
static { try { /** * use properties 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 objects Connection */
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; /* * test JDBCUtils Tools */ public class Test { public static
void main(String[] args) { show(); } private static void show() { /** *
Get database connection object Connection */ Connection conn = JDBCUtils.getConnection(); Statement
stat = null; ResultSet rs = null; try { /** * Get execution sql Statement's performer object */ stat =
conn.createStatement(); /** * implement sql sentence */ String sql = "SELECT * FROM Table name "; rs =
stat.executeQuery(sql); /** * Process result set , ergodic Set 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 { /** * use JDBCUtilsConfig Methods in tool classes close Release resources */
JDBCUtils.close(rs, stat, conn); } } }