當前位置:編程學習大全網 - 編程語言 - Java中怎樣實現批量刪除操作?

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;

上面是進行批量刪除的編程碼。

  • 上一篇:繪制緩和曲線的程序設計
  • 下一篇:Flyme7評測:One Mind全面升級 更高效更智能
  • copyright 2024編程學習大全網