Java中怎样实现批量删除操作?
进行编写编程代码就能实现批量删除操作。
具体代码如下:
[java] <SPAN style="WHITE-SPACE: pre"> </SPAN>public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?
user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try {
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?
user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try {
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
[java] <SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param){
boolean flag = false;
getConnection();
try {
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;i<param.length;i++){
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
}
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
} catch (SQLException e) {
try {
con.rollback(); //进行事务回滚
} catch (SQLException ex) {
ex.printStackTrace();
}
}finally {
closeAll(null,pstmt,con);
}
return flag;
}
/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param){
boolean flag = false;
getConnection();
try {
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;i<param.length;i++){
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
}
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
} catch (SQLException e) {
try {
con.rollback(); //进行事务回滚
} catch (SQLException ex) {
ex.printStackTrace();
}
}finally {
closeAll(null,pstmt,con);
}
return flag;
上面是进行批量删除的编程码。
广告 您可能关注的内容 |