为了提高写程序效率,做了一个简单的封装JDBC类
SqLModify.java
使用方法
SqLModify.modify("insert into usertable(username,password)values('lucy','123456')");  
这个是静态方法的调用 可以执行insert   update  和直接调用jdbc方式比少写了很多的代码
为了防止特殊字符 和 SQL注入可以用另外的调用方式
SqLModify sqlcom = new SqlModify(true);  //true表示建立连接
sqlcom.setSql("insert into usertable(username,password)values(?,?)"); 
sqlcom.setString(1,"lucy");
sqlcom.setString(2,"123456");
sqlcom.exesqlandClose();
如果是插入多条记录可以使用循环
SqLModify sqlcom = new SqlModify(true);  //true表示建立连接
sqlcom.setSql("insert into usertable(username,password)values(?,?)"); 
for(int i=0;i<30;i++)
{
   
   sqlcom.setString(1,"lucy");
   sqlcom.setString(2,"123456");
   sqlcom.exesql();
}
sqlcom.closeall();
SqlModify.java
代码
import conn.DBConnManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class SqlModify 
{
 private String SQL;
 static SqlModify sqlcom = null;
 private DBConnManager conn = null;
 private Connection con = null;
 private PreparedStatement stmt = null;
 
 public SqlModify()
 {
  
 }
 public SqlModify(boolean connect)
 {
  if(connect)
   connect();
 }
 public static int modify(String sqlStr)
 {
  if(sqlcom==null)
    sqlcom = new SqlModify();
  return sqlcom.exec(sqlStr);
 }
 public PreparedStatement getStmt()
 {
  return stmt;
 }
 public void connect(String sqlStr)
 {
  try{
   connect();
   stmt = con.prepareStatement(sqlStr);
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public void connect()
 {
  try{
   conn = DBConnManager.getInstance();
   con  = conn.getConnection("mssql");
  }catch(Exception e){
   
  }
 }
 public int exesqlandClose()
 {
  int result = exesql();
  closeall();
  return result;
 }
 public int exesql()
 {
  try{
   return stmt.executeUpdate();
  }catch(Exception e){
   e.printStackTrace();
   return -1;
  }
 }
 public void setSql(String sql)
 {
  try{
   stmt = con.prepareStatement(sql);
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   
  }
 }
 public int exec(String sqlStr)
 {
  int flag=-2;
  try{
   connect(sqlStr); 
   int value=stmt.executeUpdate();
   flag=value;
  }catch(java.lang.Exception ex){
   ex.printStackTrace();
  }finally{
   closeall();
  }
  return flag;
 }
 public void closeall()
 {
  try{
   if(stmt!=null)
   {
    stmt.close();
   }
   if(conn!=null)
   {
    conn.releaseConnection("mssql",con);
   }
   
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public void setString(int col,String value)
 {
  try{
   stmt.setString(col,value);
  }catch(java.lang.Exception ex){
   ex.printStackTrace();
  }  
 }
 public void setInt(int col,int value)
 {
  try{
   stmt.setInt(col,value);
  }catch(java.lang.Exception ex){
   ex.printStackTrace();
  }
   
 }
 public void setLong(int col,long value)
 {
  try{
   stmt.setLong(col,value);
  }catch(java.lang.Exception ex){
   ex.printStackTrace();
  } 
 }
 public void setFloat(int col,float value)
 {
  try{
   stmt.setFloat(col,value);
  }catch(java.lang.Exception ex){
   ex.printStackTrace();
  } 
 }
}