보라코딩

Day36_230216_ORACLE (시퀀스), JDBC 본문

코딩/DB

Day36_230216_ORACLE (시퀀스), JDBC

new 보라 2023. 2. 16. 14:17

 

시퀀스

 

 

 

 

 

위와 같이 박지성 하고 엔터 누르면 적용시 데이터 안나옴. 바로 적용눌러야한다!

 

 

 

오라클 JDBC

 

 

 

 

 

 

 

 

 

Build Path 설정

 

 

 

 

 

 

 

 

오라클에서 우선 작성해봄

 

 

 

 

 

순서에 맞춰 하나하나!

 

]

 

getConnection에 해당되는 내용은 아래 오라클 내용 그대로 가져온 것

 

 

 

 

 

 

 

select 

 

package co
m.mystudy.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Student_Select_Prepared {

public static void main(String[] args) {

/*===================================
JDBC 이용한 DB 연동 프로그래밍 작성 절차
0. JDBC 라이브러리 개발환경 설정(빌드경로에 등록)
1. JDBC 드라이버 로딩
2. DB연결 - Connection 객체 생성 <- DriverManager
3. Statement 문 실행(SQL 문 실행)
4. SQL 실행 결과에 대한 처리
   - SELECT : 조회(검색) 데이터 결과 값에 대한 처리
   - INSERT, UPDATE, DELETE : int 값(건수) 처리
5. 클로징 처리에 의한 자원 반납
=======================================*/
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

//1. JDBC 드라이버 로딩
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println(">> 드라이버 로딩 성공!");

//2. DB연결 - Connection 객체 생성 <- DriverManager
conn = DriverManager.getConnection( // Connection 객체는 변수 저장해야함!
"jdbc:oracle:thin:@localhost:1521:xe",
"mystudy", "mystudypw");

System.out.println(">> DB 연결 성공!");


//3. Statement 문 실행(SQL 문 실행)
// 3-1. Connection 객체로부터 PrepareStatement 객체 생성
String sql = "" + 
"SELECT ID, NAME, kor, eng, math, tot, AVG " +
" FROM STUDENT " +
"WHERE ID = ?"; // ? : 데이터 설정 위치 지정
pstmt = conn.prepareStatement(sql);

// 3-2. ?(바인드변수) 위치에 값 설정
String id = "2023004";
pstmt.setString(1, id);  // 1은 여러개일때 1부터 시작인데 하나라서

// 3-3. SQL 실행 요청 처리
rs = pstmt.executeQuery();

//4. SQL 실행 결과에 대한 처리
//   - SELECT : 조회(검색) 데이터 결과 값에 대한 처리
//   - INSERT, UPDATE, DELETE : int 값(건수) 처리
while (rs.next()) {
String str = ""
+ rs.getString("ID") + "\t"
+ rs.getString("NAME") + "\t" //대소문자상관없으나 SQL은 눈에띄게 대문자
+ rs.getInt("KOR") + "\t"
+ rs.getInt(4) + "\t"
+ rs.getInt("MATH") + "\t"
+ rs.getInt("TOT") + "\t"
+ rs.getDouble("AVG") ;
System.out.println(str);
}

System.out.println(">> SELECT 작업 끝");

} catch (ClassNotFoundException e) {
System.out.println("[예외발생] 드라이버 로딩 실패ㅠㅠ");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("[예외발생] DB연결&설정&실행 실패 ㅠㅠ");
e.printStackTrace();
} finally {
// 5. 클로징 처리에 의한 자원 반납
// close 순서는 역순

try {
if(rs != null) rs.close();
} catch (SQLException e2) {
e2.printStackTrace();
}


try {
if(pstmt != null) pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}

try {
if (conn != null) conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}

}


}

}

 

 

