这是前一阵写的篇文章,不知道为什么今天登上来没有了,郁闷了好半天,只好重发一遍了。。。。
DataBaseContext
package
com;
import java.util.Map;
public class DataBaseContext ... {
DataBaseSuper dbs;
public void setOperate(DataBaseSuper dbs)...{
this.dbs=dbs;
}
public void excuteOperate()...{
this.dbs.operate();
}
}
import java.util.Map;
public class DataBaseContext ... {
DataBaseSuper dbs;
public void setOperate(DataBaseSuper dbs)...{
this.dbs=dbs;
}
public void excuteOperate()...{
this.dbs.operate();
}
}
DataBaseSuper
package
com;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Map;
import java.util.Iterator;
import java.util.Map.Entry;
public abstract class DataBaseSuper ... {
Connection conn;
Statement stmt;
public void openDB() throws Exception...{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=lesson";
String user = "sa";
String password = "123";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}
public void closeDB() throws Exception...{
stmt.close();
conn.close();
}
public abstract void operate();
}
class Insert extends DataBaseSuper ... {
Map map;
public Insert(Map map)...{
this.map=map;
}
public void operate()...{
try ...{
openDB();
String s1="";
String s2="";
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext())...{
Map.Entry entry= (Map.Entry)iterator.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
s1+=key+",";
s2+="'"+value+"'"+",";
}
s1=s1.substring(0,s1.length()-1);
s2=s2.substring(0,s2.length()-1);
String sql="insert into test("+s1+") values("+s2+")";
stmt.executeUpdate(sql);
closeDB();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
}
}
class Delete extends DataBaseSuper ... {
Map map;
public Delete(Map map)...{
this.map=map;
}
public void operate()...{
try ...{
openDB();
String s="";
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext())...{
Map.Entry entry= (Map.Entry)iterator.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
s+=key+"='"+value+"' and ";
}
s=s.substring(0,s.length()-5);
String sql="delete from test where "+s;
stmt.executeUpdate(sql);
closeDB();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
}
}
class Update extends DataBaseSuper ... {
int id;
Map map;
public Update(int id,Map map)...{
this.id=id;
this.map=map;
}
public void operate()...{
try ...{
openDB();
String s="";
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext())...{
Map.Entry entry= (Map.Entry)iterator.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
s+=key+"='"+value+"',";
}
s=s.substring(0,s.length()-1);
String sql="update test set "+s+" where id='"+id+"'";
stmt.executeUpdate(sql);
closeDB();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Map;
import java.util.Iterator;
import java.util.Map.Entry;
public abstract class DataBaseSuper ... {
Connection conn;
Statement stmt;
public void openDB() throws Exception...{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=lesson";
String user = "sa";
String password = "123";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}
public void closeDB() throws Exception...{
stmt.close();
conn.close();
}
public abstract void operate();
}
class Insert extends DataBaseSuper ... {
Map map;
public Insert(Map map)...{
this.map=map;
}
public void operate()...{
try ...{
openDB();
String s1="";
String s2="";
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext())...{
Map.Entry entry= (Map.Entry)iterator.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
s1+=key+",";
s2+="'"+value+"'"+",";
}
s1=s1.substring(0,s1.length()-1);
s2=s2.substring(0,s2.length()-1);
String sql="insert into test("+s1+") values("+s2+")";
stmt.executeUpdate(sql);
closeDB();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
}
}
class Delete extends DataBaseSuper ... {
Map map;
public Delete(Map map)...{
this.map=map;
}
public void operate()...{
try ...{
openDB();
String s="";
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext())...{
Map.Entry entry= (Map.Entry)iterator.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
s+=key+"='"+value+"' and ";
}
s=s.substring(0,s.length()-5);
String sql="delete from test where "+s;
stmt.executeUpdate(sql);
closeDB();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
}
}
class Update extends DataBaseSuper ... {
int id;
Map map;
public Update(int id,Map map)...{
this.id=id;
this.map=map;
}
public void operate()...{
try ...{
openDB();
String s="";
Iterator iterator=map.entrySet().iterator();
while(iterator.hasNext())...{
Map.Entry entry= (Map.Entry)iterator.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
s+=key+"='"+value+"',";
}
s=s.substring(0,s.length()-1);
String sql="update test set "+s+" where id='"+id+"'";
stmt.executeUpdate(sql);
closeDB();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
}
}