日期:2014-05-20  浏览次数:20833 次

求java工厂模式
public void saveOARole(Role role) throws SQLException{
Properties pro=new Properties();
String path = Thread.currentThread().getContextClassLoader().getResource("connect.properties").getPath();
try {
pro.load(new FileInputStream(path));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
String ClassName=pro.getProperty("ClassName");
String userName=pro.getProperty("userName");
String userPass=pro.getProperty("userPass");
String url1=pro.getProperty("url1");
try {
Class.forName(ClassName);
try {
con = DriverManager.getConnection(url1, userName, userPass);
} catch (SQLException e) {
e.printStackTrace();
}
try {
stmt = this.con.createStatement();
stmt1 = this.con.createStatement();
stmt2 = this.con.createStatement();
stmt3 = this.con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
rs=stmt.executeQuery("select * from hr_role where name='"+role.getName()+"'");
if(rs.next()==false){
rs1=stmt1.executeQuery("select * from hr_role_type where name='"+role.getType().getName()+"'");
if(rs1.next()==false){
String sql2 = "insert into hr_role_type(name) values ("+"'"+role.getType().getName()+"'"+")";
stmt1.executeUpdate(sql2);
rs2=stmt2.executeQuery("select * from hr_role_type where name='"+role.getType().getName()+"'");
while (rs2.next()) {
String ids=rs2.getString("id");
String sql3 = "insert into hr_role(name,rolecode,type_id) values ("+"'"+role.getName()+"'"+",'"+role.getRolecode()+"'"+",'"+ids+"'"+")";
stmt2.executeUpdate(sql3);
}
}else{
rs3=stmt3.executeQuery("select * from hr_role_type where name='"+role.getType().getName()+"'");
while (rs3.next()) {
String ids1=rs3.getString("id");
String sql1 = "insert into hr_role(name,rolecode,type_id) values ("+"'"+role.getName()+"'"+",'"+role.getRolecode()+"'"+",'"+ids1+"'"+")";
stmt3.executeUpdate(sql1);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally{
if (rs3!=null) {
rs3.close();
stmt3.close();
rs3=null;
}
if (rs2!=null) {
rs2.close();
stmt2.close();
rs2=null;

}
if (rs1!=null) {
rs1.close();
stmt1.close();
rs1=null;
}
if (rs!=null) {
rs.close();
stmt.close();
rs=null;
}
con.close();
}
}

求告收帮忙改成工厂模式
------最佳解决方案--------------------
引用:
是不是要把可重用的部分挖出来按通用化要求重新整合一下

你那一大段sql我觉得很怪异,感……
那三条语句不能少啊,
这个大致意思是,两个系统中有部分表和字段都一样,为了两边的数据都能同步,
所以才有那样的写法,第一次rs3=stmt3.executeQuery("select * from hr_role_type where name='"+role.getType().getName()+"'"); 是这这个系统查询另外一个系统里面是否有这条数据
,如果没有的话插入一行,接着第二次把刚插入的那个ID查出来,第三次出现是如果有着条数据的话,也用这个查询把它ID查出来
------其他解决方案--------------------