Compare commits
1 Commits
s-sakiyama
...
m-endo2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c04363bb7 |
8
.classpath
Normal file
8
.classpath
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<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>
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
23
.project
Normal file
23
.project
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?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>
|
||||||
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
|
||||||
@@ -61,9 +61,14 @@ 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(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
//インスタンス生成
|
||||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
rs.getInt(ID_DEPARTMENT));
|
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);
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@@ -72,159 +77,4 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 社員テーブルから社員IDに合致する情報を取得する
|
|
||||||
* @param id
|
|
||||||
* 検索する社員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 {
|
|
||||||
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
rs = stmt.executeQuery();
|
|
||||||
while (rs.next()) {
|
|
||||||
|
|
||||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
|
||||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
|
||||||
rs.getInt(ID_DEPARTMENT));
|
|
||||||
rtnList.add(dto);
|
|
||||||
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
}
|
|
||||||
if (rtnList.size() == 0) {
|
|
||||||
System.out.println("入力された値で情報がみつかりませんでした");
|
|
||||||
}
|
|
||||||
return rtnList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 社員テーブルの社員IDに合致する情報を削除する
|
|
||||||
*
|
|
||||||
* @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 {
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
|
||||||
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 {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,79 +1,61 @@
|
|||||||
package com.rhizome.example.dto;
|
package com.rhizome.example.dto;
|
||||||
|
|
||||||
public class EmployeeDTO {
|
public class EmployeeDTO {
|
||||||
private int ID_EMPLOYEE;
|
//フィールド宣言
|
||||||
private String NM_EMPLOYEE;
|
private Integer id_employee;//社員ID
|
||||||
private String KN_EMPLOYEE;
|
private String nm_employee;//社員名
|
||||||
private String MAIL_ADDRESS;
|
private String kn_employee;//社員名カナ
|
||||||
private String PASSWORD;
|
private String mail_address;//メールアドレス
|
||||||
private int ID_DEPARTMENT;
|
private String password;//パスワード
|
||||||
|
private Integer id_department;//部署ID
|
||||||
public EmployeeDTO() {
|
|
||||||
|
|
||||||
|
//getter/setter
|
||||||
|
public Integer getId_employee() {
|
||||||
|
return id_employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EmployeeDTO(int ID_EMPLOYEE, String NM_EMPLOYEE, String KN_EMPLOYEE, String MAIL_ADDRESS, String PASSWORD,
|
public void setId_employee(Integer id_employee) {
|
||||||
int ID_DEPARTMENT) {
|
this.id_employee = id_employee;
|
||||||
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() {
|
public String getNm_employee() {
|
||||||
return ID_EMPLOYEE;
|
return nm_employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setID_EMPLOYEE(int iD_EMPLOYEE) {
|
public void setNm_employee(String nm_employee) {
|
||||||
ID_EMPLOYEE = iD_EMPLOYEE;
|
this.nm_employee = nm_employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNM_EMPLOYEE() {
|
public String getKn_employee() {
|
||||||
return NM_EMPLOYEE;
|
return kn_employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNM_EMPLOYEE(String nM_EMPLOYEE) {
|
public void setKn_employee(String kn_employee) {
|
||||||
NM_EMPLOYEE = nM_EMPLOYEE;
|
this.kn_employee = kn_employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKN_EMPLOYEE() {
|
public String getMail_address() {
|
||||||
return KN_EMPLOYEE;
|
return mail_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setKN_EMPLOYEE(String kN_EMPLOYEE) {
|
public void setMail_address(String mail_address) {
|
||||||
KN_EMPLOYEE = kN_EMPLOYEE;
|
this.mail_address = mail_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMAIL_ADDRESS() {
|
public String getPassword() {
|
||||||
return MAIL_ADDRESS;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMAIL_ADDRESS(String mAIL_ADDRESS) {
|
public void setPassword(String password) {
|
||||||
MAIL_ADDRESS = mAIL_ADDRESS;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPASSWORD() {
|
public Integer getId_department() {
|
||||||
return PASSWORD;
|
return id_department;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPASSWORD(String pASSWORD) {
|
public void setId_department(Integer id_department) {
|
||||||
PASSWORD = pASSWORD;
|
this.id_department = id_department;
|
||||||
}
|
|
||||||
|
|
||||||
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,13 +54,10 @@ 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();
|
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -73,93 +70,6 @@ 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員テーブルから情報を検索する
|
* 社員テーブルから情報を検索する
|
||||||
*
|
*
|
||||||
@@ -177,14 +87,11 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
case "2":
|
|
||||||
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (EmployeeDTO emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
emp.showInfo();
|
System.out.println(emp);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
@@ -240,26 +147,4 @@ 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,7 +4,6 @@ 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 {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -45,13 +44,10 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
//データベースへの接続
|
||||||
if (con != null && con.isValid(1)) {
|
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
return con;
|
return con;
|
||||||
}
|
|
||||||
Class.forName(DB_DRIVER);
|
|
||||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -63,10 +59,11 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
//nullチェック
|
||||||
if (Objects.nonNull(ps))
|
if (stmt != null) {
|
||||||
ps.close();
|
stmt.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -79,8 +76,10 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
if (Objects.nonNull(con))
|
//nullチェック
|
||||||
|
if (con != null) {
|
||||||
con.close();
|
con.close();
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user