Insert
package com.mystudy.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Student_Insert_Prepared {

public static void main(String[] args) {
/*===================================
JDBC 이용한 DB 연동 프로그래밍 작성 절차
0. JDBC 라이브러리 개발환경 설정(빌드경로에 등록)
1. JDBC 드라이버 로딩
2. DB연결 - Connection 객체 생성 <- DriverManager
3. Statement 문 실행(SQL 문 실행)
4. SQL 실행 결과에 대한 처리
   - SELECT : 조회(검색) 데이터 결과 값에 대한 처리
   - INSERT, UPDATE, DELETE : int 값(건수) 처리
5. 클로징 처리에 의한 자원 반납
=======================================*/

Connection conn = null;
PreparedStatement pstmt = null;

//1. JDBC 드라이버 로딩
try {
Class.forName("oracle.jdbc.OracleDriver");

//2. DB연결 - Connection 객체 생성 <- DriverManager
conn = DriverManager.getConnection( // Connection 객체는 변수 저장해야함!
"jdbc:oracle:thin:@localhost:1521:xe",
"mystudy", "mystudypw");

System.out.println(">> DB 연결 성공!");

//3. Statement 문 실행(SQL 문 실행)
// 3-1. Connection 객체로부터 PrepareStatement 객체 생성
String sql = ""
+ "INSERT INTO STUDENT "
+ "   (ID, NAME, KOR, ENG, MATH) "
+ "VALUES (?, ?, ?, ?, ?)"; //1,2,3,4,5
pstmt = conn.prepareStatement(sql);

// 3-2. ?(바인드변수) 위치에 값 설정 (매칭, 대입)
String id = "2023004";
String name = "테스트";
int kor = 85;
int eng = 95;
int math = 100;

pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setInt(3, kor);
pstmt.setInt(4, eng);
pstmt.setInt(5, math);

// 3-3. SQL 실행 요청 처리 (INSERT, UPDATE, DELETE는 executeUpdate())
int result = pstmt.executeUpdate();

//4. SQL 실행 결과에 대한 처리
//   - INSERT, UPDATE, DELETE : int 값(건수) 처리
System.out.println("처리건수 : " + result);


} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5. 클로징 처리에 의한 자원 반납
// close 순서는 역순
try {
if(pstmt != null) pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}

try {
if (conn != null) conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}


}

}

 

 

 

Update

 

package co
m.mystudy.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Student_Update_Prepared {

public static void main(String[] args) {
/*===================================
JDBC 이용한 DB 연동 프로그래밍 작성 절차
0. JDBC 라이브러리 개발환경 설정(빌드경로에 등록)
1. JDBC 드라이버 로딩
2. DB연결 - Connection 객체 생성 <- DriverManager
3. Statement 문 실행(SQL 문 실행)
4. SQL 실행 결과에 대한 처리
   - SELECT : 조회(검색) 데이터 결과 값에 대한 처리
   - INSERT, UPDATE, DELETE : int 값(건수) 처리
5. 클로징 처리에 의한 자원 반납
=======================================*/

Connection conn = null;
PreparedStatement pstmt = null;

//1. JDBC 드라이버 로딩
try {
Class.forName("oracle.jdbc.OracleDriver");

//2. DB연결 - Connection 객체 생성 <- DriverManager
conn = DriverManager.getConnection( // Connection 객체는 변수 저장해야함!
"jdbc:oracle:thin:@localhost:1521:xe",
"mystudy", "mystudypw");

System.out.println(">> DB 연결 성공!");

//3. Statement 문 실행(SQL 문 실행)
// 3-1. Connection 객체로부터 PrepareStatement 객체 생성
String sql = ""
+ "UPDATE STUDENT"
+ " SET NAME = ? " //1
+ " , KOR = ? "    //2
+ " , ENG = ? "    //3
+ " , MATH = ? "   //4
+ " WHERE ID = ? ";     //5

pstmt = conn.prepareStatement(sql);

// 3-2. ?(바인드변수) 위치에 값 설정 (매칭, 대입)
String id = "2023004";
String name = "테스트4";
int kor = 77;
int eng = 88;
int math = 99;

pstmt.setString(1, name);
pstmt.setInt(2, kor);
pstmt.setInt(3, eng);
pstmt.setInt(4, math);
pstmt.setString(5, id);

// 3-3. SQL 실행 요청 처리 (INSERT, UPDATE, DELETE는 executeUpdate())
int result = pstmt.executeUpdate();

//4. SQL 실행 결과에 대한 처리
//   - INSERT, UPDATE, DELETE : int 값(건수) 처리
System.out.println("처리건수 : " + result);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5. 클로징 처리에 의한 자원 반납
// close 순서는 역순
try {
if(pstmt != null) pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}

try {
if (conn != null) conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}


}

}