package Exam01;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1.등록 2.조회 3.수정 4.삭제 5.종료 6.개별조회 >> ");
int choice = sc.nextInt();
if (choice == 1) {
System.out.println("===== 등록 =====");
System.out.print("ID : ");
String id = sc.next();
System.out.print("PW : ");
String pw = sc.next();
System.out.print("NAME : ");
String name = sc.next();
System.out.print("NICK : ");
String nick = sc.next();
PreparedStatement psmt = null;
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
String db_id = "hr";
String db_pw = "hr";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
if (conn != null) {
System.out.println("DB연결 성공");
} else {
System.out.println("DB연결 실패");
}
String sql = "INSERT INTO SPRINGMEMBER VALUES(?,?,?,?)";
psmt = conn.prepareStatement(sql);
psmt.setString(1, id);
psmt.setString(2, pw);
psmt.setString(3, name);
psmt.setString(4, nick);
int cnt = psmt.executeUpdate();
if (cnt > 0) {
System.out.println("등록성공");
} else {
System.out.println("등록실패");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} else if (choice == 2) {
System.out.println("===== 조회 =====");
PreparedStatement psmt = null;
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
String db_id = "hr";
String db_pw = "hr";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
if (conn != null) {
System.out.println("DB연결 성공");
} else {
System.out.println("DB연결 실패");
}
String sql = "SELECT * FROM SPRINGMEMBER";
psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
String id = rs.getString(1);
String pw = rs.getString(2);
String name = rs.getString(3);
String nick = rs.getString(4);
System.out.println("ID : " + id + ", PW : " + pw + ", NAME : " + name + ", NICK : " + nick);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} else if (choice == 3) {
System.out.println("===== 수정 =====");
System.out.print("ID : ");
String id = sc.next();
System.out.print("NICK : ");
String nick = sc.next();
PreparedStatement psmt = null;
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
String db_id = "hr";
String db_pw = "hr";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
if (conn != null) {
System.out.println("DB연결 성공");
} else {
System.out.println("DB연결 실패");
}
String sql = "UPDATE SPRINGMEMBER SET NICK = ? WHERE ID = ?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, nick);
psmt.setString(2, id);
int cnt = psmt.executeUpdate();
if (cnt > 0) {
System.err.println("수정성공");
} else {
System.err.println("수정실패");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} else if (choice == 4) {
System.out.println("===== 삭제 =====");
System.out.print("ID : ");
String name = sc.next();
PreparedStatement psmt = null;
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
String db_id = "hr";
String db_pw = "hr";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
if (conn != null) {
System.out.println("DB연결 성공");
} else {
System.out.println("DB연결 실패");
}
String sql = "DELETE FROM SPRINGMEMBER WHERE ID = ?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, name);
int cnt = psmt.executeUpdate();
if (cnt > 0) {
System.out.println("삭제성공");
} else {
System.out.println("삭제실패");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else if (choice == 5) {
System.out.println("시스템을 종료합니다.");
break;
} else if (choice == 6) {
System.out.println("===== 개별조회 =====");
System.out.print("ID : ");
String searchid = sc.next();
PreparedStatement psmt = null;
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
String db_id = "hr";
String db_pw = "hr";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
if (conn != null) {
System.out.println("DB연결 성공");
} else {
System.out.println("DB연결 실패");
}
String sql = "SELECT * FROM SPRINGMEMBER WHERE ID = ?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, searchid);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
String id = rs.getString(1);
String pw = rs.getString(2);
String name = rs.getString(3);
String nick = rs.getString(4);
System.out.println("ID : " + id + ", PW : " + pw + ", NAME : " + name + ", NICK : " + nick);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} else {
System.out.println("잘못된 명령어입니다.");
}
}
sc.close();
}
}