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(); + } + } + } }