把註冊驅動放到JDBC工具類
阿新 • • 發佈:2018-11-27
改動的地方:
JDBCTest:
JDBCUtil:
JDBCTest:
package com.test.example; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mysql.jdbc.Driver; import com.test.util.JDBCUtil; public class JDBCTest { public static void main(String[] args) { // TODO Auto-generated method stub Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { //1. 註冊驅動 connection = JDBCUtil.getConn(); /* //Driver 這個類裡面有靜態程式碼塊,一上來就執行了,所以等同於我們註冊了兩次驅動。 其實沒這個必要的。 //靜態程式碼塊 ---> 類載入了,就執行。 java.sql.DriverManager.registerDriver(new Driver()); //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); //2. 建立連線 引數一: 協議 + 訪問的資料庫 , 引數二: 使用者名稱 , 引數三: 密碼。 connection = DriverManager.getConnection("jdbc:mysql://localhost/jdbc", "root", "root");*/ //3. 建立statement , 跟資料庫打交道,一定需要這個物件 statement = connection.createStatement(); //4. 執行查詢 , 得到結果集 String sql = "select * from stu"; resultSet = statement.executeQuery(sql); //5. 遍歷查詢每一條記錄 while(resultSet.next()){ int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); System.out.println("id="+id+",name="+name+",age="+age); } // resultSet.close(); // Statement.close(); // connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtil.close(connection, resultSet, statement); } } }
JDBCUtil:
package com.test.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtil { static String url = "jdbc:mysql://localhost/jdbc"; static String name = "root"; static String password = "root"; /** * 註冊驅動 建立引數 * <p>Title: close</p> * <p>Description: </p> * @param connection * @param resultSet * @param statement */ public static Connection getConn(){ Connection connection = null; //2. 建立連線 引數一: 協議 + 訪問的資料庫 , 引數二: 使用者名稱 , 引數三: 密碼。 try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(url, name, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } /** * 釋放資源 * <p>Title: close</p> * <p>Description: </p> * @param connection * @param resultSet * @param statement */ public static void close(Connection connection,ResultSet resultSet,Statement statement){ closeRS(resultSet); closeSt(statement); closeConn(connection); } private static void closeRS(ResultSet resultSet){ try { if(resultSet !=null){ resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { resultSet = null; } } private static void closeSt(Statement statement){ try { if(statement !=null){ statement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { statement = null; } } private static void closeConn(Connection connection){ try { if(connection !=null){ connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { connection = null; } } }
資料庫:
執行結果: