JDBC | 从配置文件中读取驱动 (设计模式)
2007-04-03 02:43
411 查看
模板模式:
策略模式(组合):
总结:
我们应该多用组合,少用继承!
本文出自 “悟道集” 博客,请务必保留此出处http://blackanger.blog.51cto.com/140924/22408
package cn.zhd; import cn.zhd.Configuration; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; /** * * @author hEart.hack * 模板模式,公共部分抽象为一个模板,子类再去实现需要的方法 * */ abstract class JdbcDemo1 { public void execute(){ Connection con=null; Statement stat=null; ResultSet rs=null; try{ Configuration cfg=new Configuration(".\\JdbcDemo.properties"); String className=cfg.getValue("driver"); String userName=cfg.getValue("user"); String passName=cfg.getValue("password"); String conName=cfg.getValue("connection"); Class.forName(className); con=DriverManager.getConnection(conName,userName,passName); stat=con.createStatement(); doExecute(stat); }catch(Exception e){ e.printStackTrace(); } finally{ if(rs != null) try{rs.close();}catch(Exception e){ e.printStackTrace(); } if(stat != null) try{stat.close();}catch(Exception e){ e.printStackTrace(); } if(con != null) try{ con.close(); }catch(Exception e){ e.printStackTrace(); } } } abstract void doExecute(Statement stat); } //继承模板的子类 public class JdbcDemo extends JdbcDemo1{ public void doExecute(Statement stat){ try{ stat.executeUpdate("delete from student where id=5 or id=6"); stat.executeUpdate("insert into student values(5,'张三')"); stat.executeUpdate("insert into student values(6,'lisi')"); ResultSet rs=stat.executeQuery("select * from student"); while(rs.next()){ System.out.println(rs.getString(1)+":"+rs.getString(2)); } }catch(SQLException e){ e.printStackTrace(); } } public static void main(String[] args){ JdbcDemo jb=new JdbcDemo(); jb.execute(); } } |
package cn.zhd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; //接口 interface MyPolicy { public void doExecute(Statement stmt); } //实现类--被策略类,JdbcDemo_celue类需要实现的方法,交给这个类去实现 class MyPolicyImpl implements MyPolicy { public void doExecute(Statement stat) { try{ stat.executeUpdate("delete from student where id=5 or id=6"); stat.executeUpdate("insert into student values(5,'张三')"); stat.executeUpdate("insert into student values(6,'lisi')"); ResultSet rs=stat.executeQuery("select * from student"); while(rs.next()){ System.out.println(rs.getString(1)+":"+rs.getString(2)); } }catch(SQLException e){ e.printStackTrace(); } } } //----------------------------- public class JdbcDemo_celue { MyPolicy mp = null; public static void main(String[] args){ JdbcDemo_celue jc=new JdbcDemo_celue(); MyPolicy mp1=new MyPolicyImpl(); jc.setPolicy(mp1); jc.execute(); } public void setPolicy(MyPolicy mp){ this.mp=mp; } public void execute(){ Connection con = null; Statement stat = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/students","root",""); stat = con.createStatement(); mp.doExecute(stat); } catch(Exception e) { e.printStackTrace(); } finally{ if(rs != null) try{ rs.close(); rs=null; }catch(Exception e){ e.printStackTrace(); } if(stat != null) try{ stat.close(); }catch(Exception e){ e.printStackTrace(); } if(con != null) try{ con.close(); }catch(Exception e){ e.printStackTrace(); } } } } |
我们应该多用组合,少用继承!
本文出自 “悟道集” 博客,请务必保留此出处http://blackanger.blog.51cto.com/140924/22408
相关文章推荐
- JDBC | 从配置文件中读取驱动 Ⅱ
- JDBC | 从配置文件中读取驱动
- Java基础之设计模式、读取配置文件、算法、linux命令
- JDBC | 从配置文件中读取驱动
- 设计模式之反射与配置文件
- 一只简单的网络爬虫(基于linux C/C++)————配置文件设计及读取
- 反射机制与配置文件结合的工厂设计模式
- javaJDBC(2)-抽取jdbcutil及配置文件读取数据库信息
- 一份采用单例模式编写,可读取配置文件的代码
- Spring使用配置文件读取jdbc.properties
- JDBC之类路径读取配置文件
- Spring 使用配置文件读取jdbc.properties
- 设计模式之反射与配置文件
- 【设计模式】反射+配置文件
- 单例模式读取配置文件到内存中
- Spring使用配置文件读取jdbc.properties
- 【设计模式】不就是换DB嘛(二)——升级版抽象工厂+反射+配置文件
- 设计模式之反射与配置文件
- (设计模式)简单工厂模式之通过配置文件动态创建实现类
- [Java 15 反射机制 ] 工厂模式与 properties 读取配置文件