이렇게 적어도 들어갈 수 있다.
정형화된 데이터
commit 하가 전까지를 트랜잭션./ commit을 한 곳까지 다 지움
어떤 타입인지 모르겠으니까
class타입으로 밀어붙임
package dbtest.dao;
public class InsertMain {
public InsertMain() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
InsertMain im = new InsertMain();
}
}
커넥션을 만들어주는 메소드
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class InsertMain {
private Connection conn;
private PreparedStatement pstmt;
public InsertMain() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"c##java", "1234");
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("접속 에러");
}
}
public void insertArticle() {
Scanner scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
String name = scan.next();
System.out.print("나이 입력 : ");
int age = scan.nextInt();
System.out.print("키 입력 : ");
double height = scan.nextDouble();
try {
// 접속
this.getConnection();
// 스테이트먼트 생성
pstmt = conn.prepareStatement("insert into dbtest values(?,?,?,sysdate)");
// java는 0이지만 db는 순서가 1부터 시작
// ? 데이터 대입
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setDouble(3, height);
// 실행
int su = pstmt.executeUpdate(); // 개수 리턴
System.out.println(su + "개의 행이 삽입되었습니다.");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("스테이트먼트 에러");
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
InsertMain im = new InsertMain();
im.insertArticle();
}
}
심지어 자동 커밋
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class InsertMain {
private Connection conn;
private PreparedStatement pstmt;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
public InsertMain() {
try {
Class.forName(driver);
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("접속 에러");
}
}
환경 변수로 주소와 이름 비밀번호를 빼고 식을 간다하게 바꿔주었다.
드라이버 로딩
커넥션
수정할 이름 입력 : 홍
=> 홍이 들어간 이름을 찾아서 나이를 1증가 시키시오
update
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class UpdateMain {
private Connection conn;
private PreparedStatement pstmt;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
public UpdateMain() {
try {
Class.forName(driver);
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url,username, password);
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateArticle() {
Scanner scan = new Scanner(System.in);
System.out.print("수정할 이름 입력 : ");
String name = scan.next();
this.getConnection(); // 접속
String sql = "update dbtest set age = age+1 where name like ?";
try {
pstmt = conn.prepareStatement(sql);
// ?에 데이터 대입
// ?는 1번으로 시작
pstmt.setString(1, "%" + name + "%");
int su = pstmt.executeUpdate();
System.out.println(su + "개의 행을 수정하였습니다.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
UpdateMain um = new UpdateMain();
um.updateArticle();
}
}
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SelectMain {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
public SelectMain() {
try {
Class.forName(driver);
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url,username, password);
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
}
}
// select로 안에 내용을 여기로 가져오겠다.
public void selectArticle() {
this.getConnection();
String sql = "select * from dbtest";
try {
pstmt = conn.prepareStatement(sql);
// 위 String sql = "select * from dbtest"; 문장을 처리, 번역해줄 번역가
rs = pstmt.executeQuery(); // 실행 -> ResultSet으로 리턴
// rs.next() - 현재 위치에 레코드(행)가 있으면 true, 없으면 false
// - 다음 레코드로 이동한다.
// rs.getString("name") = rs.getString(1)
// rs.getInt("age") = rs.getInt(2)
// rs.getDouble("height") = rs.getDouble(3)
// rs.getDate("logtime") or rs.getString("logtime") = rs.getDate(4)
while(rs.next()) {
System.out.println(rs.getString("name")+"\t"
+rs.getInt("age")+"\t"
+rs.getDouble("height")+"\t"
+rs.getString("logtime"));
}//while -> rs.next가 없을 때 까지
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SelectMain sm = new SelectMain();
sm.selectArticle();
}
}
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class InsertMain {
private Connection conn;
private PreparedStatement pstmt;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
public InsertMain() {
try {
Class.forName(driver);
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("접속 에러");
}
}
public void insertArticle() {
Scanner scan = new Scanner(System.in);
System.out.print("이름 입력 : ");
String name = scan.next();
System.out.print("나이 입력 : ");
int age = scan.nextInt();
System.out.print("키 입력 : ");
double height = scan.nextDouble();
try {
// 접속
this.getConnection();
// 스테이트먼트 생성
pstmt = conn.prepareStatement("insert into dbtest values(?,?,?,sysdate)");
// java는 0이지만 db는 순서가 1부터 시작
// ? 데이터 대입
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setDouble(3, height);
// 실행
int su = pstmt.executeUpdate(); // 개수 리턴
System.out.println(su + "개의 행이 삽입되었습니다.");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("스테이트먼트 에러");
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
InsertMain im = new InsertMain();
im.insertArticle();
}
}
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class UpdateMain {
private Connection conn;
private PreparedStatement pstmt;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
public UpdateMain() {
try {
Class.forName(driver);
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url,username, password);
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateArticle() {
Scanner scan = new Scanner(System.in);
System.out.print("수정할 이름 입력 : ");
String name = scan.next();
this.getConnection(); // 접속
String sql = "update dbtest set age = age+1 where name like ?";
try {
pstmt = conn.prepareStatement(sql);
// ?에 데이터 대입
// ?는 1번으로 시작
pstmt.setString(1, "%" + name + "%");
int su = pstmt.executeUpdate();
System.out.println(su + "개의 행을 수정하였습니다.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
UpdateMain um = new UpdateMain();
um.updateArticle();
}
}
package dbtest.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SelectMain {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
public SelectMain() {
try {
Class.forName(driver);
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url,username, password);
System.out.println("접속 성공");
} catch (SQLException e) {
e.printStackTrace();
}
}
// select로 안에 내용을 여기로 가져오겠다.
public void selectArticle() {
this.getConnection();
String sql = "select * from dbtest";
try {
pstmt = conn.prepareStatement(sql);
// 위 String sql = "select * from dbtest"; 문장을 처리, 번역해줄 번역가
rs = pstmt.executeQuery(); // 실행 -> ResultSet으로 리턴
// rs.next() - 현재 위치에 레코드(행)가 있으면 true, 없으면 false
// - 다음 레코드로 이동한다.
// rs.getString("name") = rs.getString(1)
// rs.getInt("age") = rs.getInt(2)
// rs.getDouble("height") = rs.getDouble(3)
// rs.getDate("logtime") or rs.getString("logtime") = rs.getDate(4)
while(rs.next()) {
System.out.println(rs.getString("name")+"\t"
+rs.getInt("age")+"\t"
+rs.getDouble("height")+"\t"
+rs.getString("logtime"));
}//while -> rs.next가 없을 때 까지
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SelectMain sm = new SelectMain();
sm.selectArticle();
}
}
package student.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Student {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "1234";
Scanner scan = new Scanner(System.in);
public Student() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("접속 에러");
}
}
public void menu() {
int num;
while(true) {
System.out.println();
System.out.println("***********");
System.out.println(" 관리");
System.out.println("***********");
System.out.println("1. 입력 ");
System.out.println("2. 검색 ");
System.out.println("3. 삭제 ");
System.out.println("4. 종료 ");
System.out.println("***********");
System.out.print ("번호 : ");
num = scan.nextInt();
if(num == 4) {
System.out.println();
System.out.println("프로그램을 종료합니다.");
break;
}
if(num == 1) {
insertArticle();
}else if (num == 2) {
selectArticle();
}else if (num == 3) {
deleteArticle();
}else {
System.out.println("1~4사이의 숫자를 입력하세요");
}
}
}
public void insertArticle() {
int num;
while(true) {
System.out.println("***********");
System.out.println("1. 학생 ");
System.out.println("2. 교수 ");
System.out.println("3. 관리자 ");
System.out.println("4.이전메뉴 ");
System.out.println("***********");
System.out.print ("번호 : ");
num = scan.nextInt();
if(num == 4) {
break;
}
String name = null;
String value = null;
int code = 0;
System.out.print("이름 입력 : ");
name = scan.next();
if(num==1) {
System.out.print("학번 입력 : ");
value = scan.next();
code = 1;
}else if(num==2) {
System.out.print("과목 입력 : ");
value = scan.next();
code = 2;
}else if(num==3) {
System.out.print("부서 입력 : ");
value = scan.next();
code = 3;
}else if(num >= 5){
System.out.println("1~4사이의 숫자를 입력하세요");
}
try {
this.getConnection();
pstmt = conn.prepareStatement
("insert into student2 values(?,?,?)");
pstmt.setString(1, name);
pstmt.setString(2, value);
pstmt.setInt(3, code);
int su = pstmt.executeUpdate();
System.out.println(su + "개의 행이 삽입되었습니다.");
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public void selectArticle() {
int num;
while(true) {
System.out.println("***********");
System.out.println("1.이름 검색 ");
System.out.println("2.전체 검색 ");
System.out.println("3.이전 메뉴");
System.out.println("***********");
System.out.print ("번호 : ");
num = scan.nextInt();
if(num == 3) {
break;
}
String name = null;
if(num==1) {
System.out.print("검색할 이름 입력 : ");
name = scan.next();
}
this.getConnection(); // 접속
String sql = null;
if(num == 1)
sql = "select * from student2 where name like ?";
else
sql = "select * from student2";
try {
pstmt = conn.prepareStatement(sql);
if(num == 1) pstmt.setString(1, "%" + name + "%");
rs = pstmt.executeQuery();
while(rs.next()) { // rs가 없을 때까지
System.out.print("이름 = " + rs.getString("name")+"\t\t");
if(rs.getInt("code") == 1)
System.out.println("학번 = " + rs.getString("value"));
else if(rs.getInt("code") == 2)
System.out.println("과목 = " + rs.getString("value"));
else if(rs.getInt("code") == 3)
System.out.println("부서 = " + rs.getString("value"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private void deleteArticle() {
System.out.print("삭제할 이름 입력 : ");
String name = scan.next();
getConnection();
String sql = "delete student2 where name = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
int su = pstmt.executeUpdate();
System.out.println(su + "개의 행이 삭제되었습니다.");
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Student st = new Student();
st.menu();
}
}