Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6493baccef | |||
| f909550b87 | |||
| df7972c662 |
@@ -1,10 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<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"/>
|
||||
|
||||
6
.project
6
.project
@@ -6,18 +6,18 @@
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
@@ -7,25 +7,30 @@
|
||||
package com.rhizome.example.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
//import com.rhizome.example.main.EmployeeDTO;
|
||||
//実行結果をDTOに格納し、リストに追加
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
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コネクション */
|
||||
//public Connection con;
|
||||
public Connection con;
|
||||
/** DBステートメント */
|
||||
//PreparedStatement stmt;
|
||||
PreparedStatement stmt;
|
||||
/** 検索結果 */
|
||||
//ResultSet rs;
|
||||
ResultSet rs;
|
||||
|
||||
/**
|
||||
* インスタンス生成時DBコネクション
|
||||
@@ -33,9 +38,9 @@ public class EmployeeDAO {
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
//public EmployeeDAO(Connection con) {
|
||||
//this.con = con;
|
||||
//}
|
||||
public EmployeeDAO(Connection con) {
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの情報を全件取得する
|
||||
@@ -46,116 +51,135 @@ public class EmployeeDAO {
|
||||
*/
|
||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
|
||||
Connection con = null;
|
||||
Statement stmt = null;
|
||||
|
||||
|
||||
try {
|
||||
// JDBCドライバの読み込み
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
try {
|
||||
|
||||
// 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に格納し、リストに追加
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
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"));
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setId(rs.getInt("id_employee"));
|
||||
dto.setName(rs.getString("nm_employee"));
|
||||
dto.setKana(rs.getString("kn_employee"));
|
||||
dto.setEmail(rs.getString("mail_address"));
|
||||
dto.setPass(rs.getString("password"));
|
||||
dto.setIdDep(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(dto);
|
||||
|
||||
rtnList.add(emp);
|
||||
}
|
||||
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//IDによる条件検索
|
||||
public java.util.List<EmployeeDTO> selectById(int id) throws SQLException, ClassNotFoundException, NumberFormatException{
|
||||
|
||||
public java.util.List<EmployeeDTO> selectById(int id)
|
||||
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
|
||||
Connection con = null;
|
||||
Statement stmt = null;
|
||||
|
||||
try {
|
||||
// JDBCドライバの読み込み
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT * FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE id_employee = ?");
|
||||
|
||||
// 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"));
|
||||
try {
|
||||
|
||||
rtnList.add(emp);
|
||||
}
|
||||
|
||||
List<EmployeeDTO> selectList = new ArrayList<>();
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
stmt.setInt(1, id);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
for (EmployeeDTO employee : rtnList) {
|
||||
if (employee.getPrimaryKeyValue().equals(id)) {
|
||||
selectList.add(employee);
|
||||
}
|
||||
}
|
||||
while (rs.next()) {
|
||||
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setId(rs.getInt("id_employee"));
|
||||
dto.setName(rs.getString("nm_employee"));
|
||||
dto.setKana(rs.getString("kn_employee"));
|
||||
dto.setEmail(rs.getString("mail_address"));
|
||||
dto.setPass(rs.getString("password"));
|
||||
dto.setIdDep(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(dto);
|
||||
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void empDelete(int id) throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE id_employee = ?");
|
||||
|
||||
try {
|
||||
this.stmt = this.con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1, id);
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
public void empInsert(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass,
|
||||
int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException {
|
||||
//List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" INSERT INTO " + TBL_NAME);
|
||||
sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) ");
|
||||
sql.append(" VALUES ");
|
||||
sql.append(" (?, ?, ?, ?, ?) ");
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
|
||||
this.stmt.setString(1, name);
|
||||
this.stmt.setString(2, kana);
|
||||
this.stmt.setString(3, mail);
|
||||
this.stmt.setString(4, pass);
|
||||
this.stmt.setInt(5, depId);
|
||||
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
public void empUpdate(int id, java.lang.String name, java.lang.String kana, java.lang.String mail,
|
||||
java.lang.String pass, int depId)
|
||||
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" UPDATE ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" SET ");
|
||||
sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? ");
|
||||
sql.append(" WHERE id_employee = ? ");
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
|
||||
this.stmt.setString(1, name);
|
||||
this.stmt.setString(2, kana);
|
||||
this.stmt.setString(3, mail);
|
||||
this.stmt.setString(4, pass);
|
||||
this.stmt.setInt(5, depId);
|
||||
this.stmt.setInt(6, id);
|
||||
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +1,60 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
//Employeeテーブルのカラム分のフィールド
|
||||
private Integer id_employee;
|
||||
private String nm_employee;
|
||||
private String kn_employee;
|
||||
private String mail_address;
|
||||
private String password;
|
||||
private Integer id_department;
|
||||
|
||||
|
||||
public Integer getId_employee() {
|
||||
return id_employee;
|
||||
}
|
||||
public void setId_employee(Integer id_employee) {
|
||||
this.id_employee = id_employee;
|
||||
}
|
||||
public String getNm_employee() {
|
||||
return nm_employee;
|
||||
}
|
||||
public void setNm_employee(String nm_employee) {
|
||||
this.nm_employee = nm_employee;
|
||||
}
|
||||
public String getKn_employee() {
|
||||
return kn_employee;
|
||||
}
|
||||
public void setKn_employee(String kn_employee) {
|
||||
this.kn_employee = kn_employee;
|
||||
}
|
||||
public String getMail_address() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String kana;
|
||||
private String email;
|
||||
private String pass;
|
||||
private Integer idDep;
|
||||
|
||||
}
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getKana() {
|
||||
return kana;
|
||||
}
|
||||
|
||||
public void setKana(String kana) {
|
||||
this.kana = kana;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return pass;
|
||||
}
|
||||
|
||||
public void setPass(String pass) {
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
public Integer getIdDep() {
|
||||
return idDep;
|
||||
}
|
||||
|
||||
public void setIdDep(Integer idDep) {
|
||||
this.idDep = idDep;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,10 +54,13 @@ public class Main {
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
empInsert();
|
||||
break;
|
||||
case "3":
|
||||
empUpdate();
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -79,7 +82,7 @@ public class Main {
|
||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
String select = sc.nextLine();
|
||||
try {
|
||||
@@ -88,23 +91,119 @@ public class Main {
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
String id = sc.nextLine();//IDを入力
|
||||
int num = Integer.valueOf(id);//String型のIDをint型に変換
|
||||
empList = empDao.selectById(num);
|
||||
String idStr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idStr);
|
||||
empList = empDao.selectById(id);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
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());
|
||||
System.out.println(
|
||||
|
||||
emp.getId() + "\t" +
|
||||
emp.getName() + "\t" +
|
||||
emp.getKana() + "\t" +
|
||||
emp.getEmail() + "\t" +
|
||||
emp.getPass() + "\t" +
|
||||
emp.getIdDep()
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection();
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
public static void empInsert()
|
||||
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||
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);
|
||||
String depIdstr = inputCheck("部署ID", true);
|
||||
|
||||
int depId = Integer.parseInt(depIdstr);
|
||||
|
||||
System.out.println("登録が完了しました");
|
||||
|
||||
empDao.empInsert(name, kana, mail, pass, depId);
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void empUpdate()
|
||||
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String idstr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idstr);
|
||||
|
||||
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||
if (checkList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
String depIdstr = inputCheck("部署ID", true);
|
||||
|
||||
int depId = Integer.parseInt(depIdstr);
|
||||
|
||||
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||
|
||||
System.out.println("更新が完了しました");
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String idStr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idStr);
|
||||
|
||||
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||
if (checkList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
String yY = sc.nextLine();
|
||||
if (!"y".equals(yY) && !"Y".equals(yY)) {
|
||||
System.out.println("削除をキャンセルしました");
|
||||
return;
|
||||
}
|
||||
empDao.empDelete(id);
|
||||
System.out.println("削除が完了しました");
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,9 +232,6 @@ public class Main {
|
||||
* @param flg
|
||||
* 数値チェック可否フラグ
|
||||
* @return
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public static String inputCheck(String titleName, boolean flg) {
|
||||
String str = null;
|
||||
@@ -155,11 +251,9 @@ public class Main {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@ package com.rhizome.example.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
@@ -45,26 +42,11 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
//DB接続用メソッドを作成する
|
||||
public static Connection getConnection() throws ClassNotFoundException {
|
||||
//データベースの接続オブジェクトを初期化
|
||||
Connection con = null;
|
||||
// JDBCドライバの読み込み
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
// Connectionの生成
|
||||
try {
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
|
||||
, "rezouser" // ユーザ
|
||||
, "Rezo_0000" // パスワード
|
||||
);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return con;
|
||||
}
|
||||
|
||||
public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
return con;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
@@ -75,14 +57,11 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement() {
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
if (stmt != null) stmt.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
public static void closeStatement(java.sql.PreparedStatement stmt) throws java.sql.SQLException {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -94,12 +73,16 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection() {
|
||||
try {
|
||||
if (con != null) con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//public static void closeConnection(Connection con) throws SQLException {
|
||||
//if (con != null) {
|
||||
//con.close();
|
||||
//}
|
||||
//
|
||||
|
||||
public static void closeConnection(java.sql.Connection con) throws java.sql.SQLException {
|
||||
if (con != null)
|
||||
con.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user