Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7cb48440b0 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/.metadata/
|
||||||
11
.project
Normal file
11
.project
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>em-jdbc</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
Binary file not shown.
6
src/.classpath
Normal file
6
src/.classpath
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="src" path=""/>
|
||||||
|
<classpathentry kind="output" path=""/>
|
||||||
|
</classpath>
|
||||||
17
src/.project
Normal file
17
src/.project
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>src</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
1
src/com/rhizome/example/dao/.gitignore
vendored
Normal file
1
src/com/rhizome/example/dao/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/EmployeeDAO.class
|
||||||
@@ -13,7 +13,6 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.rhizome.example.dto.EmployeeDTO;
|
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
public class EmployeeDAO {
|
public class EmployeeDAO {
|
||||||
@@ -49,8 +48,8 @@ public class EmployeeDAO {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
List<String> rtnList = new ArrayList<>();
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" SELECT ");
|
sql.append(" SELECT ");
|
||||||
sql.append(" " + "*");
|
sql.append(" " + "*");
|
||||||
@@ -61,14 +60,7 @@ 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 dto = new EmployeeDTO();
|
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -76,42 +68,4 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
1
src/com/rhizome/example/main/.gitignore
vendored
Normal file
1
src/com/rhizome/example/main/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/Main.class
|
||||||
@@ -7,7 +7,6 @@ import java.util.List;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
import com.rhizome.example.dao.EmployeeDAO;
|
||||||
import com.rhizome.example.dto.EmployeeDTO;
|
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,7 +57,6 @@ public class Main {
|
|||||||
case "3":
|
case "3":
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
empDelete();
|
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -78,7 +76,7 @@ public class Main {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||||
List<EmployeeDTO> empList = new ArrayList<>();
|
List<String> empList = new ArrayList<>();
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||||
@@ -88,15 +86,10 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
case "2":
|
|
||||||
String inputId = inputCheck("検索する社員ID", true);
|
|
||||||
int input = Integer.parseInt(inputId);
|
|
||||||
empList = empDao.selectById(input);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (EmployeeDTO emp : empList) {
|
for (String emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(emp);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@@ -104,43 +97,6 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* フラグを判定し、テーブル名を取得する
|
* フラグを判定し、テーブル名を取得する
|
||||||
*
|
*
|
||||||
|
|||||||
1
src/com/rhizome/example/util/.gitignore
vendored
Normal file
1
src/com/rhizome/example/util/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/DbUtil.class
|
||||||
@@ -44,10 +44,10 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
public static Connection getConnection() throws SQLException {
|
||||||
Class.forName(DB_DRIVER);
|
//データベースへの接続
|
||||||
|
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -60,10 +60,10 @@ public class DbUtil {
|
|||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||||
|
//nullチェック
|
||||||
if (stmt != null) {
|
if (stmt != null) {
|
||||||
stmt.close();
|
stmt.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -76,9 +76,10 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
|
//nullチェック
|
||||||
if (con != null) {
|
if (con != null) {
|
||||||
con.close();
|
con.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user