In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍数据库如何实现批量删除数据的操作方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
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 * * 功能:数据库操作 * */ 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 getTableNames() { List tableNames = new ArrayList(); 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 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(); }}以上是数据库如何实现批量删除数据的操作方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.