Commit 1954070c authored by Sangmin Lee's avatar Sangmin Lee
Browse files

Quiz Upload

parent 5060a60f
package assignment;
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() {
final StringBuilder sb = new StringBuilder("Department{");
if (code != null) sb.append("code='").append(code).append('\'');
if (name != null) sb.append(", name='").append(name).append('\'');
if (location != null) sb.append(", location='").append(location).append('\'');
sb.append('}');
return sb.toString();
}
}
package assignment;
import java.sql.Date;
public class Drama {
private String code;
private String name;
private String producer;
private String broadcastStation;
private Date broadcastDate;
public Drama(String code, String name, String producer, String broadcastStation, Date broadcastDate) {
this.code = code;
this.name = name;
this.producer = producer;
this.broadcastStation = broadcastStation;
this.broadcastDate = broadcastDate;
}
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 getBroadcastStation() {
return broadcastStation;
}
public String getProducer() {
return producer;
}
public void setProducer(String producer) {
this.producer = producer;
}
public void setBroadcastStation(String broadcastStation) {
this.broadcastStation = broadcastStation;
}
public Date getBroadcastDate() {
return broadcastDate;
}
public void setBroadcastDate(Date broadcastDate) {
this.broadcastDate = broadcastDate;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Drama{");
if(code != null) sb.append("code='").append(code).append('\'');
if(name != null) sb.append(", name='").append(name).append('\'');
if(producer != null) sb.append(", producer='").append(producer).append('\'');
if(broadcastStation != null) sb.append(", broadcastStation='").append(broadcastStation).append('\'');
if(broadcastDate != null) sb.append(", broadcastDate=").append(broadcastDate);
sb.append('}');
return sb.toString();
}
}
package assignment;
public class Employee {
private String code;
private String name;
private String manager;
private int salary;
private String roleCode;
public Employee(String code, String name, String manager, int salary, String roleCode) {
this.code = code;
this.name = name;
this.manager = manager;
this.salary = salary;
this.roleCode = roleCode;
}
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 String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Employee{");
if (code != null) sb.append("code='").append(code).append('\'');
if (name != null) sb.append(", name='").append(name).append('\'');
if (manager != null) sb.append(", manager='").append(manager).append('\'');
if (salary != -1) sb.append(", salary=").append(salary);
if (roleCode != null) sb.append(", roleCode='").append(roleCode).append('\'');
sb.append('}');
return sb.toString();
}
}
package assignment;
import assignment.service.*;
import assignment.service.selectservice.DepartmentSelectService;
import assignment.service.selectservice.DramaSelectService;
import assignment.service.selectservice.EmployeeSelectService;
import assignment.service.updateservice.DramaUpdateService;
import assignment.service.updateservice.EmployeeUpdateService;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
DepartmentSelectService departmentSelectService = new DepartmentSelectService();
DramaSelectService dramaSelectService = new DramaSelectService();
DramaUpdateService dramaUpdateService = new DramaUpdateService();
EmployeeSelectService employeeSelectService = new EmployeeSelectService();
EmployeeUpdateService employeeUpdateService = new EmployeeUpdateService();
EmployeeInsertService employeeInsertService = new EmployeeInsertService();
EmployeeDeleteService employeeDeleteService = new EmployeeDeleteService();
System.out.println("1. HNU Entertainment의 부서 코드, 이름 위치를 검색하시오.");
departmentSelectService.printAll();
System.out.println();
System.out.println("2. HNU Entertainment의 연예관계자 코드, 이름, 관리자, 급여를 검색하시오");
employeeSelectService.printNotAll(
EmployeeSelectService.EMP_CODE,
EmployeeSelectService.EMP_NAME,
EmployeeSelectService.EMP_MGT,
EmployeeSelectService.EMP_SAL);
System.out.println();
System.out.println("3. HNU Entertainment에서 제작한 드라마의 코드와 이름을 검색하시오.");
dramaSelectService.printByProducer("HNU");
System.out.println();
System.out.println("4. 드라마 방영사가 KBC이거나 SBC인 드라마를 검색하시오.");
dramaSelectService.printByBroadcastStation("KBC", "SBC");
System.out.println();
System.out.println("5. 드라마 제작사를 검색하시오. 단, 중복된 값이 있으면 제거하시오.");
dramaSelectService.printGroupBy(DramaSelectService.DRM_PRD);
System.out.println();
System.out.println("6. 연예관계자들의 급여의 총합과 평균 급여액을 계산하시오.");
employeeSelectService.printSumAndAvg(EmployeeSelectService.EMP_SAL);
System.out.println();
System.out.println("7. 방영일자가 아직 확정되지 않은 드라마의 이름을 검색하시오.");
dramaSelectService.printIsNull(DramaSelectService.DRM_OPDATE, DramaSelectService.DRM_NAME);
System.out.println();
System.out.println("8. 연예관계자에 대해 연예관계자의 이름과 직속 상사의 이름을 검색하시오.");
employeeSelectService.printNameAndManager();
System.out.println();
System.out.println("9. 연예관계자에 대해 이름과 급여를 출력하고, " +
"급여의 내림차순으로 정렬하시오. 단, 동일 급여일 때는 이름의 오름차순으로 정렬하시오.");
employeeSelectService.printOrderBySalaryAndName();
System.out.println();
System.out.println("10. 모든 연예관계자를 직급별로 그룹화하고, 평균 급여액이 5000 이상인 직급에 대해 " +
"연예 관계자의 직급, 평균 급여액, 최소 급여액, 최대 급여액을 검색하시오.");
employeeSelectService.printEmployeeBySalary();
System.out.println();
System.out.println("11. 모든 연예관계자의 평균 급여액보다 많은 급여를 받는 연예관계자의 이름과 급여를 검색하라.");
employeeSelectService.printByOverAvgSalary();
System.out.println();
System.out.println("12. 방영일자가 확정되지 않은 드라마의 방영일자가 " +
"2013-05-01로 편성되었습니다. 알맞게 변경하시오.");
dramaUpdateService.updateDramaOpdate("2013-05-01");
System.out.println();
System.out.println("13. 연예관계자 김수현 씨가 대리에서 실장으로 " +
"승진하고 급여가 20% 증가되었습니다. 알맞게 변경하시오.");
employeeUpdateService.updateSalaryAndRole("김수현", "실장", 1.2);
System.out.println();
System.out.println("14. 우리 회사에 한 명의 임원이 등록되었습니다. 코드는 E903, 이름은 손진현, " +
"관리자는 E901, 급여는 4000입니다. 알맞게 등록하시오.");
employeeInsertService.insertEmployees(Collections.singletonList(
new Employee("E903", "손진현", "E901", 4000, "R005")
));
System.out.println();
System.out.println("15. 연예관계자인 손진현님이 퇴직했습니다. 연예관계자 목록에서 제거하시오.");
employeeDeleteService.deleteEmployee("손진현");
System.out.println();
}
}
1. HNU Entertainment의 부서 코드, 이름, 위치를 검색하시오
```sql
select *
from department;
```
2. HNU Entertainment의 연예관계자 코드, 이름, 관리자, 급여를 검색하시오
```sql
select emp_code, emp_name, emp_name, emp_sal
from employee e
```
3. HNU Entertainment에서 제작한 드라마의 코드와 이름을 검색하시오.
```sql
select drm_code, drm_name
from drama
where drm_prd like '%HNU%';
```
4. 드라마 방영사가 KBC이거나 SBC인 드라마를 검색하시오.
```sql
select *
from drama
where drm_brd in ('KBC', 'SBC');
```
5. 드라마 제작사를 검색하시오. 단, 중복된 값이 있으면 제거하시오.
```sql
select drm_prd
from drama
group by drm_prd;
```
6. 연예관계자들의 급여의 총합과 평균 급여액을 계산하시오.
```sql
select sum(emp_sal), round(avg(emp_sal), 2)
from employee;
```
7. 방영일자가 아직 확정되지 않은 드라마의 이름을 검색하시오.
```sql
select drm_name
from drama
where drm_opdate is null;
```
8. 연예관계자에 대해 연예관계자의 이름과 직속 상사의 이름을 검색하시오.
```sql
select e1.emp_name, e2.emp_name
from employee e1, employee e2
where e1.emp_mgt = e2.emp_code;
```
1. 연예관계자에 대해 이름과 급여를 출력하고, 급여의 내림차순으로 정렬하시오. 단, 동일 급여일 때는 이름의 오름차순으로 정렬하시오.
```sql
select emp_name, emp_sal
from employee
order by emp_sal desc, emp_name;
```
10. 모든 연예관계자를 직급별로 그룹화하고, 평균 급여액이 5000 이상인 직급에 대해 연예 관계자의 직급, 평균 급여액, 최소 급여액, 최대 급여액을 검색하시오.
```sql
select emp_rcode, round(avg(emp_sal), 2), min(emp_sal), max(emp_sal)
from employee
group by emp_rcode
having 5000 <= avg(emp_sal)
order by emp_rcode;
```
3. 모든 연예관계자의 평균 급여액보다 많은 급여를 받는 연예관계자의 이름과 급여를 검색하라.
```sql
select emp_name, emp_sal
from employee
where emp_sal > (
select avg(emp_sal)
from employee
);
```
1. 방영일자가 확정되지 않은 드라마의 방영일자가 2013-05-01로 편성되었습니다. 알맞게 변경하시오.
```sql
update drama set drm_opdate = '2013-05-01'
where drm_opdate is null;
```
2. 연예관계자 김수현 씨가 대리에서 실장으로 승진하고 급여가 20% 증가되었습니다. 알맞게 변경하시오.
```sql
update employee set emp_rcode = (
select emp_rcode
from emp_role
where emp_rname = '실장'
),
emp_sal = emp_sal * 1.2
where emp_name = '김수현';
```
1. 우리 회사에 한 명의 임원이 등록되었습니다. 코드는 E903, 이름은 손진현, 관리자는 E901, 급여는 4000입니다. 알맞게 등록하시오.
```sql
insert into employee values
('E903', '손진현', 'E901', 4000, 'R005');
```
2. 연예관계자인 손진현님이 퇴직했습니다. 연예관계자 목록에서 제거하시오.
```sql
delete from employee where emp_name = '손진현';
```
\ No newline at end of file
package assignment.service;
import java.sql.*;
import java.util.Arrays;
import java.util.function.Consumer;
public class DatabaseService {
private static Connection connection = null;
private static Connection getConnection() {
String url = "jdbc:postgresql://arjuna.db.elephantsql.com:5432/";
String username = "fsmfppcj";
String password = "opXwqwWLpezpFQHX6OWFl3mQW1xf0VqH";
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void executeSelectQuery(String query, Consumer<ResultSet> consumer) {
try (Connection connection = DatabaseService.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();
) {
consumer.accept(resultSet);
} catch (SQLException e) {
printSQLException(e);
}
}
public static void executeUpdateQuery(String query) {
try (Connection connection = DatabaseService.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
) {
connection.setAutoCommit(false);
int retValue = preparedStatement.executeUpdate();
connection.commit();
System.out.println(retValue + "건의 사항이 처리되었습니다.");
} catch (SQLException e) {
printSQLException(e);
}
}
public static void executeInsertQuery(String query, Consumer<PreparedStatement> consumer) {
try (Connection connection = DatabaseService.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
) {
connection.setAutoCommit(false);
consumer.accept(preparedStatement);
int[] retValue = preparedStatement.executeBatch();
connection.commit();
System.out.println(retValue.length + "건의 사항이 처리되었습니다.");
} catch (SQLException e) {
printSQLException(e);
}
}
public static void executeDeleteQuery(String query) {
try (Connection connection = DatabaseService.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
) {
connection.setAutoCommit(false);
int retValue = preparedStatement.executeUpdate();
connection.commit();
System.out.println(retValue + "건의 사항이 처리되었습니다.");
} catch (SQLException e) {
printSQLException(e);
}
}
public static void printSQLException(SQLException exception) {
System.out.println("SQLException: " + exception.getMessage());
System.out.println("SQLState: " + exception.getSQLState());
System.out.println(Arrays.toString(exception.getStackTrace()));
}
}
package assignment.service;
public class EmployeeDeleteService {
final private String targetTable = "employee";
public void deleteEmployee(String name) {
String query = "delete from " + targetTable + " where emp_name = '" + name + "'";
DatabaseService.executeDeleteQuery(query);
}
}
package assignment.service;
import assignment.Employee;
import java.util.List;
public class EmployeeInsertService {
final private String targetTable = "employee";
public void insertEmployees(List<Employee> employees) {
String query = "insert into employee values (?, ?, ?, ?, ?)";
DatabaseService.executeInsertQuery(query, preparedStatement -> {
try {
for (Employee employee : employees) {
preparedStatement.setString(1, employee.getCode());
preparedStatement.setString(2, employee.getName());
preparedStatement.setString(3, employee.getManager());
preparedStatement.setInt(4, employee.getSalary());
preparedStatement.setString(5, employee.getRoleCode());
preparedStatement.addBatch();
preparedStatement.clearParameters();
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
package assignment.service.selectservice;
public class DepartmentSelectService {
private final int DEPT_CODE = 1;
private final int DEPT_NAME = 2;
private final int DEPT_LOC = 3;
private SelectService selectService = new SelectService("department");
public void printAll() {
selectService.all(resultSet -> {
try {
while (resultSet.next()) {
System.out.println("[부서코드] " + resultSet.getString(DEPT_CODE));
System.out.println("[부서이름] " + resultSet.getString(DEPT_NAME));
System.out.println("[부서위치] " + resultSet.getString(DEPT_LOC));
System.out.println("-----------------------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
public static void main(String[] args) {
DepartmentSelectService service = new DepartmentSelectService();
service.printAll();
}
}
package assignment.service.selectservice;
import assignment.Drama;
import java.util.ArrayList;
import java.util.List;