Compare commits
2 Commits
fa98c66334
...
hirayama
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cc08bdf7a | |||
| 5210508a78 |
@@ -1,7 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path=""/>
|
||||
<classpathentry kind="lib" path="mysql-connector-java-8.0.23.jar"/>
|
||||
<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>
|
||||
|
||||
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
|
||||
23
bin/.project
23
bin/.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.wst.validation.validationbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
BIN
bin/src/com/rhizome/example/dao/EmployeeDAO.class
Normal file
BIN
bin/src/com/rhizome/example/dao/EmployeeDAO.class
Normal file
Binary file not shown.
BIN
bin/src/com/rhizome/example/main/Main.class
Normal file
BIN
bin/src/com/rhizome/example/main/Main.class
Normal file
Binary file not shown.
BIN
bin/src/com/rhizome/example/util/DbUtil.class
Normal file
BIN
bin/src/com/rhizome/example/util/DbUtil.class
Normal file
Binary file not shown.
BIN
bin/実行例/DAO_Simple.jar
Normal file
BIN
bin/実行例/DAO_Simple.jar
Normal file
Binary file not shown.
2
bin/実行例/このファイルを実行してください.bat
Normal file
2
bin/実行例/このファイルを実行してください.bat
Normal file
@@ -0,0 +1,2 @@
|
||||
java -jar DAO_Simple.jar
|
||||
pause
|
||||
Binary file not shown.
@@ -1,8 +0,0 @@
|
||||
package snippet;
|
||||
|
||||
public class Snippet {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
package src.com.rhizome.example.dao;
|
||||
/*
|
||||
* EmployeeDAO.java
|
||||
* 作成 IT-College 2020
|
||||
*------------------------------------------------------------
|
||||
* Copyright(c) Rhizome Inc. All Rights Reserved.
|
||||
*/
|
||||
package com.rhizome.example.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -7,8 +13,8 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import src.com.rhizome.example.dto.EmployeeDTO;
|
||||
import src.com.rhizome.example.util.DbUtil;
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class EmployeeDAO {
|
||||
private static final String TBL_NAME = "employee";
|
||||
@@ -21,7 +27,7 @@ public class EmployeeDAO {
|
||||
|
||||
/** DBコネクション */
|
||||
public Connection con;
|
||||
/** DBステートメント */
|
||||
/** DBステートPreparedStatement stmtメント */
|
||||
PreparedStatement stmt;
|
||||
/** 検索結果 */
|
||||
ResultSet rs;
|
||||
@@ -55,129 +61,25 @@ public class EmployeeDAO {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto =
|
||||
new EmployeeDTO();
|
||||
|
||||
dto.setIdEmployee(
|
||||
rs.getInt("ID_EMPLOYEE"));
|
||||
//インスタンス生成
|
||||
EmployeeDTO emp = new EmployeeDTO();
|
||||
|
||||
dto.setNmEmployee(
|
||||
rs.getString("NM_EMPLOYEE"));
|
||||
emp.setId(rs.getInt("id_employee"));
|
||||
emp.setName(rs.getString("nm_employee"));
|
||||
emp.setKana(rs.getString("kn_employee"));
|
||||
emp.setEmail(rs.getString("mail_address"));
|
||||
emp.setPass(rs.getString("password"));
|
||||
emp.setIdDep(rs.getInt("id_department"));
|
||||
|
||||
dto.setKnEmployee(
|
||||
rs.getString("KN_EMPLOYEE"));
|
||||
rtnList.add(emp);
|
||||
|
||||
dto.setMailEmployee(
|
||||
rs.getString("MAIL_ADDRESS"));
|
||||
|
||||
dto.setPassEmployee(
|
||||
rs.getString("PASSWORD"));
|
||||
|
||||
dto.setIdDepartment(
|
||||
rs.getInt("ID_DEPARTMENT"));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
public List<EmployeeDTO> selectById(int employeeId) throws SQLException{
|
||||
List<EmployeeDTO>rtnList = new ArrayList<>();
|
||||
|
||||
java.lang.String sql =
|
||||
"SELECT * FROM employee WHERE id_employee = ?";
|
||||
try {
|
||||
stmt = con.prepareStatement(sql);
|
||||
stmt.setInt(1, employeeId);
|
||||
|
||||
rs = 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.setMailEmployee(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPassEmployee(rs.getString(PASSWORD));
|
||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
return rtnList;
|
||||
}
|
||||
public int empDelete (int employeeId)throws SQLException{
|
||||
java.lang.String sql =
|
||||
"DELETE FROM employee WHERE id_employee = ?";
|
||||
try {
|
||||
stmt = con.prepareStatement(sql);
|
||||
stmt.setInt(1, employeeId);
|
||||
|
||||
return stmt.executeUpdate();
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
|
||||
}
|
||||
}
|
||||
public int empInsert(String employeeName,String employeeKn,String employeeMail
|
||||
,String employeePass, int departmentId)throws SQLException{
|
||||
java.lang.String sql =
|
||||
"INSERT INTO employee " +
|
||||
"(nm_employee, kn_employee, mail_address, password, id_department) " +
|
||||
"VALUES (?, ?, ?, ?, ?)";
|
||||
try {
|
||||
stmt = con.prepareStatement(sql);
|
||||
stmt.setString(1, employeeName);
|
||||
stmt.setString(2, employeeKn);
|
||||
stmt.setString(3, employeeMail);
|
||||
stmt.setString(4, employeePass);
|
||||
stmt.setInt(5, departmentId);
|
||||
|
||||
return stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public int empUpdate(int employeeId,String employeeName,String employeeKn,String employeeMail
|
||||
,String employeePass, int departmentId)throws SQLException{
|
||||
java.lang.String sql =
|
||||
"UPDATE employee " +
|
||||
"SET nm_employee = ?, " +
|
||||
"kn_employee = ?, " +
|
||||
"mail_address = ?, " +
|
||||
"password = ?, " +
|
||||
"id_department = ? " +
|
||||
"WHERE id_employee = ?";
|
||||
|
||||
try {
|
||||
|
||||
stmt = con.prepareStatement(sql);
|
||||
|
||||
stmt.setString(1, employeeName);
|
||||
stmt.setString(2, employeeKn);
|
||||
stmt.setString(3, employeeMail);
|
||||
stmt.setString(4, employeePass);
|
||||
stmt.setInt(5, departmentId);
|
||||
stmt.setInt(6, employeeId);
|
||||
|
||||
return stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +1,50 @@
|
||||
package src.com.rhizome.example.dto;
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
//フィールド
|
||||
public class EmployeeDTO {
|
||||
private int idEmployee;
|
||||
private String nmEmployee;
|
||||
private String knEmployee;
|
||||
private String mailEmployee;
|
||||
private String passEmployee;
|
||||
private int idDepartment;
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String kana;
|
||||
private String email;
|
||||
private String pass;
|
||||
private Integer idDep;
|
||||
|
||||
|
||||
public int getIdEmployee() {
|
||||
return idEmployee;
|
||||
//ゲッター・セッター
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
public void setIdEmployee(int idEmployee) {
|
||||
this.idEmployee = idEmployee;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getNmEmployee() {
|
||||
return nmEmployee;
|
||||
public String getKana() {
|
||||
return kana;
|
||||
}
|
||||
public void setNmEmployee(String nmEmployee) {
|
||||
this.nmEmployee = nmEmployee;
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
public String getKnEmployee() {
|
||||
return knEmployee;
|
||||
public String getPass() {
|
||||
return pass;
|
||||
}
|
||||
public void setKnEmployee(String knEmployee) {
|
||||
this.knEmployee = knEmployee;
|
||||
public Integer getIdDep() {
|
||||
return idDep;
|
||||
}
|
||||
public String getMailEmployee() {
|
||||
return mailEmployee;
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setMailEmployee(String mailEmployee) {
|
||||
this.mailEmployee = mailEmployee;
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getPassEmployee() {
|
||||
return passEmployee;
|
||||
public void setKana(String kana) {
|
||||
this.kana = kana;
|
||||
}
|
||||
public void setPassEmployee(String passEmployee) {
|
||||
this.passEmployee = passEmployee;
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
public int getIdDepartment() {
|
||||
return idDepartment;
|
||||
public void setPass(String pass) {
|
||||
this.pass = pass;
|
||||
}
|
||||
public void setIdDepartment(int idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
public void setIdDep(Integer idDep) {
|
||||
this.idDep = idDep;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package src.com.rhizome.example.main;
|
||||
package com.rhizome.example.main;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@@ -6,9 +6,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import src.com.rhizome.example.dao.EmployeeDAO;
|
||||
import src.com.rhizome.example.dto.EmployeeDTO;
|
||||
import src.com.rhizome.example.util.DbUtil;
|
||||
import com.rhizome.example.dao.EmployeeDAO;
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
/**
|
||||
* @author IT-College
|
||||
@@ -54,13 +54,10 @@ public class Main {
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
empInsert();
|
||||
break;
|
||||
case "3":
|
||||
empUpdate();
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -90,35 +87,15 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
empList = empDao.selectById(id);
|
||||
break;
|
||||
default:
|
||||
|
||||
return;
|
||||
}
|
||||
if(empList.isEmpty()){
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
}else {
|
||||
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(
|
||||
emp.getIdEmployee() + "\t"
|
||||
+ emp.getNmEmployee() + "\t"
|
||||
+ emp.getKnEmployee() + "\t"
|
||||
+ emp.getMailEmployee() + "\t"
|
||||
+ emp.getPassEmployee() + "\t"
|
||||
+ emp.getIdDepartment() + "\t");
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp);
|
||||
}
|
||||
}finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,100 +147,4 @@ public class Main {
|
||||
}
|
||||
return str;
|
||||
}
|
||||
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
|
||||
int count = empDao.empDelete(id);
|
||||
|
||||
if (count == 0) {
|
||||
System.out.println("入力されたIDの情報は見つかりませんでした。");
|
||||
} else {
|
||||
System.out.println("削除しました。");
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
public static void empInsert() throws ClassNotFoundException, SQLException {
|
||||
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String name= inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
|
||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||
|
||||
int count = empDao.empInsert(
|
||||
name,
|
||||
kana,
|
||||
mail,
|
||||
pass,
|
||||
depId);
|
||||
|
||||
if (count > 0) {
|
||||
System.out.println("登録が完了しました");
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
public static void empUpdate() throws ClassNotFoundException, SQLException {
|
||||
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
String strId = inputCheck("ID", true);
|
||||
|
||||
int id = Integer.parseInt(strId);
|
||||
try {
|
||||
|
||||
|
||||
String name =
|
||||
inputCheck("社員名", false);
|
||||
|
||||
String kana =
|
||||
inputCheck("フリガナ", false);
|
||||
|
||||
String mail =
|
||||
inputCheck("メールアドレス", false);
|
||||
|
||||
String pass =
|
||||
inputCheck("パスワード", false);
|
||||
|
||||
int depId =
|
||||
Integer.parseInt(inputCheck("部署ID", true));
|
||||
|
||||
|
||||
int count = empDao.empUpdate(
|
||||
id,
|
||||
name,
|
||||
kana,
|
||||
mail,
|
||||
pass,
|
||||
depId);
|
||||
|
||||
if (count > 0) {
|
||||
System.out.println("更新が完了しました");
|
||||
} else {
|
||||
System.out.println("入力されたIDの情報は見つかりませんでした。");
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package src.com.rhizome.example.util;
|
||||
package com.rhizome.example.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
@@ -44,25 +44,25 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection()
|
||||
throws ClassNotFoundException, SQLException{
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
|
||||
if(con != null && !con.isClosed()) {
|
||||
return con;
|
||||
}
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
con = DriverManager.getConnection(
|
||||
DB_URL,
|
||||
DB_USER,
|
||||
DB_PASS);
|
||||
|
||||
return con;
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
|
||||
, "rezouser" // ユーザ
|
||||
, "Rezo_0000" // パスワード
|
||||
);
|
||||
}catch (ClassNotFoundException e){
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return con;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -72,12 +72,15 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(
|
||||
PreparedStatement stmt) throws SQLException{
|
||||
if(stmt !=null)
|
||||
stmt.close();
|
||||
|
||||
}
|
||||
public static void closeStatement(PreparedStatement stmt){
|
||||
try {
|
||||
if (stmt != null) stmt.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
@@ -88,9 +91,12 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con)
|
||||
throws SQLException{
|
||||
if(con != null)
|
||||
con.close();
|
||||
}
|
||||
public static void closeConnection(Connection con) {
|
||||
try {
|
||||
if (con != null) con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user