From ac0ebd51cebabb40331c1598f983bc12e4820ce7 Mon Sep 17 00:00:00 2001 From: m-shimizu <26-m-e-stella-tecnica@it-college.jp> Date: Tue, 29 Sep 2026 16:43:48 +0900 Subject: [PATCH] =?UTF-8?q?1=E6=AE=B5=E9=9A=8E=E7=9B=AE=EF=BC=88DB?= =?UTF-8?q?=E6=8E=A5=E7=B6=9A=E5=87=A6=E7=90=86=E4=BD=9C=E6=88=90=E2=86=92?= =?UTF-8?q?=E5=85=A8=E6=A4=9C=E7=B4=A2=E5=AE=8C=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/rhizome/example/util/DbUtil.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/com/rhizome/example/util/DbUtil.java b/src/com/rhizome/example/util/DbUtil.java index d2bb82e..3354fb2 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,13 @@ public class DbUtil { * 生成されたConnectionを返却する * ※Connection生成には上記の定数を活用してください */ + public static Connection getConnection() + throws ClassNotFoundException, SQLException { + Class.forName(DB_DRIVER); + con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); + return con; + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -52,7 +61,12 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする */ - + public static void closeStatement(PreparedStatement stmt) + throws SQLException { + if (stmt != null) { + stmt.close(); + } + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -63,6 +77,11 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する */ - + public static void closeConnection(Connection con) + throws SQLException { + if (con != null) { + con.close(); + } + } }