From 012e7adc0d77bf6cd84d129b7f4a28b05ab11c8b Mon Sep 17 00:00:00 2001 From: huoyo <1729913829@qq.com> Date: Sat, 28 May 2022 17:42:35 +0800 Subject: [PATCH] optimize the saver of database --- .../cn/langpy/kotime/util/DataBaseUtil.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java b/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java index 5ffac2f..b2fc223 100644 --- a/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java +++ b/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java @@ -32,7 +32,7 @@ public class DataBaseUtil { } public static int insert(Connection connection, String sql, Object[] values) { - PreparedStatement statement; + PreparedStatement statement = null; try { statement = connection.prepareStatement(sql); if (null != values) { @@ -44,6 +44,14 @@ public class DataBaseUtil { log.warning("Duplicate id:" + values[0]); } catch (SQLException throwables) { throwables.printStackTrace(); + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } } return 0; } @@ -72,7 +80,7 @@ public class DataBaseUtil { public static List> query(Connection connection, String sql, Object[] values) { List> list = new ArrayList<>(); - PreparedStatement statement; + PreparedStatement statement = null; try { statement = connection.prepareStatement(sql); if (null != values) { @@ -90,13 +98,21 @@ public class DataBaseUtil { } } catch (SQLException throwables) { throwables.printStackTrace(); + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } } return list; } public static List query(Connection connection, String sql, Object[] values, Class c) { List list = new ArrayList<>(); - PreparedStatement statement; + PreparedStatement statement = null; try { statement = connection.prepareStatement(sql); if (null != values) { @@ -139,6 +155,14 @@ public class DataBaseUtil { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } } return list; }