Commit 210e3dab authored by Sulim Ji's avatar Sulim Ji
Browse files

update homework

parent 7b3cf4a6
package Week07.Quiz;
public class Department {
private String code;
private String name;
private String location;
public Department(String code, String name, String location) {
this.code = code;
this.name = name;
this.location = location;
}
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 String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Department{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
", location='" + location + '\'' +
'}';
}
}
\ No newline at end of file
package Week07.Quiz;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DepartmentService {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public void findAll() {
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "SELECT * FROM DEPARTMENT";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
List<Department> departments = new ArrayList<>();
while (rs.next()) {
departments.add(new Department(rs.getString(1), rs.getString(2), rs.getString(3)));
}
print(departments);
} catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void print(final List<Department> departments) {
for (Department department : departments) {
System.out.println(department);
}
}
}
package Week07.Quiz;
import java.sql.Date;
public class Drama {
private String code;
private String name;
private String production;
private String opDate;
public void setOpDate(String opDate) {
this.opDate = opDate;
}
public Drama(String production){
this.production = production;
}
public Drama(String code, String name, String production){
this.code = code;
this.name = name;
this.production = production;
}
@Override
public String toString() {
return "Drama{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
", production='" + production + '\'' +
'}';
}
}
package Week07.Quiz;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DramaService {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public void findByDramaProduction(String production) {
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select drm_code, drm_name, drm_prd from drama where drm_prd = ?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, production);
rs = pstmt.executeQuery();
List<Drama> dramas = new ArrayList<>();
while (rs.next()) {
dramas.add(new Drama(rs.getString(1), rs.getString(2), rs.getString(3)));
}
print(dramas);
} catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void findByDramaBroadcast() {
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select drm_code, drm_name, drm_prd from drama where drm_brd in('KBC','SBC')";
pstmt = conn.prepareStatement(query);
rs=pstmt.executeQuery();
List<Drama> dramas = new ArrayList<>();
while (rs.next()) {
dramas.add(new Drama(rs.getString(1), rs.getString(2), rs.getString(3)));
}
print(dramas);
} catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void findDramaProduction(){
try{
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select distinct drm_code, drm_name, drm_prd from drama";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
List<Drama> dramas = new ArrayList<>();
while(rs.next()){
dramas.add(new Drama(rs.getString(1), rs.getString(2), rs.getString(3)));
}
print(dramas);
}catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void dramaName(){
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select drm_name from drama where drm_opdate is null";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println("드라마 이름 : " + rs.getString(1));
}
}catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void updateopDate (String opDate){
try{
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "update drama set drm_opdate = ? where drm_opdate is null";
pstmt = conn.prepareStatement(query);
pstmt.setDate(1, Date.valueOf(opDate));
int retValue = pstmt.executeUpdate();
conn.commit();
System.out.println(retValue + "건의 사항이 처리되었습니다.");
}catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void print(final List<Drama> dramas){
for(Drama drama : dramas){
System.out.println(drama);
}
}
}
package Week07.Quiz;
public class Employee {
private String code;
private String name;
private String manager;
private int salary;
private int sum;
private double avg;
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 String getManager(){
return manager;
}
public void setManager(String manager){
this.manager = manager;
}
public int getSalary(){
return salary;
}
public void setSalary(int salary){
this.salary = salary;
}
public Employee(int sum, double avg){
this.sum = sum;
this.avg = avg;
}
public Employee(String code, String name, String mgt, int sal){
this.code = code;
this.name = name;
this.manager = mgt;
this.salary = sal;
}
@Override
public String toString() {
return "Employee{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
", manager='" + manager + '\'' +
", salary=" + salary +
'}';
}
}
package Week07.Quiz;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class EmployeeService {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public void findAll() {
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select * from employee";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
List<Employee> employees = new ArrayList<>();
while (rs.next()) {
employees.add(new Employee(rs.getString(1), rs.getString(2), rs.getString(3), rs.getInt(4)));
}
print(employees);
} catch (SQLException sqex) {
System.out.println("SQLExeption : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void salaryOfSumAvg() {
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select sum(e.emp_sal), avg(e.emp_sal) from employee e";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
List<Employee> employees = new LinkedList<>();
while (rs.next()) {
System.out.println(rs.getInt(1) + " || " + rs.getDouble(2));
}
} catch (SQLException sqex) {
System.out.println("SQLException : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void nameAndManagement() {
try {
conn = PostgresqlAccess.getConnection();
conn.setAutoCommit(false);
String query = "select e1.emp_name as \"사원\", e2.emp_name as \"관리자\" " +
"from employee e1 left outer join employee e2 on e1.emp_mgt = e2.emp_code";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("Drama-{name='" + rs.getString(1) + "', name='" + rs.getString(2) + "'}");
}
} catch (SQLException sqex) {
System.out.println("SQLException : " + sqex.getMessage());
System.out.println("SQLState : " + sqex.getSQLState());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void findNameSal() {
try {
conn = PostgresqlAccess.getConnection();