Commit 929a48f5 authored by Aram Goo's avatar Aram Goo
Browse files

add Quiz

parent 1954070c
package io.wisoft.jdbc.quiz;
public class Department {
private String code;
private String name;
private String location;
public Department() {
}
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 "[부서코드] " + code + "\t " +
" [부서이름] " + name + "\t" +
" [부서위치] " + location + '\n' ;
}
}
package io.wisoft.jdbc.quiz;
import java.sql.*;
import java.util.*;
public class DepartmentService {
public void departmentFindAll() {
String query = "SELECT * FROM DEPARTMENT";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Department> departments = new ArrayList<>();
while (rs.next()) {
departments.add(new Department(rs.getString(1), rs.getString(2), rs.getString(3)));
}
printDepartment(departments);
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
private void printSQLException(final SQLException e) {
System.out.format("SQLException: %s, SQLException: %s", e.getMessage(), e.getSQLState());
}
private void printDepartment(final List<Department> departments) {
for (Department department : departments) {
System.out.print(department); //toString 생략
}
}
}
package io.wisoft.jdbc.quiz;
public class Drama {
private String code;
private String name;
private String product;
private String broadcast;
public Drama(String name) {
this.name = name;
}
public Drama(String code, String name) {
this.code = code;
this.name = name;
}
public Drama(String code, String name, String product, String broadcast) {
this.code = code;
this.name = name;
this.product = product;
this.broadcast = broadcast;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public String getProduct() {
return product;
}
public String getBroadcast() {
return broadcast;
}
@Override
public String toString() {
return "[코드] " + code + "\t" +
" [이름] " + name + "\t" +
" [제작사] " + product + "\t" +
" [방영사] " + broadcast;
}
}
package io.wisoft.jdbc.quiz;
import java.sql.*;
import java.sql.Date;
import java.util.*;
public class DramaService {
public void dramaFindCodeAndName() {
String query = "SELECT DRM_CODE, DRM_NAME FROM DRAMA WHERE DRM_PRD='HNU-E'";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Drama> dramaList = new ArrayList<>();
while (rs.next()) {
dramaList.add(new Drama(rs.getString(1), rs.getString(2)));
}
printDrama(dramaList);
} catch(SQLException sqex) {
printSQLException(sqex);
}
}
public void dramaProductFindKBCAndSBC() {
String query = "SELECT * FROM DRAMA WHERE DRM_BRD in ('SBC', 'KBC')";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Drama> dramaList = new ArrayList<>();
while (rs.next()) {
dramaList.add(new Drama(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
printDramaAll(dramaList);
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void dramaFindName() {
String query = "SELECT DRM_NAME FROM DRAMA WHERE DRM_OPDATE IS NULL";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Drama> dramaList = new ArrayList<>();
while (rs.next()) {
dramaList.add(new Drama(rs.getString(1)));
System.out.println(rs.getString(1));
}
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void dramaFindProduct() {
String query = "select distinct drm_prd from drama";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Drama> dramaList = new ArrayList<>();
while (rs.next()){
dramaList.add(new Drama(rs.getString(1)));
System.out.println("[제작사] " + rs.getString(1));
}
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void dramaUpdateOpdate() {
String query = "UPDATE DRAMA SET DRM_OPDATE = ? WHERE DRM_OPDATE IS NULL";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setDate(1, Date.valueOf("2013-05-01"));
int retValue = pstmt.executeUpdate();
System.out.println(retValue + "건의 사항이 처리되었습니다.");
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
private void printSQLException(final SQLException e) {
System.out.format("SQLException: %s, SQLException: %s", e.getMessage(), e.getSQLState());
}
private void printDramaAll(final List<Drama> dramaList) {
for (Drama drama : dramaList) {
System.out.println(drama);
}
}
private void printDrama(final List<Drama> dramaList) {
for (Drama drama : dramaList) {
System.out.print("[코드] " + drama.getCode() + "\t" );
System.out.println(" [이름] " + drama.getName());
}
}
}
package io.wisoft.jdbc.quiz;
public class Employee {
private String code;
private String name;
private String manager;
private int salary;
public Employee() {}
public Employee(String name, String manager) {
this.name = name;
this.manager = manager;
}
public Employee(String code, String name, String manager, int salary){
this.code = code;
this.name = name;
this.manager = manager;
this.salary = salary;
}
public Employee (String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public String getManager() {
return manager;
}
public int getSalary() {
return salary;
}
@Override
public String toString() {
return "[코드] " + code + "\t" +
" [이름] " + name + "\t" +
" [관리자] " + manager + "\t" +
" [급여] " + salary + "\n" ;
}
}
package io.wisoft.jdbc.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 EmployeeService {
public void employeeFindAll() {
String query = "SELECT EMP_CODE, EMP_NAME, EMP_MGT, EMP_SAL FROM EMPLOYEE";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet 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)));
}
printEmployeeAll(employees);
} catch(SQLException sqex) {
printSQLException(sqex);
}
}
public void employeeFindName() {
String query = "SELECT e1.emp_name, e2.emp_name \n" +
" from employee e1 left outer join employee e2 \n" +
"on e1.emp_mgt = e2.emp_code;";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Employee> employees = new ArrayList<>();
while (rs.next()) {
employees.add(new Employee(rs.getString(1), rs.getString(2)));
}
printEmployee(employees);
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void employeeFindSalary() {
String query = "SELECT SUM(EMP_SAL), AVG(EMP_SAL) FROM EMPLOYEE";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
EmployeeTotalAvgSalary employeeTotalAvgSalary = new EmployeeTotalAvgSalary(rs.getInt(1), rs.getInt(2));
System.out.print("[총합] " + employeeTotalAvgSalary.getTotal() + "\t");
System.out.println("[평균] " + employeeTotalAvgSalary.getAverage());
}
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void employeeFindSalaryAll() {
String query = "SELECT EMP_NAME, EMP_SAL FROM EMPLOYEE ORDER BY EMP_SAL DESC, EMP_NAME ASC";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Employee> employees = new ArrayList<>();
while (rs.next()) {
employees.add(new Employee(rs.getString(1), rs.getInt(2)));
System.out.print("[이름] " + rs.getString(1));
System.out.println(" [급여] " + rs.getInt(2));
}
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void employeeFindAboveAvgSal() {
String query = "SELECT EMP_NAME, EMP_SAL FROM EMPLOYEE WHERE EMP_SAL > (SELECT AVG(EMP_SAL) FROM EMPLOYEE)";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
List<Employee> employees = new ArrayList<>();
while (rs.next()) {
employees.add(new Employee(rs.getString(1), rs.getInt(2)));
System.out.print("[이름] " + rs.getString(1));
System.out.println(" [급여] " + rs.getInt(2));
}
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
public void employeeInsertData() {
String query = "insert into employee (emp_code, emp_name, emp_mgt, emp_sal) values (?, ?, ?, ?)";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, "E903");
pstmt.setString(2, "손진현");
pstmt.setString(3, "E901");
pstmt.setInt(4, 4000);
int retValue = pstmt.executeUpdate();
System.out.println(retValue + "건의 사항이 처리되었습니다.");
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
// public void employeeUpdateData() {
// String query = "update employee set emp_rcode = 'R003', emp_sal = emp_sal + (emp_sal*0.2) where emp_name = '김수현'";
//
// try (Connection conn = PostgresqlAccess.getConnection();
// PreparedStatement pstmt = conn.prepareStatement(query)) {
// pstmt.setString(1, "R003");
// pstmt.setInt(2,emp_sal + (emp_sal*0.2));
// pstmt.setString(3, "김수현");
// } catch (SQLException sqex) {
// printSQLException(sqex);
// }
// }
public void employeeDeleteData() {
String query = "DELETE FROM EMPLOYEE WHERE EMP_NAME = ?";
try (Connection conn = PostgresqlAccess.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, "손진현");
int retValue = pstmt.executeUpdate();
System.out.println(retValue + "건의 사항이 처리되었습니다.");
} catch (SQLException sqex) {
printSQLException(sqex);
}
}
private void printSQLException(final SQLException e) {
System.out.format("SQLException: %s, SQLException: %s", e.getMessage(), e.getSQLState());
}
private void printEmployee(final List<Employee> employees) {
for (Employee employee : employees) {
System.out.print("[사원] " + employee.getName());
System.out.println(" [직속] " + employee.getManager());
}
}
private void printEmployeeAll(final List<Employee> employees) {
for (Employee employee : employees) {
System.out.print(employee);
}
}
}
package io.wisoft.jdbc.quiz;
public class EmployeeTotalAvgSalary {
private int total;
private double average;
public EmployeeTotalAvgSalary(int total, double average) {
this.total = total;
this.average = average;
}
public int getTotal() {
return total;
}
public double getAverage() {
return average;
}
@Override
public String toString() {
return "EmployeeTotalAvgSalary{" +
"total=" + total +
", average=" + average +
'}';
}
}
package io.wisoft.jdbc.quiz;
public class Main {
public static void main(String[] args) {
DepartmentService departmentService = new DepartmentService();
EmployeeService employeeService = new EmployeeService();
DramaService dramaService = new DramaService();
System.out.println("Q1. HNU 엔터테인먼트 부서의 코드, 이름, 위치를 검색하시오.");
departmentService.departmentFindAll();
System.out.println();
System.out.println("Q2. HNU 엔터테인먼트 연예관계자의 코드, 이름, 관리자, 급여를 검색하시오.");
employeeService.employeeFindAll();
System.out.println();
System.out.println("Q3. HNU-E에서 제작한 드라마의 코드, 이름을 검색하시오.");
dramaService.dramaFindCodeAndName();
System.out.println();
System.out.println("Q4. 드라마 방영사가 KBC 이거나 SBC 인 드라마를 검색하시오.");
dramaService.dramaProductFindKBCAndSBC();
System.out.println();
System.out.println("Q5. 모든 드라마 제작사를 중복없이 검색하시오.");
dramaService.dramaFindProduct();
System.out.println();
System.out.println("Q6. 연예관계자들의 급여의 총합과 평균 급여액을 계산하시오.");
employeeService.employeeFindSalary();
System.out.println();
System.out.println("Q7. 방영일자가 아직 확정되지 않은 드라마의 이름을 검색하시오.");
dramaService.dramaFindName();
System.out.println();
System.out.println("Q8. 연예관계자에 대해 연예관계자의 이름과 직속 상사의 이름을 검색하시오.");
employeeService.employeeFindName();
System.out.println();
System.out.println("Q9. 연예관계자에 대해 이름과 급여를 출력하고, 급여의 내림차순으로 정렬하시오. 단, 동일 급여일 때는 이름의 오름차순으로 정렬하시오.");
employeeService.employeeFindSalaryAll();
System.out.println();
System.out.println("Q10. 모든 연예관계자를 직급별로 그룹화하고, 평균 급여액이 5000 이상인 직급에 대해 연예 관계자의 직급, 평균 급여액, 최소 급여액, 최대 급여액을 검색하시오.");
System.out.println();
System.out.println("Q11. 모든 연예관계자의 평균 급여액보다 많은 급여를 받는 연예관계자의 이름과 급여를 검색하라.");
employeeService.employeeFindAboveAvgSal();
System.out.println();
System.out.println("Q12. 방영일자가 확정되지 않은 드라마의 방영일자가 2013-05-01로 편성되었습니다. 알맞게 변경하시오.");
dramaService.dramaUpdateOpdate();
System.out.println();
System.out.println("Q13. 연예관계자 김수현 씨가 대리에서 실장으로 승진하고 급여가 20% 증가되었습니다. 알맞게 변경하시오.");
System.out.println();
System.out.println("Q14. 우리 회사에 한 명의 임원이 등록되었습니다. 코드는 E903, 이름은 손진현, 관리자는 E901, 급여는 4000입니다. 알맞게 등록하시오.");
employeeService.employeeInsertData();
System.out.println();
System.out.println("Q15. 연예관계자인 손진현님이 퇴직했습니다. 연예관계자 목록에서 제거하시오.");
employeeService.employeeDeleteData();
System.out.println();
}
}
package io.wisoft.jdbc.quiz;
import java.sql.*;
public class PostgresqlAccess {
private static Connection conn = null;
public void init() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
String url = "jdbc:postgresql://arjuna.db.elephantsql.com:5432/bijdlile";
String username = "bijdlile";
String password = "QYdOIlgomeiV3LAxuqwwAnsm2nRiznmZ";
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}