5 Commits

Author SHA1 Message Date
b4f52cbc55 コミットし直し 2026-09-30 16:20:10 +09:00
f5ef9d86f6 段階4完了 2026-09-30 16:10:30 +09:00
54565e4472 段階3完了 2026-09-30 13:20:01 +09:00
da3b79b06b 段階2完了 2026-09-30 10:56:35 +09:00
fb8d2c0b1c 1段階目完了しました 2026-09-29 16:30:38 +09:00
9 changed files with 125 additions and 82 deletions

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<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
View File

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

View File

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

View File

@@ -1,7 +0,0 @@
<?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

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

View File

@@ -1 +0,0 @@
Global

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,20 +49,27 @@ 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(" " + "*");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
try {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
//DBのカラムをリストに追加
while (rs.next()) {
// EmployeeDAO dao = new EmployeeDAO(con);
rtnList.add(rs.getString(NM_EMPLOYEE));
EmployeeDTO dto = new EmployeeDTO();
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
dto.setName(rs.getString(NM_EMPLOYEE));
dto.setKana(rs.getString(KN_EMPLOYEE));
dto.setMail(rs.getString(MAIL_ADDRESS));
dto.setPass(rs.getString(PASSWORD));
dto.setDepid(rs.getInt(ID_DEPARTMENT));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
@@ -69,4 +77,65 @@ public class EmployeeDAO {
return rtnList;
}
public List<EmployeeDTO> selectByld(int id) throws SQLException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
//社員テーブルから指定された社員IDに一致するデータを取得
sql.append(" SELECT ");
sql.append(" " + "*");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" " + ID_EMPLOYEE);
sql.append(" = ");
sql.append(" " + id);
try {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
//DBのカラムをリストに追加
while (rs.next()) {
EmployeeDTO dto = new EmployeeDTO();
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
dto.setName(rs.getString(NM_EMPLOYEE));
dto.setKana(rs.getString(KN_EMPLOYEE));
dto.setMail(rs.getString(MAIL_ADDRESS));
dto.setPass(rs.getString(PASSWORD));
dto.setDepid(rs.getInt(ID_DEPARTMENT));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
}
if (rtnList.size() == 0) {
System.out.println("入力された値で情報が見つかりませんでした");
}
return rtnList;
}
public void empDelete(int id) throws SQLException {
StringBuilder sql = new StringBuilder();
//社員テーブルから指定された社員IDに一致するデータを取得
sql.append(" DELETE ");
sql.append(" " + "*");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" " + ID_EMPLOYEE);
sql.append(" = ");
sql.append(" " + id);
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.executeUpdate();
System.out.println("削除が完了しました");
} finally {
DbUtil.closeStatement(this.stmt);
}
}
public void emplnsert (String name, String kana, String mail, String pass, int id) {
StringBuilder sql = new StringBuilder();
}
}

View File

@@ -7,6 +7,7 @@ import java.util.List;
import java.util.Scanner;
import com.rhizome.example.dao.EmployeeDAO;
import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil;
/**
@@ -15,7 +16,6 @@ import com.rhizome.example.util.DbUtil;
*/
public class Main {
static Scanner sc = new Scanner(System.in);
/**
* テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル
*/
@@ -45,6 +45,8 @@ public class Main {
* @throws SQLException
*/
public void start() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
System.out.println();
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
@@ -58,6 +60,7 @@ public class Main {
case "3":
break;
case "4":
empDelete();
break;
case "9":
return;
@@ -70,6 +73,29 @@ public class Main {
}
}
private void empDelete() throws SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
List<EmployeeDTO> empList = empDao.selectByld(id);
if (empList.isEmpty()) {
} else {
System.out.println("指定されたIDの情報を削除します? Y/y");
System.out.println("Y/y以外の入力であればキャンセル");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if (str.equals("Y") || str.equals("y")) {
empDao.empDelete(id);
} else {
System.out.println("キャンセルしました");
}
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
/**
* 社員テーブルから情報を検索する
@@ -78,7 +104,7 @@ public class Main {
* @throws SQLException
*/
public static void empSelect() throws ClassNotFoundException, SQLException {
List<String> empList = new ArrayList<>();
List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
@@ -88,17 +114,20 @@ public class Main {
case "1":
empList = empDao.selectAll();
break;
case "2":
System.out.println("IDを入力してください");
int id = sc.nextInt();
empList = empDao.selectByld(id);
break;
default:
return;
}
System.out.println("取得件数" + empList.size());
for (String emp : empList) {
System.out.println(emp);
for (EmployeeDTO emp : empList) {
System.out.println(emp.getEmpid() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t"
+ emp.getMail() + "\t" + emp.getPass() + "\t" + emp.getDepid());
}
} finally {
DbUtil.closeConnection(empDao.con);
DbUtil.closeConnection(empDao.con);
}
}
@@ -129,7 +158,7 @@ public class Main {
* @return
*/
public static String inputCheck(String titleName, boolean flg) {
String str = null;
String str;
while (true) {
System.out.println(titleName + "を入力してください");
str = sc.nextLine();

View File

@@ -9,8 +9,8 @@ public class DbUtil {
// 定数定義(共通)
private static final String DB_HOST = "127.0.0.1";
private static final String DB_NAME = "rezodb";
private static final String DB_USER = "rezouser1";
private static final String DB_PASS = "Rezo_0001";
private static final String DB_USER = "rezouser";
private static final String DB_PASS = "Rezo_0000";
// 定数定義(PostgreSQL用)
// private static final String DBMS = "postgresql";
@@ -44,22 +44,11 @@ public class DbUtil {
* 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください
*/
//例外が2個あるためカンマでつなげることができる
public static Connection getConnection() throws SQLException {
//データベースへの接続
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
return con;
//データベースへの接続
con = DriverManager.getConnection(DB_URL, DB_USER ,DB_PASS);
return con;
}
// try {
// Class.forName("com.mysql.cj.jdbc.Driver");
// } catch (ClassNotFoundException e) {
// throw new SQLException("JDBCドライバが見つかりません", e);
// }
// //JDBCドライバの読み込み
// return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/rezodb"
// , "rezouser1"
// , "Rezo_0001");
/*
* TODO 以下の仕様でメソッドを作成してください。
@@ -70,12 +59,10 @@ public class DbUtil {
* 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/
// PreparedStatement型 / DBステートメント 例外: SQLException
public static void closeStatement( PreparedStatement stmt) throws SQLException {
//引数のDBステートメントがnullでない場合
if(stmt != null) {
//そのDBステートメントをクローズする
stmt.close();
public static void closeStatement(PreparedStatement ps) throws SQLException {
//DBステートメントのクローズ
if (ps != null) {
ps.close(); //DBステートメントをクローズ
}
}
/*
@@ -88,11 +75,10 @@ public class DbUtil {
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/
public static void closeConnection(Connection con) throws SQLException {
//引数のコネクションがnullでない場合
if(con != null) {
//そのコネクションを切断する
con.close();
//コネクションの切断
if (con != null) {
con.close(); //コネクション切断
}
}
}
}