Compare commits
1 Commits
t-eguchi
...
a-takayama
| Author | SHA1 | Date | |
|---|---|---|---|
| 9650f00f45 |
8
.classpath
Normal file
8
.classpath
Normal file
@@ -0,0 +1,8 @@
|
||||
<?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
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin/
|
||||
23
.project
Normal file
23
.project
Normal 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.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>
|
||||
7
.settings/.jsdtscope
Normal file
7
.settings/.jsdtscope
Normal 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>
|
||||
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
@@ -0,0 +1 @@
|
||||
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
|
||||
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
@@ -0,0 +1 @@
|
||||
Global
|
||||
Binary file not shown.
@@ -13,7 +13,6 @@ 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 {
|
||||
@@ -49,26 +48,20 @@ public class EmployeeDAO {
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<String> 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()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
|
||||
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
|
||||
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
|
||||
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
|
||||
dto.setPASSWORD(rs.getString("PASSWORD"));
|
||||
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
|
||||
rtnList.add(dto);
|
||||
// EmployeeDAO dao = new EmployeeDAO(con);
|
||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
@@ -76,42 +69,4 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得し、DTOに設定の上、リスト形式で戻
|
||||
public List<EmployeeDTO> selectById(int id_employee) throws SQLException {
|
||||
List<EmployeeDTO> list = new ArrayList<>();
|
||||
String sql = "SELECT * FROM employee WHERE ID_EMPLOYEE = ?";
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1, id_employee);
|
||||
this.rs = this.stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
|
||||
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
|
||||
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
|
||||
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
|
||||
dto.setPASSWORD(rs.getString("PASSWORD"));
|
||||
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
|
||||
list.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// 社員テーブルから指定された社員IDに一致するデータを削除する
|
||||
public void empDelete(int id_employee) throws SQLException {
|
||||
String sql = "DELETE FROM employee WHERE ID_EMPLOYEE = ?";
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql);
|
||||
this.stmt.setInt(1, id_employee);
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -16,6 +15,7 @@ import com.rhizome.example.util.DbUtil;
|
||||
*/
|
||||
public class Main {
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
|
||||
/**
|
||||
* テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル
|
||||
*/
|
||||
@@ -58,7 +58,6 @@ public class Main {
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -71,6 +70,7 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
@@ -78,7 +78,7 @@ public class Main {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
List<String> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
@@ -88,56 +88,17 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
String inputId = inputCheck("検索する社員ID", true);
|
||||
int input = Integer.parseInt(inputId);
|
||||
empList = empDao.selectById(input);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
|
||||
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);
|
||||
try {
|
||||
System.out.println("IDを入力してください");
|
||||
String input = sc.nextLine();
|
||||
// 空文字が入力された場合
|
||||
if (input.strip().isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
int inputId = Integer.parseInt(input.strip());
|
||||
List<EmployeeDTO> empList = empDao.selectById(inputId);
|
||||
// 検索結果が存在しないIDの場合
|
||||
if (empList.isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
String confilm = sc.nextLine();
|
||||
// 検索結果が存在した場合
|
||||
if ("Y".equalsIgnoreCase(confilm)) {
|
||||
empDao.empDelete(inputId);
|
||||
System.out.println("社員ID" + input + "のデータを削除しました");
|
||||
} else {
|
||||
System.out.println("削除をキャンセルしました");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
DbUtil.closeConnection(empDao.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 = "rezouser";
|
||||
private static final String DB_PASS = "Rezo_0000";
|
||||
private static final String DB_USER = "rezouser1";
|
||||
private static final String DB_PASS = "Rezo_0001";
|
||||
|
||||
// 定数定義(PostgreSQL用)
|
||||
// private static final String DBMS = "postgresql";
|
||||
@@ -44,11 +44,22 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||
Class.forName(DB_DRIVER);
|
||||
|
||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
//例外が2個あるためカンマでつなげることができる
|
||||
public static Connection getConnection() throws SQLException {
|
||||
//データベースへの接続
|
||||
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 以下の仕様でメソッドを作成してください。
|
||||
@@ -59,13 +70,14 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||
if (stmt != null) {
|
||||
// PreparedStatement型 / DBステートメント 例外: SQLException
|
||||
public static void closeStatement( PreparedStatement stmt) throws SQLException {
|
||||
//引数のDBステートメントがnullでない場合
|
||||
if(stmt != null) {
|
||||
//そのDBステートメントをクローズする
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -76,7 +88,9 @@ public class DbUtil {
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
if (con != null) {
|
||||
//引数のコネクションがnullでない場合
|
||||
if(con != null) {
|
||||
//そのコネクションを切断する
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user