01.31(9일차) -자바연동해서 데이터구축
import java.sql.*;
import java.util.*;
public class DB {
static Connection conn = null;
static Statement stmt = null;
static ResultSet rs = null;
static String URL = "jdbc:mysql://localhost:3306/koreaitacademy";
public static void loadConnect() {
try {
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e) {
System.out.println("오류 : "+e);
}
try {
conn = DriverManager.getConnection(URL, "root", "1234");
}catch(Exception e) {
System.err.println("오류 : "+e);
}
}
public static void selectEmployee() { // 함수 이름
String sql = "select * from employee"; // SQL 쿼리
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println("ID : "+rs.getString("id")); // 속성 이름
System.out.println("Name : "+rs.getString("name")); // 속성 이름
System.out.println("Email : "+rs.getString("email")); // 속성 이름
}
}catch(Exception e) {
System.out.println("오류 : "+e);
}
}
public static void selectSkill() { // 함수 이름
String sql = "select * from skill"; // SQL 쿼리
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println("E_ID : "+rs.getString("e_id")); // 속성 이름
System.out.println("Skill : "+rs.getString("skill")); // 속성 이름
}
}catch(Exception e) {
System.out.println("오류 : "+e);
}
}
public static void selectLecture() { // 함수 이름
String sql = "select * from lecture"; // SQL 쿼리
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println("ID : "+rs.getString("id")); // 속성 이름
System.out.println("Name : "+rs.getString("name")); // 속성 이름
System.out.println("E_ID : "+rs.getString("e_id")); // 속성 이름
System.out.println("N_Student : "+rs.getString("n_student")); // 속성 이름
}
}catch(Exception e) {
System.out.println("오류 : "+e);
}
}
public static void selectStudent() { // 함수 이름
String sql = "select * from student"; // SQL 쿼리
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println("ID : "+rs.getString("id")); // 속성 이름
System.out.println("Name : "+rs.getString("name")); // 속성 이름
System.out.println("L_ID : "+rs.getString("l_id")); // 속성 이름
System.out.println("Tel : "+rs.getString("tel")); // 속성 이름
}
}catch(Exception e) {
System.out.println("오류 : "+e);
}
}
public static int insertEmployee(String name, String email) {
String sql;
if(email.equals("")) {
sql = "insert into employee(name, email) values('"+name+"', default)";
}else {
sql = "insert into employee(name, email) values('"+name+"', '"+email+"')";
}
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
return stmt.executeUpdate(sql);
}catch(Exception e) {
System.out.println("오류 : "+e);
return 0;
}
}
public static void insertStudent(String name, int l_id, String tel) {
List<Integer> lid_list = checkLID();
boolean check = lid_list.contains(l_id);
String sql;
if(check) {
if(tel.equals("")) {
sql = "insert into student(name, l_id, tel) values('"+name+"', "+l_id+", default)";
}else {
sql = "insert into student(name, l_id, tel) values('"+name+"', "+l_id+", '"+tel+"')";
}
}else {
System.out.println("Lecture 테이블에 해당 ID가 존재하지 않습니다.");
sql = null;
}
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
refreshData(l_id);
}catch(Exception e) {
System.out.println("오류 : "+e);
}
}
public static List<Integer> checkLID(){
List<Integer> id_list = new ArrayList<>();
String sql = "select id from lecture";
System.out.println("SQL > "+sql);
int id;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
id = rs.getInt(1);
id_list.add(id);
}
}catch(Exception e) {
System.out.println("오류 : "+e);
}
return id_list;
}
public static int count(int l_id) {
String sql = "select count(*) from student where l_id = "+l_id+"";
System.out.println("SQL > "+sql);
int count_value = 0;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
count_value = rs.getInt(1);
}
}catch(Exception e) {
System.out.println("오류 : "+e);
}
return count_value;
}
public static void refreshData(int l_id) {
int count = count(l_id);
String sql = "update lecture set n_student = "+count+" where id = "+l_id+"";
System.out.println("SQL > "+sql);
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch(Exception e) {
System.out.println("오류 : "+e);
}
}
}