| Index ソフト・ハード Java | Sample |
|
BusinessLogic ・EmployeeDAOを使った業務処理
package sample;
import java.sql.SQLException;
import java.util.List;
public class BusinessLogic {
public static void main(String[] args) {
EmployeeDAO dao = new EmployeeDAO();
try {
Employee employee = new Employee();
employee.setCode("社員no");
employee.setName("名前");
employee.setAge(30);
employee.setSection("所属");
int count = dao.insert(employee);
System.out.println(count + "件のデータを登録しました。");
System.out.println("*** すべての従業員情報を表示 ***");
List<Employee> employeeList = dao.selectAll();
for (Employee tempEmp : employeeList) {
String code = tempEmp.getCode();
String name = tempEmp.getName();
int age = tempEmp.getAge();
String section = tempEmp.getSection();
System.out.print(code + "\t| ");
System.out.print(name + "\t| ");
System.out.print(age + "\t| ");
System.out.print(section + "\n");
}
} catch (SQLException e) {
System.out.println("処理結果:異常が発生しました。");
e.printStackTrace();
}
}
}
ConnectionManager ・
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private final static String URL = "jdbc:mysql://localhost:3306/sampledb?useSSL=false";
private final static String USER = "userU";
private final static String PASSWORD = "userP";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
Employee ・m_employeeのDTO
package sample;
public class Employee {
private String code;
private String name;
private int age;
private String section;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
}
EmployeeDAO ・m_employeeテーブルのDAO
package sample;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class EmployeeDAO {
public int insert(Employee employee) throws SQLException {
int count = 0;
try (Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt
= con.prepareStatement("INSERT INTO m_employee VALUES(?, ?, ?, ?)")) {
String code = employee.getCode();
String name = employee.getName();
int age = employee.getAge();
String section = employee.getSection();
pstmt.setString(1, code);
pstmt.setString(2, name);
pstmt.setInt(3, age);
pstmt.setString(4, section);
count = pstmt.executeUpdate();
}
return count;
}
public List<Employee> selectAll() throws SQLException {
List<Employee> employeeList = new ArrayList<Employee>();
try (Connection con = ConnectionManager.getConnection();
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("SELECT * FROM m_employee")) {
while (res.next()) {
String code = res.getString("code");
String name = res.getString("name");
int age = res.getInt("age");
String section = res.getString("section");
Employee employee = new Employee();
employee.setCode(code);
employee.setName(name);
employee.setAge(age);
employee.setSection(section);
employeeList.add(employee);
}
}
return employeeList;
}
public String getName(String code) throws SQLException {
String name = null; //氏名
try (Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt
= con.prepareStatement("SELECT name FROM m_employee WHERE code = ?")) {
pstmt.setString(1, code);
ResultSet res = pstmt.executeQuery();
if (res.next()) {
name = res.getString("name");
}
}
return name;
}
public List<String> selectName(String section) throws SQLException {
List<String> nameList = new ArrayList<String>();
try (Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt
= con.prepareStatement("SELECT name FROM m_employee WHERE section = ?")) {
pstmt.setString(1, section);// プレースホルダへの値の設定
ResultSet res = pstmt.executeQuery();
while (res.next()) {
nameList.add(res.getString("name"));
}
}
return nameList;
}
public int delete(String code) throws SQLException {
int count = 0; //処理件数
try (Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt
= con.prepareStatement("DELETE FROM m_employee WHERE code = ?")) {
pstmt.setString(1, code);
count = pstmt.executeUpdate();
}
return count;
}
}
|
| All Rights Reserved. Copyright (C) ITCL | |