上一篇的例子可以明显看出,在增删改查的时候,很多的代码都是重复的,
那么,是否可以将增删改查封装成一个类,方便使用者
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 { private JDBCUtils() { } 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) { } } } }