JDBC | 从配置文件中读取驱动 Ⅱ

续昨天的blog:我用模板模式策略模式重写了那个例子程序,如下:
模板模式:
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();
                }
        }
       
    }
}
总结:
       我们应该多用组合,少用继承!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值