Compare commits
7 Commits
t-eguchi
...
s-sakiyama
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a56fa4eefa | ||
|
|
b4b3f35d00 | ||
|
|
a6bd282b1c | ||
|
|
1aade1c6ed | ||
|
|
e4e9c1f3ac | ||
|
|
450270610f | ||
|
|
468b80bc4a |
Binary file not shown.
@@ -61,13 +61,9 @@ 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();
|
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||||
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
|
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||||
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
|
rs.getInt(ID_DEPARTMENT));
|
||||||
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);
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@@ -76,42 +72,159 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
//社員テーブルから指定された社員IDに一致するデータを取得し、DTOに設定の上、リスト形式で戻
|
/**
|
||||||
public List<EmployeeDTO> selectById(int id_employee) throws SQLException {
|
* 社員テーブルから社員IDに合致する情報を取得する
|
||||||
List<EmployeeDTO> list = new ArrayList<>();
|
* @param id
|
||||||
String sql = "SELECT * FROM employee WHERE ID_EMPLOYEE = ?";
|
* 検索する社員ID
|
||||||
|
* @return 検索結果
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws NumberFormatException
|
||||||
|
*/
|
||||||
|
public List<EmployeeDTO> selectById(int id)
|
||||||
|
throws SQLException, ClassNotFoundException, NumberFormatException {
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
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 {
|
try {
|
||||||
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
this.stmt.setInt(1, id_employee);
|
rs = stmt.executeQuery();
|
||||||
this.rs = this.stmt.executeQuery();
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
EmployeeDTO dto = new EmployeeDTO();
|
|
||||||
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
|
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||||
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
|
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||||
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
|
rs.getInt(ID_DEPARTMENT));
|
||||||
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
|
rtnList.add(dto);
|
||||||
dto.setPASSWORD(rs.getString("PASSWORD"));
|
|
||||||
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
|
|
||||||
list.add(dto);
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
|
||||||
}
|
}
|
||||||
return list;
|
if (rtnList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
}
|
||||||
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 社員テーブルから指定された社員IDに一致するデータを削除する
|
/**
|
||||||
public void empDelete(int id_employee) throws SQLException {
|
* 社員テーブルの社員IDに合致する情報を削除する
|
||||||
String sql = "DELETE FROM employee WHERE ID_EMPLOYEE = ?";
|
*
|
||||||
|
* @param id
|
||||||
|
* 削除ID
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws NumberFormatException
|
||||||
|
*/
|
||||||
|
public void empDelete(int id)
|
||||||
|
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" DELETE ");
|
||||||
|
sql.append(" FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" WHERE ");
|
||||||
|
sql.append(" " + ID_EMPLOYEE);
|
||||||
|
sql.append(" = ");
|
||||||
|
sql.append(" " + id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.stmt = con.prepareStatement(sql);
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
this.stmt.setInt(1, id_employee);
|
stmt.executeUpdate();
|
||||||
this.stmt.executeUpdate();
|
System.out.println("削除が完了しました");
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルに指定された情報を新規登録する
|
||||||
|
*
|
||||||
|
* @param name - 社員名
|
||||||
|
* @param kana - 社員名カナ
|
||||||
|
* @param mail - メールアドレス
|
||||||
|
* @param pass - パスワード
|
||||||
|
* @param depId - 部署ID
|
||||||
|
* @throws NumberFormatException
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public void empInsert(String name, String kana, String mail, String pass, int depId)
|
||||||
|
throws NumberFormatException, SQLException, ClassNotFoundException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" INSERT ");
|
||||||
|
sql.append(" INTO ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" ( " + NM_EMPLOYEE);
|
||||||
|
sql.append(" , " + KN_EMPLOYEE);
|
||||||
|
sql.append(" , " + MAIL_ADDRESS);
|
||||||
|
sql.append(" , " + PASSWORD);
|
||||||
|
sql.append(" , " + ID_DEPARTMENT + " ) ");
|
||||||
|
sql.append(" VALUES ");
|
||||||
|
sql.append(" ( '" + name);
|
||||||
|
sql.append("' , '" + kana);
|
||||||
|
sql.append("' , '" + mail);
|
||||||
|
sql.append("' , '" + pass);
|
||||||
|
sql.append("' , " + depId + " ) ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
stmt.executeUpdate();
|
||||||
|
System.out.println("登録が完了しました");
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルの社員IDに合致する情報を更新する
|
||||||
|
*
|
||||||
|
* @param id - 社員ID
|
||||||
|
* @param name - 社員名
|
||||||
|
* @param kana - 社員名カナ
|
||||||
|
* @param mail - メールアドレス
|
||||||
|
* @param pass - パスワード
|
||||||
|
* @param depId - 部署ID
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws NumberFormatException
|
||||||
|
|
||||||
|
*/
|
||||||
|
public void empUpdate(int id, String name, String kana, String mail, String pass, int depId)
|
||||||
|
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" UPDATE ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" SET ");
|
||||||
|
sql.append(" " + NM_EMPLOYEE);
|
||||||
|
sql.append(" = '" + name + " ' ");
|
||||||
|
sql.append(" , " + KN_EMPLOYEE);
|
||||||
|
sql.append(" = '" + kana + " ' ");
|
||||||
|
sql.append(" , " + MAIL_ADDRESS);
|
||||||
|
sql.append(" = '" + mail + " ' ");
|
||||||
|
sql.append(" , " + PASSWORD);
|
||||||
|
sql.append(" = '" + pass + " ' ");
|
||||||
|
sql.append(" , " + ID_DEPARTMENT);
|
||||||
|
sql.append(" = " + depId);
|
||||||
|
sql.append(" WHERE ");
|
||||||
|
sql.append(" " + ID_EMPLOYEE);
|
||||||
|
sql.append(" = ");
|
||||||
|
sql.append(" " + id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
stmt.executeUpdate();
|
||||||
|
System.out.println("更新が完了しました");
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
79
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
79
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package com.rhizome.example.dto;
|
||||||
|
|
||||||
|
public class EmployeeDTO {
|
||||||
|
private int ID_EMPLOYEE;
|
||||||
|
private String NM_EMPLOYEE;
|
||||||
|
private String KN_EMPLOYEE;
|
||||||
|
private String MAIL_ADDRESS;
|
||||||
|
private String PASSWORD;
|
||||||
|
private int ID_DEPARTMENT;
|
||||||
|
|
||||||
|
public EmployeeDTO() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeDTO(int ID_EMPLOYEE, String NM_EMPLOYEE, String KN_EMPLOYEE, String MAIL_ADDRESS, String PASSWORD,
|
||||||
|
int ID_DEPARTMENT) {
|
||||||
|
setID_EMPLOYEE(ID_EMPLOYEE);
|
||||||
|
setNM_EMPLOYEE(NM_EMPLOYEE);
|
||||||
|
setKN_EMPLOYEE(KN_EMPLOYEE);
|
||||||
|
setMAIL_ADDRESS(MAIL_ADDRESS);
|
||||||
|
setPASSWORD(PASSWORD);
|
||||||
|
setID_DEPARTMENT(ID_DEPARTMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getID_EMPLOYEE() {
|
||||||
|
return ID_EMPLOYEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setID_EMPLOYEE(int iD_EMPLOYEE) {
|
||||||
|
ID_EMPLOYEE = iD_EMPLOYEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNM_EMPLOYEE() {
|
||||||
|
return NM_EMPLOYEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNM_EMPLOYEE(String nM_EMPLOYEE) {
|
||||||
|
NM_EMPLOYEE = nM_EMPLOYEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKN_EMPLOYEE() {
|
||||||
|
return KN_EMPLOYEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKN_EMPLOYEE(String kN_EMPLOYEE) {
|
||||||
|
KN_EMPLOYEE = kN_EMPLOYEE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMAIL_ADDRESS() {
|
||||||
|
return MAIL_ADDRESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMAIL_ADDRESS(String mAIL_ADDRESS) {
|
||||||
|
MAIL_ADDRESS = mAIL_ADDRESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPASSWORD() {
|
||||||
|
return PASSWORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPASSWORD(String pASSWORD) {
|
||||||
|
PASSWORD = pASSWORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getID_DEPARTMENT() {
|
||||||
|
return ID_DEPARTMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setID_DEPARTMENT(int iD_DEPARTMENT) {
|
||||||
|
ID_DEPARTMENT = iD_DEPARTMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showInfo() {
|
||||||
|
System.out.println(ID_EMPLOYEE + "\t" + NM_EMPLOYEE +
|
||||||
|
"\t" + KN_EMPLOYEE + "\t" + MAIL_ADDRESS +
|
||||||
|
"\t" + PASSWORD + "\t" + ID_DEPARTMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -54,11 +54,13 @@ public class Main {
|
|||||||
empSelect();
|
empSelect();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
|
empInsert();
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
|
empUpdate();
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
empDelete();
|
empDelete();
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -71,6 +73,93 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルの社員IDに合致する情報を更新する
|
||||||
|
*
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws NumberFormatException
|
||||||
|
*/
|
||||||
|
public static void empUpdate()
|
||||||
|
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
int id = Integer.parseInt(inputCheck("ID", true));
|
||||||
|
|
||||||
|
if (empDao.selectById(id).size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String name = inputCheck("社員名", false);
|
||||||
|
String kana = inputCheck("フリガナ", false);
|
||||||
|
String mail = inputCheck("メールアドレス", false);
|
||||||
|
String pass = inputCheck("パスワード", false);
|
||||||
|
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルに指定された情報を新規登録する
|
||||||
|
* @throws NumberFormatException
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public static void empInsert()
|
||||||
|
throws NumberFormatException, SQLException, ClassNotFoundException {
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
String name = inputCheck("社員名", false);
|
||||||
|
String kana = inputCheck("フリガナ", false);
|
||||||
|
String mail = inputCheck("メールアドレス", false);
|
||||||
|
String pass = inputCheck("パスワード", false);
|
||||||
|
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
empDao.empInsert(name, kana, mail, pass, depId);
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルの社員IDに合致する情報を削除する
|
||||||
|
*
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
public static void empDelete()
|
||||||
|
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
int id = Integer.parseInt(inputCheck("ID", true));
|
||||||
|
rtnList = empDao.selectById(id);
|
||||||
|
if (rtnList.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("指定されたIDの情報を削除します? Y/y\r\n"
|
||||||
|
+ "(Y/y以外の入力であればキャンセル)");
|
||||||
|
|
||||||
|
String str = inputCheck(false);
|
||||||
|
|
||||||
|
if (!str.equalsIgnoreCase("y")) {
|
||||||
|
System.out.println("キャンセルしました");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
empDao.empDelete(id);
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員テーブルから情報を検索する
|
* 社員テーブルから情報を検索する
|
||||||
*
|
*
|
||||||
@@ -89,58 +178,19 @@ public class Main {
|
|||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
String inputId = inputCheck("検索する社員ID", true);
|
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
||||||
int input = Integer.parseInt(inputId);
|
|
||||||
empList = empDao.selectById(input);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (EmployeeDTO emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
emp.showInfo();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* フラグを判定し、テーブル名を取得する
|
* フラグを判定し、テーブル名を取得する
|
||||||
*
|
*
|
||||||
@@ -190,4 +240,26 @@ public class Main {
|
|||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String inputCheck(boolean flg) {
|
||||||
|
String str = null;
|
||||||
|
while (true) {
|
||||||
|
str = sc.nextLine();
|
||||||
|
if (str.trim().length() == 0) {
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (flg) {
|
||||||
|
if (str.trim().length() > 0) {
|
||||||
|
try {
|
||||||
|
Integer.parseInt(str);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.sql.Connection;
|
|||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class DbUtil {
|
public class DbUtil {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -44,9 +45,12 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
|
||||||
Class.forName(DB_DRIVER);
|
|
||||||
|
|
||||||
|
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||||
|
if (con != null && con.isValid(1)) {
|
||||||
|
return con;
|
||||||
|
}
|
||||||
|
Class.forName(DB_DRIVER);
|
||||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,11 +63,10 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
|
||||||
if (stmt != null) {
|
|
||||||
stmt.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||||
|
if (Objects.nonNull(ps))
|
||||||
|
ps.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -76,9 +79,8 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
if (con != null) {
|
if (Objects.nonNull(con))
|
||||||
con.close();
|
con.close();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user