您的位置:首页 > 其它

JDBC | 从配置文件中读取驱动 (设计模式)

2007-04-03 02:43 411 查看
模板模式:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: