From 897a006918268aa71dd2ef879360b94a6d0a0bff Mon Sep 17 00:00:00 2001 From: y-nakano <26-y-nakano-tmn@it-college.co.jp> Date: Tue, 29 Sep 2026 17:21:43 +0900 Subject: [PATCH] =?UTF-8?q?1=E6=AE=B5=E9=9A=8E=E7=9B=AE=E3=81=A7=E3=81=99?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/rhizome/example/util/DbUtil.java | 37 ++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/com/rhizome/example/util/DbUtil.java b/src/com/rhizome/example/util/DbUtil.java index d2bb82e..e68ef1a 100644 --- a/src/com/rhizome/example/util/DbUtil.java +++ b/src/com/rhizome/example/util/DbUtil.java @@ -1,6 +1,9 @@ package com.rhizome.example.util; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class DbUtil { // 定数定義(共通) @@ -41,7 +44,19 @@ public class DbUtil { * 生成されたConnectionを返却する * ※Connection生成には上記の定数を活用してください */ + public static Connection getConnection() throws ClassNotFoundException, SQLException { + try { + Class.forName(DB_DRIVER); + return DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); + } catch (ClassNotFoundException e) { + System.out.println(); + throw e; + } catch (SQLException e) { + System.out.println(); + throw e; + } + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -52,7 +67,15 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする */ - + public static void closeStatement(PreparedStatement stmt) { + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + System.out.println(); + } + } + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -63,6 +86,16 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する */ - + public static void closeConnection(Connection con) { + if (con != null) { + try { + if (!con.isClosed()) { + con.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } }