Compare commits
4 Commits
a-takayama
...
m_shimizu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae6a693aeb | ||
|
|
00cf2096e2 | ||
|
|
d2ebdbd11c | ||
|
|
ac0ebd51ce |
@@ -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
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
/bin/
|
||||
23
.project
23
.project
@@ -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>
|
||||
@@ -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>
|
||||
@@ -1 +0,0 @@
|
||||
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
|
||||
@@ -1 +0,0 @@
|
||||
Global
|
||||
@@ -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();
|
||||
while (rs.next()) {
|
||||
// EmployeeDAO dao = new EmployeeDAO(con);
|
||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPassword(rs.getString(PASSWORD));
|
||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
@@ -69,4 +77,50 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
public List<EmployeeDTO> selectById(int id) throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" * ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE ID_EMPLOYEE = ? ");
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1, id);
|
||||
rs = this.stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPassword(rs.getString(PASSWORD));
|
||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//社員データ削除
|
||||
public void empDelete(int id) throws SQLException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE ID_EMPLOYEE = ? ");
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1, id);
|
||||
this.stmt.executeUpdate();
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
59
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
59
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
private int idEmployee;
|
||||
private String nmEmployee;
|
||||
private String knEmployee;
|
||||
private String mailAddress;
|
||||
private String password;
|
||||
private int idDepartment;
|
||||
|
||||
public int getIdEmployee() {
|
||||
return idEmployee;
|
||||
}
|
||||
|
||||
public void setIdEmployee(int idEmployee) {
|
||||
this.idEmployee = idEmployee;
|
||||
}
|
||||
|
||||
public String getNmEmployee() {
|
||||
return nmEmployee;
|
||||
}
|
||||
|
||||
public void setNmEmployee(String nmEmployee) {
|
||||
this.nmEmployee = nmEmployee;
|
||||
}
|
||||
|
||||
public String getKnEmployee() {
|
||||
return knEmployee;
|
||||
}
|
||||
|
||||
public void setKnEmployee(String knEmployee) {
|
||||
this.knEmployee = knEmployee;
|
||||
}
|
||||
|
||||
public String getMailAddress() {
|
||||
return mailAddress;
|
||||
}
|
||||
|
||||
public void setMailAddress(String mailAddress) {
|
||||
this.mailAddress = mailAddress;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public int getIdDepartment() {
|
||||
return idDepartment;
|
||||
}
|
||||
|
||||
public void setIdDepartment(int idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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テーブル
|
||||
*/
|
||||
@@ -58,6 +58,7 @@ public class Main {
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -70,7 +71,6 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
@@ -78,7 +78,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 +88,57 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
String id = inputCheck("ID", true);
|
||||
empList = empDao.selectById(Integer.parseInt(id));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
if (empList.isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
} else {
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(
|
||||
emp.getIdEmployee() + " "
|
||||
+ emp.getNmEmployee() + " "
|
||||
+ emp.getKnEmployee() + " "
|
||||
+ emp.getMailAddress() + " "
|
||||
+ emp.getPassword() + " "
|
||||
+ emp.getIdDepartment());
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("取得件数" + empList.size());
|
||||
|
||||
for (String emp : empList) {
|
||||
System.out.println(emp);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
//社員テーブルから情報を削除する
|
||||
public static void empDelete()
|
||||
throws ClassNotFoundException, SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
String id = inputCheck("ID", true);
|
||||
|
||||
try {
|
||||
List<EmployeeDTO> empList = empDao.selectById(Integer.parseInt(id));
|
||||
|
||||
if (empList.isEmpty()) {
|
||||
System.out.println("入力されて値で情報がみつかりませんでした");
|
||||
} else {
|
||||
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
||||
System.out.println("Y/y以外の入力であればキャンセル");
|
||||
|
||||
String confirm = sc.nextLine();
|
||||
|
||||
if (confirm.equalsIgnoreCase("y")) {
|
||||
empDao.empDelete(Integer.parseInt(id));
|
||||
System.out.println("削除が完了しました");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
DbUtil.closeConnection(con);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,13 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
//例外が2個あるためカンマでつなげることができる
|
||||
public static Connection getConnection() throws SQLException {
|
||||
//データベースへの接続
|
||||
public static Connection getConnection()
|
||||
throws ClassNotFoundException, SQLException {
|
||||
Class.forName(DB_DRIVER);
|
||||
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");
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
@@ -70,14 +61,13 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
// PreparedStatement型 / DBステートメント 例外: SQLException
|
||||
public static void closeStatement( PreparedStatement stmt) throws SQLException {
|
||||
//引数のDBステートメントがnullでない場合
|
||||
if(stmt != null) {
|
||||
//そのDBステートメントをクローズする
|
||||
public static void closeStatement(PreparedStatement stmt)
|
||||
throws SQLException {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -87,10 +77,9 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
//引数のコネクションがnullでない場合
|
||||
if(con != null) {
|
||||
//そのコネクションを切断する
|
||||
public static void closeConnection(Connection con)
|
||||
throws SQLException {
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user