20261029
This commit is contained in:
@@ -7,30 +7,25 @@
|
|||||||
package com.rhizome.example.dao;
|
package com.rhizome.example.dao;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.DriverManager;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
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.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
//import com.rhizome.example.main.EmployeeDTO;
|
||||||
|
//実行結果をDTOに格納し、リストに追加
|
||||||
|
|
||||||
public class EmployeeDAO {
|
public class EmployeeDAO {
|
||||||
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";
|
|
||||||
|
|
||||||
/** DBコネクション */
|
/** DBコネクション */
|
||||||
public Connection con;
|
//public Connection con;
|
||||||
/** DBステートメント */
|
/** DBステートメント */
|
||||||
PreparedStatement stmt;
|
//PreparedStatement stmt;
|
||||||
/** 検索結果 */
|
/** 検索結果 */
|
||||||
ResultSet rs;
|
//ResultSet rs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* インスタンス生成時DBコネクション
|
* インスタンス生成時DBコネクション
|
||||||
@@ -38,9 +33,9 @@ public class EmployeeDAO {
|
|||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public EmployeeDAO(Connection con) {
|
//public EmployeeDAO(Connection con) {
|
||||||
this.con = con;
|
//this.con = con;
|
||||||
}
|
//}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員テーブルの情報を全件取得する
|
* 社員テーブルの情報を全件取得する
|
||||||
@@ -51,24 +46,116 @@ public class EmployeeDAO {
|
|||||||
*/
|
*/
|
||||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
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());
|
Connection con = null;
|
||||||
rs = stmt.executeQuery();
|
Statement stmt = null;
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
// JDBCドライバの読み込み
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
|
||||||
|
// Connectionの生成
|
||||||
|
con = DriverManager.getConnection(
|
||||||
|
"jdbc:mysql://127.0.0.1:3306/rezodb", // データベース
|
||||||
|
"rezouser", // ユーザ名
|
||||||
|
"Rezo_0000" // パスワード
|
||||||
|
);
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM employee";
|
||||||
|
stmt = con.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(sql);
|
||||||
|
|
||||||
|
// 実行結果をDTOに格納し、リストに追加
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
EmployeeDTO emdto = new EmployeeDTO();
|
EmployeeDTO emp = new EmployeeDTO();
|
||||||
emdto.set
|
|
||||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
emp.setId_employee(rs.getInt("id_employee"));
|
||||||
|
emp.setNm_employee(rs.getString("nm_employee"));
|
||||||
|
emp.setKn_employee(rs.getString("kn_employee"));
|
||||||
|
emp.setMail_address(rs.getString("mail_address"));
|
||||||
|
emp.setPassword(rs.getString("password"));
|
||||||
|
emp.setId_department(rs.getInt("id_department"));
|
||||||
|
|
||||||
|
rtnList.add(emp);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement();
|
} catch (ClassNotFoundException | SQLException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
// Statement、Connectionの切断
|
||||||
|
try {
|
||||||
|
if (stmt != null) stmt.close();
|
||||||
|
if (con != null) con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
//IDによる条件検索
|
||||||
|
public java.util.List<EmployeeDTO> selectById(int id) throws SQLException, ClassNotFoundException, NumberFormatException{
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
|
||||||
|
Connection con = null;
|
||||||
|
Statement stmt = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// JDBCドライバの読み込み
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
|
||||||
|
// Connectionの生成
|
||||||
|
con = DriverManager.getConnection(
|
||||||
|
"jdbc:mysql://127.0.0.1:3306/rezodb", // データベース
|
||||||
|
"rezouser", // ユーザ名
|
||||||
|
"Rezo_0000" // パスワード
|
||||||
|
);
|
||||||
|
|
||||||
|
String sql = "SELECT id_employee FROM employee WHERE id_employee";
|
||||||
|
stmt = con.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(sql);
|
||||||
|
|
||||||
|
// 実行結果をDTOに格納し、リストに追加
|
||||||
|
while (rs.next()) {
|
||||||
|
EmployeeDTO emp = new EmployeeDTO();
|
||||||
|
|
||||||
|
emp.setId_employee(rs.getInt("id_employee"));
|
||||||
|
emp.setNm_employee(rs.getString("nm_employee"));
|
||||||
|
emp.setKn_employee(rs.getString("kn_employee"));
|
||||||
|
emp.setMail_address(rs.getString("mail_address"));
|
||||||
|
emp.setPassword(rs.getString("password"));
|
||||||
|
emp.setId_department(rs.getInt("id_department"));
|
||||||
|
|
||||||
|
rtnList.add(emp);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<EmployeeDTO> selectList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (EmployeeDTO employee : rtnList) {
|
||||||
|
if (employee.getPrimaryKeyValue().equals(id)) {
|
||||||
|
selectList.add(employee);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return selectList;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException | SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
// Statement、Connectionの切断
|
||||||
|
try {
|
||||||
|
if (stmt != null) stmt.close();
|
||||||
|
if (con != null) con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rtnList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,34 +3,56 @@ package com.rhizome.example.dto;
|
|||||||
public class EmployeeDTO {
|
public class EmployeeDTO {
|
||||||
|
|
||||||
//Employeeテーブルのカラム分のフィールド
|
//Employeeテーブルのカラム分のフィールド
|
||||||
private static final String TBL_NAME = "employee";
|
private Integer id_employee;
|
||||||
private static final String ID_EMPLOYEE = "id_employee";
|
private String nm_employee;
|
||||||
private static final String NM_EMPLOYEE = "nm_employee";
|
private String kn_employee;
|
||||||
private static final String KN_EMPLOYEE = "kn_employee";
|
private String mail_address;
|
||||||
private static final String MAIL_ADDRESS = "mail_address";
|
private String password;
|
||||||
private static final String PASSWORD = "password";
|
private Integer id_department;
|
||||||
private static final String ID_DEPARTMENT = "id_department";
|
|
||||||
public static String getTblName() {
|
|
||||||
return TBL_NAME;
|
public Integer getId_employee() {
|
||||||
|
return id_employee;
|
||||||
}
|
}
|
||||||
public static String getIdEmployee() {
|
public void setId_employee(Integer id_employee) {
|
||||||
return ID_EMPLOYEE;
|
this.id_employee = id_employee;
|
||||||
}
|
}
|
||||||
public static String getNmEmployee() {
|
public String getNm_employee() {
|
||||||
return NM_EMPLOYEE;
|
return nm_employee;
|
||||||
}
|
}
|
||||||
public static String getKnEmployee() {
|
public void setNm_employee(String nm_employee) {
|
||||||
return KN_EMPLOYEE;
|
this.nm_employee = nm_employee;
|
||||||
}
|
}
|
||||||
public static String getMailAddress() {
|
public String getKn_employee() {
|
||||||
return MAIL_ADDRESS;
|
return kn_employee;
|
||||||
}
|
}
|
||||||
public static String getPassword() {
|
public void setKn_employee(String kn_employee) {
|
||||||
return PASSWORD;
|
this.kn_employee = kn_employee;
|
||||||
}
|
}
|
||||||
public static String getIdDepartment() {
|
public String getMail_address() {
|
||||||
return ID_DEPARTMENT;
|
return mail_address;
|
||||||
}
|
}
|
||||||
|
public void setMail_address(String mail_address) {
|
||||||
|
this.mail_address = mail_address;
|
||||||
|
}
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
public Integer getId_department() {
|
||||||
|
return id_department;
|
||||||
|
}
|
||||||
|
public void setId_department(Integer id_department) {
|
||||||
|
this.id_department = id_department;
|
||||||
|
}
|
||||||
|
public Object getPrimaryKeyValue() {
|
||||||
|
// TODO 自動生成されたメソッド・スタブ
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,9 +77,9 @@ public class Main {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||||
List<String> empList = new ArrayList<>();
|
List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO();
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||||
String select = sc.nextLine();
|
String select = sc.nextLine();
|
||||||
try {
|
try {
|
||||||
@@ -86,11 +87,21 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
String id = sc.nextLine();//IDを入力
|
||||||
|
int num = Integer.valueOf(id);//String型のIDをint型に変換
|
||||||
|
empList = empDao.selectById(num);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(emp.getId_employee() + "\t"
|
||||||
|
+ emp.getNm_employee() + "\t"
|
||||||
|
+ emp.getKn_employee() + "\t"
|
||||||
|
+ emp.getMail_address() + "\t"
|
||||||
|
+ emp.getPassword() + "\t"
|
||||||
|
+ emp.getId_department());
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection();
|
DbUtil.closeConnection();
|
||||||
@@ -122,6 +133,9 @@ public class Main {
|
|||||||
* @param flg
|
* @param flg
|
||||||
* 数値チェック可否フラグ
|
* 数値チェック可否フラグ
|
||||||
* @return
|
* @return
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws NumberFormatException
|
||||||
*/
|
*/
|
||||||
public static String inputCheck(String titleName, boolean flg) {
|
public static String inputCheck(String titleName, boolean flg) {
|
||||||
String str = null;
|
String str = null;
|
||||||
@@ -141,7 +155,9 @@ public class Main {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
|
|||||||
Reference in New Issue
Block a user