您好,登錄后才能下訂單哦!
這篇文章主要介紹數據庫如何實現批量刪除數據的操作方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
package com.mescs.baseinfo.utils; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory;/** * * 類名:DatabaseUtil <br /> * * 功能:數據庫操作 * */ public class DatabaseUtil { private final static Logger LOGGER = LoggerFactory.getLogger(DatabaseUtil.class); private static final String DRIVER = "org.firebirdsql.jdbc.FBDriver"; private static final String URL = "jdbc:firebirdsql:embedded:mescs.fdb?lc_ctype=UTF8"; private static final String USERNAME = "root"; private static final String PASSWORD = "root"; private static final String SQL = "DELETE FROM ";// 數據庫數據庫表內容清空 static { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { LOGGER.error("數據庫驅動加載失敗===>>>", e); } } /** * 獲取數據庫連接 * * @return */ public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { LOGGER.error("數據庫連接失敗===>>>", e); } return conn; } /** * 關閉數據庫連接 * @param conn */ public static void closeConnection(Connection conn) { if(conn != null) { try { conn.close(); } catch (SQLException e) { LOGGER.error("數據庫關閉失敗===>>>", e); } } } /** * 獲取數據庫下的所有表名 */ public static List<String> getTableNames() { List<String> tableNames = new ArrayList<String>(); Connection conn = getConnection(); ResultSet rs = null; try { //獲取數據庫的元數據 DatabaseMetaData db = conn.getMetaData(); //從元數據中獲取到所有的表名 rs = db.getTables(null, null, null, new String[] { "TABLE" }); while(rs.next()) { tableNames.add(rs.getString(3)); } } catch (SQLException e) { LOGGER.error("獲取數據庫表名失敗===>>>", e); } finally { try { rs.close(); closeConnection(conn); } catch (SQLException e) { LOGGER.error("關閉數據庫失敗===>>>", e); } } return tableNames; } /* * 清空數據庫表 */ public static void emptyDatabase() throws SQLException { List<String> tableNames = getTableNames(); Connection conn = getConnection(); PreparedStatement pStemt = null; for(String table:tableNames){ if(!table.equals("SYSPARAM") && !table.equals("T_CLEAR_DATA")){ String tableSql = SQL + table; try { pStemt = conn.prepareStatement(tableSql); Statement stmt=conn.createStatement(); stmt.executeUpdate(tableSql); } catch (SQLException e) { e.printStackTrace(); } } } conn.close(); } }
以上是數據庫如何實現批量刪除數據的操作方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。