段階2完了
This commit is contained in:
@@ -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,8 +49,8 @@ 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(" " + "*");
|
||||||
@@ -59,8 +60,16 @@ public class EmployeeDAO {
|
|||||||
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
|
//DBのカラムをリストに追加
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
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 {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,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:条件検索 それ以外:戻る");
|
||||||
@@ -89,8 +90,9 @@ public class Main {
|
|||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(emp.getEmpid() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t"
|
||||||
|
+ emp.getMail() + "\t" + emp.getPass() + "\t" + emp.getDepid());
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
|||||||
@@ -44,21 +44,9 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
public static Connection getConnection() {
|
public static Connection getConnection() throws SQLException {
|
||||||
try {
|
//データベースへの接続
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
con = DriverManager.getConnection(DB_URL, DB_USER ,DB_PASS);
|
||||||
con = DriverManager.getConnection(
|
|
||||||
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
|
|
||||||
, "rezouser" // ユーザー名
|
|
||||||
, "Rezo_0000" // パスワード
|
|
||||||
);
|
|
||||||
// forName()で例外発生
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
// getConnection()で例外発生
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return con;
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,14 +59,10 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
public static void closeStatement(PreparedStatement ps) {
|
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||||
|
//DBステートメントのクローズ
|
||||||
if (ps != null) {
|
if (ps != null) {
|
||||||
try {
|
|
||||||
ps.close(); //DBステートメントをクローズ
|
ps.close(); //DBステートメントをクローズ
|
||||||
//例外処理
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
@@ -90,13 +74,11 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
|
//コネクションの切断
|
||||||
if (con != null) {
|
if (con != null) {
|
||||||
try {
|
|
||||||
con.close(); //コネクション切断
|
con.close(); //コネクション切断
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user