Compare commits
3 Commits
a-takayama
...
y-ogawa
| Author | SHA1 | Date | |
|---|---|---|---|
| d8d9f1d9b7 | |||
| 65ff97be06 | |||
| bb5df7baad |
6
.project
6
.project
@@ -6,18 +6,18 @@
|
|||||||
</projects>
|
</projects>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
public class EmployeeDAO {
|
public class EmployeeDAO {
|
||||||
@@ -48,25 +49,59 @@ public class EmployeeDAO {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||||
List<String> rtnList = new ArrayList<>();
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" SELECT ");
|
sql.append(" SELECT ");
|
||||||
sql.append(" " + "*");
|
sql.append(" " + "*");
|
||||||
sql.append(" FROM ");
|
sql.append(" FROM ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
// EmployeeDAO dao = new EmployeeDAO(con);
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
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);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closestatement(this.stmt);
|
||||||
}
|
}
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<EmployeeDTO> selectById(int id_employee) throws SQLException, ClassNotFoundException {
|
||||||
|
List<EmployeeDTO> rtnList2 = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" SELECT ");
|
||||||
|
sql.append(" " + "*");
|
||||||
|
sql.append(" FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" WHERE " + ID_EMPLOYEE+ "=?");
|
||||||
|
try {
|
||||||
|
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
while (rs.next()) {
|
||||||
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
|
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"));
|
||||||
|
rtnList2.add(dto);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
DbUtil.closestatement(this.stmt);
|
||||||
|
}
|
||||||
|
return rtnList2;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
69
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
69
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package com.rhizome.example.dto;
|
||||||
|
|
||||||
|
public class EmployeeDTO {
|
||||||
|
|
||||||
|
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 String toString() {
|
||||||
|
return id_employee + "\t"
|
||||||
|
+ nm_employee + "\t"
|
||||||
|
+ kn_employee + "\t"
|
||||||
|
+ mail_address + "\t"
|
||||||
|
+ password + "\t"
|
||||||
|
+ id_department;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import java.util.List;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
import com.rhizome.example.dao.EmployeeDAO;
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,7 +16,6 @@ import com.rhizome.example.util.DbUtil;
|
|||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
static Scanner sc = new Scanner(System.in);
|
static Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル
|
* テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル
|
||||||
*/
|
*/
|
||||||
@@ -70,7 +70,6 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員テーブルから情報を検索する
|
* 社員テーブルから情報を検索する
|
||||||
*
|
*
|
||||||
@@ -78,7 +77,7 @@ public class Main {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||||
List<String> empList = new ArrayList<>();
|
List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||||
@@ -88,17 +87,19 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
System.out.println("IDを入力してください");
|
||||||
|
|
||||||
|
empList = empDao.selectById(tblFlg);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println("取得件数" + empList.size());
|
|
||||||
|
|
||||||
for (String emp : empList) {
|
|
||||||
System.out.println(emp);
|
System.out.println(emp);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ public class DbUtil {
|
|||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
private static final String DB_HOST = "127.0.0.1";
|
private static final String DB_HOST = "127.0.0.1";
|
||||||
private static final String DB_NAME = "rezodb";
|
private static final String DB_NAME = "rezodb";
|
||||||
private static final String DB_USER = "rezouser1";
|
private static final String DB_USER = "rezouser";
|
||||||
private static final String DB_PASS = "Rezo_0001";
|
private static final String DB_PASS = "Rezo_0000";
|
||||||
|
|
||||||
// 定数定義(PostgreSQL用)
|
// 定数定義(PostgreSQL用)
|
||||||
// private static final String DBMS = "postgresql";
|
// private static final String DBMS = "postgresql";
|
||||||
@@ -44,22 +44,11 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
//例外が2個あるためカンマでつなげることができる
|
|
||||||
public static Connection getConnection() throws SQLException {
|
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;
|
return con;
|
||||||
}
|
}
|
||||||
// try {
|
|
||||||
// Class.forName("com.mysql.cj.jdbc.Driver");
|
|
||||||
// } catch (ClassNotFoundException e) {
|
|
||||||
// throw new SQLException("JDBCドライバが見つかりません", e);
|
|
||||||
// }
|
|
||||||
// //JDBCドライバの読み込み
|
|
||||||
// return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/rezodb"
|
|
||||||
// , "rezouser1"
|
|
||||||
// , "Rezo_0001");
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -70,14 +59,14 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
// PreparedStatement型 / DBステートメント 例外: SQLException
|
|
||||||
public static void closeStatement( PreparedStatement stmt) throws SQLException {
|
public static void closestatement(PreparedStatement stmt) throws SQLException {
|
||||||
//引数のDBステートメントがnullでない場合
|
if (!stmt.equals(null)) {
|
||||||
if(stmt != null) {
|
//DBステートメントを閉じる
|
||||||
//そのDBステートメントをクローズする
|
|
||||||
stmt.close();
|
stmt.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
*
|
*
|
||||||
@@ -88,11 +77,8 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
//引数のコネクションがnullでない場合
|
if (!con.equals(null)) {
|
||||||
if(con != null) {
|
|
||||||
//そのコネクションを切断する
|
|
||||||
con.close();
|
con.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
トランザクション・メディア・ネットワークス_小川悠里_em-jdbc.zip
Normal file
BIN
トランザクション・メディア・ネットワークス_小川悠里_em-jdbc.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user