这篇文章主要介绍数据库如何实现批量删除数据的操作方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。