1日目 2段階目まで

This commit is contained in:
2026-10-28 16:58:58 +09:00
parent 9932633268
commit df7972c662
10 changed files with 73 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil;
public class EmployeeDAO {
@@ -48,8 +49,8 @@ public class EmployeeDAO {
* @throws SQLException
* @throws ClassNotFoundException
*/
public List<String> selectAll() throws SQLException, ClassNotFoundException {
List<String> rtnList = new ArrayList<>();
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" " + "*");
@@ -63,7 +64,7 @@ public class EmployeeDAO {
rtnList.add(rs.getString(NM_EMPLOYEE));
}
} finally {
DbUtil.closeStatement(this.stmt);
DbUtil.closeStatement​(this.stmt);
}
return rtnList;
}

View File

@@ -0,0 +1,7 @@
package com.rhizome.example.dto;
public class EmployeeDTO {
}

View File

@@ -93,7 +93,7 @@ public class Main {
System.out.println(emp);
}
} finally {
DbUtil.closeConnection(empDao.con);
DbUtil.closeConnection​(empDao.con);
}
}

View File

@@ -1,6 +1,7 @@
package com.rhizome.example.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
// 定数定義(共通)
@@ -42,6 +43,10 @@ public class DbUtil {
* ※Connection生成には上記の定数を活用してください
*/
public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException {
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
return con;
}
/*
* TODO 以下の仕様でメソッドを作成してください。
@@ -53,6 +58,11 @@ public class DbUtil {
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/
public static void closeStatement​(java.sql.PreparedStatement stmt) throws java.sql.SQLException {
if (stmt != null) {
stmt.close();
}
}
/*
* TODO 以下の仕様でメソッドを作成してください。
@@ -64,5 +74,15 @@ public class DbUtil {
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/
//public static void closeConnection(Connection con) throws SQLException {
//if (con != null) {
//con.close();
//}
//
public static void closeConnection​(java.sql.Connection con) throws java.sql.SQLException {
if (con != null)
con.close();
}
}