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>
|
||||
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.List;
|
||||
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class EmployeeDAO {
|
||||
@@ -49,8 +48,8 @@ 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(" " + "*");
|
||||
@@ -60,16 +59,8 @@ public class EmployeeDAO {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
//DBのカラムをリストに追加
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPass(rs.getString(PASSWORD));
|
||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
@@ -77,65 +68,4 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
public List<EmployeeDTO> selectByld(int id) throws SQLException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
//DBのカラムをリストに追加
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPass(rs.getString(PASSWORD));
|
||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
if (rtnList.size() == 0) {
|
||||
System.out.println("入力された値で情報が見つかりませんでした");
|
||||
}
|
||||
return rtnList;
|
||||
|
||||
}
|
||||
|
||||
public void empDelete(int id) throws SQLException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.executeUpdate();
|
||||
System.out.println("削除が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
|
||||
}
|
||||
}
|
||||
public void emplnsert (String name, String kana, String mail, String pass, int id) {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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 com.rhizome.example.dao.EmployeeDAO;
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
/**
|
||||
@@ -45,8 +44,6 @@ public class Main {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void start() throws ClassNotFoundException, SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println();
|
||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
||||
@@ -60,7 +57,6 @@ public class Main {
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -73,30 +69,6 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
private void empDelete() throws SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
try {
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
List<EmployeeDTO> empList = empDao.selectByld(id);
|
||||
if (empList.isEmpty()) {
|
||||
} else {
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("Y/y以外の入力であればキャンセル");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String str = sc.nextLine();
|
||||
if (str.equals("Y") || str.equals("y")) {
|
||||
empDao.empDelete(id);
|
||||
} else {
|
||||
System.out.println("キャンセルしました");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
@@ -104,7 +76,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:条件検索 それ以外:戻る");
|
||||
@@ -114,17 +86,11 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
System.out.println("IDを入力してください");
|
||||
int id = sc.nextInt();
|
||||
empList = empDao.selectByld(id);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp.getEmpid() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t"
|
||||
+ emp.getMail() + "\t" + emp.getPass() + "\t" + emp.getDepid());
|
||||
for (String emp : empList) {
|
||||
System.out.println(emp);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
@@ -158,7 +124,7 @@ public class Main {
|
||||
* @return
|
||||
*/
|
||||
public static String inputCheck(String titleName, boolean flg) {
|
||||
String str;
|
||||
String str = null;
|
||||
while (true) {
|
||||
System.out.println(titleName + "を入力してください");
|
||||
str = sc.nextLine();
|
||||
|
||||
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
|
||||
@@ -45,8 +45,8 @@ public class DbUtil {
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection() throws SQLException {
|
||||
//データベースへの接続
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER ,DB_PASS);
|
||||
//データベースへの接続
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
return con;
|
||||
}
|
||||
|
||||
@@ -59,12 +59,13 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||
//DBステートメントのクローズ
|
||||
if (ps != null) {
|
||||
ps.close(); //DBステートメントをクローズ
|
||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||
//nullチェック
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -75,10 +76,10 @@ public class DbUtil {
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
//コネクションの切断
|
||||
//nullチェック
|
||||
if (con != null) {
|
||||
con.close(); //コネクション切断
|
||||
}
|
||||
con.close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user