This commit is contained in:
2026-10-28 16:54:53 +09:00
parent 9932633268
commit 195edf2188
10 changed files with 129 additions and 6 deletions

12
.classpath Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-8.0.23.jar"/>
<classpathentry kind="lib" path="実行例/DAO_Simple.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin/

23
.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>em-jdbc</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

7
.settings/.jsdtscope Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>

View File

@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER

View File

@@ -0,0 +1 @@
Global

View File

@@ -13,6 +13,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.rhizome.example.main.EmployeeDTO;
import com.rhizome.example.util.DbUtil; import com.rhizome.example.util.DbUtil;
public class EmployeeDAO { public class EmployeeDAO {
@@ -48,8 +49,8 @@ public class EmployeeDAO {
* @throws SQLException * @throws SQLException
* @throws ClassNotFoundException * @throws ClassNotFoundException
*/ */
public List<String> selectAll() throws SQLException, ClassNotFoundException { public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
List<String> rtnList = new ArrayList<>(); List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" SELECT "); sql.append(" SELECT ");
sql.append(" " + "*"); sql.append(" " + "*");
@@ -60,10 +61,12 @@ public class EmployeeDAO {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
EmployeeDTO emdto = new EmployeeDTO();
emdto.set
rtnList.add(rs.getString(NM_EMPLOYEE)); rtnList.add(rs.getString(NM_EMPLOYEE));
} }
} finally { } finally {
DbUtil.closeStatement(this.stmt); DbUtil.closeStatement();
} }
return rtnList; return rtnList;
} }

View File

@@ -0,0 +1,38 @@
package com.rhizome.example.dto;
public class EmployeeDTO {
//Employeeテーブルのカラム分のフィールド
private static final String TBL_NAME = "employee";
private static final String ID_EMPLOYEE = "id_employee";
private static final String NM_EMPLOYEE = "nm_employee";
private static final String KN_EMPLOYEE = "kn_employee";
private static final String MAIL_ADDRESS = "mail_address";
private static final String PASSWORD = "password";
private static final String ID_DEPARTMENT = "id_department";
public static String getTblName() {
return TBL_NAME;
}
public static String getIdEmployee() {
return ID_EMPLOYEE;
}
public static String getNmEmployee() {
return NM_EMPLOYEE;
}
public static String getKnEmployee() {
return KN_EMPLOYEE;
}
public static String getMailAddress() {
return MAIL_ADDRESS;
}
public static String getPassword() {
return PASSWORD;
}
public static String getIdDepartment() {
return ID_DEPARTMENT;
}
}

View File

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

View File

@@ -1,6 +1,10 @@
package com.rhizome.example.util; package com.rhizome.example.util;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DbUtil { public class DbUtil {
// 定数定義(共通) // 定数定義(共通)
@@ -41,6 +45,25 @@ public class DbUtil {
* 生成されたConnectionを返却する * 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください * ※Connection生成には上記の定数を活用してください
*/ */
//DB接続用メソッドを作成する
public static Connection getConnection() throws ClassNotFoundException {
//データベースの接続オブジェクトを初期化
Connection con = null;
// JDBCドライバの読み込み
Class.forName("com.mysql.cj.jdbc.Driver");
// Connectionの生成
try {
con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
, "rezouser" // ユーザ
, "Rezo_0000" // パスワード
);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/* /*
@@ -52,7 +75,15 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/ */
public static void closeStatement() {
PreparedStatement stmt = null;
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
@@ -63,6 +94,12 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/ */
public static void closeConnection() {
try {
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }