Java學習筆記48(DBUtils工具類一)
阿新 • • 發佈:2018-01-21
lex time name span 載器 .sql 定義 ride oot
上一篇的例子可以明顯看出,在增刪改查的時候,很多的代碼都是重復的,
那麽,是否可以將增刪改查封裝成一個類,方便使用者
package demo; /* * 實現JDBC的工具類 * 定義方法,直接返回數據庫的連接對象 * */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils { privateJDBCUtils() { } private static Connection con; static { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mybase"; String username = "root"; String password = "xuyiqing"; con = DriverManager.getConnection(url, username, password); }catch (Exception ex) { throw new RuntimeException(ex + "數據庫連接失敗"); } } /* * 定義靜態方法,返回數據庫的連接對象 */ public static Connection getConnection() { return con; } // 關閉方法 public static void close(Connection con, Statement stat) { if (stat != null) { try { stat.close(); } catch (SQLException ex) { } } if (con != null) { try { con.close(); } catch (SQLException ex) { } } } // 重載 public static void close(Connection con, Statement stat, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException ex) { } } if (stat != null) { try { stat.close(); } catch (SQLException ex) { } } if (con != null) { try { con.close(); } catch (SQLException ex) { } } } }
測試類:
package demo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestJDBCUtils { public static void main(String[] args) throws Exception { Connection con = JDBCUtils.getConnection(); PreparedStatement pst = con.prepareStatement("SELECT sname FROM sort"); ResultSet rs = pst.executeQuery(); while (rs.next()) { System.out.println(rs.getString("sname")); } JDBCUtils.close(con, pst, rs); } }
可以將表中的數據儲存到對象中:
package demo; public class Sort { private int sid; private String sname; private double sprice; private String sdesc; public Sort(int sid, String sname, double sprice, String sdesc) { this.sid = sid; this.sname = sname; this.sprice = sprice; this.sdesc = sdesc; } public Sort() { } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public double getSprice() { return sprice; } public void setSprice(double sprice) { this.sprice = sprice; } public String getSdesc() { return sdesc; } public void setSdesc(String sdesc) { this.sdesc = sdesc; } @Override public String toString() { return "Sort [sid=" + sid + ", sname=" + sname + ", sprice=" + sprice + ", sdesc=" + sdesc + "]"; } }
package demo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /* * JDBC讀取數據表sort,每行數據封裝到Sort類的對象中 * 很多個Sort類對象,存儲到List集合中 */ public class JDBCDemo { public static void main(String[] args) throws Exception{ //使用JDBC工具類,直接獲取數據庫連接對象 Connection con = JDBCUtils.getConnection(); //連接獲取數據庫SQL語句執行者對象 PreparedStatement pst = con.prepareStatement("SELECT * FROM sort"); //調用查詢方法,獲取結果集 ResultSet rs = pst.executeQuery(); //創建集合對象 List<Sort> list = new ArrayList<Sort>(); while(rs.next()){ //獲取到每個列數據,封裝到Sort對象中 Sort s = new Sort(rs.getInt("sid"),rs.getString("sname"),rs.getDouble("sprice"),rs.getString("sdesc")); //封裝的Sort對象,存儲到集合中 list.add(s); } JDBCUtils.close(con, pst, rs); //遍歷List集合 for(Sort s : list){ System.out.println(s); } } }
這裏有一個問題,我們自定義的工具類中的數據是不能修改的,那麽如果要改變驅動、用戶名、或是連接地址,很不方便
所以,可以把參數放在配置文件中,修改的時候只要改變配置文件即可,不需要修改源碼,有利於後期維護
配置文件通常放在scr文件下
這裏註意,在src目錄下創建文件的時候在bin目錄下也會自動復制這個文件
創建文件database.properties,寫入鍵值對
diverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybase
username=root
password=xuyiqing
使用IO流加載配置文件:
package demo; import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) throws Exception { //以前的方法 //FileInputStream fis1 = new FileInputStream("database.properties"); //這裏介紹新方法:類的加載器,從bin目錄下加載 InputStream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("database.properties"); Properties pro = new Properties(); pro.load(in); System.out.println(pro); } }
輸出:{password=xuyiqing, url=jdbc:mysql://localhost:3306/mybase, diverClass=com.mysql.jdbc.Driver, username=root}
成功加載
於是想到是否可以通過配置文件連接數據庫?
package demo; import java.io.InputStream; 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 JDBCUtils { private JDBCUtils() { } private static Connection con; static { try { InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("database.properties"); Properties pro = new Properties(); pro.load(in); String diverClass = pro.getProperty("driverClass"); String url = pro.getProperty("url"); String username = pro.getProperty("username"); String password = pro.getProperty("password"); Class.forName(diverClass); con = DriverManager.getConnection(url, username, password); } catch (Exception ex) { throw new RuntimeException(ex + "數據庫連接失敗"); } } public static Connection getConnection() { return con; } public static void close(Connection con, Statement stat) { if (stat != null) { try { stat.close(); } catch (SQLException ex) { } } if (con != null) { try { con.close(); } catch (SQLException ex) { } } } public static void close(Connection con, Statement stat, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException ex) { } } if (stat != null) { try { stat.close(); } catch (SQLException ex) { } } if (con != null) { try { con.close(); } catch (SQLException ex) { } } } }
Java學習筆記48(DBUtils工具類一)