Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 푸리에 변환
- 사전학습
- 예제
- css 기초
- FileWriter
- Position
- FlowLayout
- 미디어쿼리
- rnn
- g검정
- GridLayout
- iframe 태그
- 메서드
- ObjectOutputStream
- 반응형 웹 프로젝트
- java
- Database
- BorderLayout
- html 프로젝트
- CSS
- 퍼셉트론
- html 기초
- 상속
- FFT
- inline
- 파이썬
- oracle
- Codility
- HTML
- 반응형웹
Archives
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 61, 62일차 본문

Class
: 클래스 정보를 찾을 때 사용하는 클래스
- forName() : 클래스 이름 검색. try - catch문으로 예외 처리 필요
드라이버 클래스 등록 확인
try {
// 프로젝트에 클래스를 검색해서 없으면 예외를 발생시킴
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("드라이버 등록 성공");
} catch (ClassNotFoundException e) {
System.out.println("드라이버 등록 실패");
// e.printStackTrace();
}
Oracle 접속 (Connection)
public Connection getConnection() {
Connection conn = null;
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String username = "C##dbexam";
String password = "m1234";
try {
// oracle DB에 접속을 시도함
// 접속이 성공하면, 접속정보를 Connection 객체에 저장시키고, 리턴함
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
SQL 처리 ; insert
public int insertArticle(String name, int age, double height) {
// SQL문에는 세미콜론(;)이 포함되어 있으면 안됨
// SQL문에 삽입될 데이터 위치에는 "?"로 표시함
String sql = "insert into dbtest values (?, ?, ?, sysdate)";
int result = 0; // 처리 결과를 저장할 변수
// DB에 접속
Connection conn = getConnection();
// SQL문 요청, 응답 처리 클래스
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// SQL문의 "?" 자리에 데이터를 삽입시켜서, SQL문을 완성하기
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setDouble(3, height);
// SQL문 처리 작업
// 1) SQL문 처리 요청
// 2) 응답 기다림
// 3) 전달된 응답 데이터를 리턴함
// => insert 처리 데이터수를 oracle db가 전달함
// => 기본값이 auto commit
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 접속 끊기
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
SQL 처리 ; select
public void selectArticle() {
String sql = "select * from dbtest";
// 접속
Connection conn = getConnection();
// 요청, 응답 클래스
PreparedStatement pstmt = null;
// oracle db로부터 전달된 결과를 데이터를 저장하는 클래스
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
// SQL문 처리 작업
// 1) SQL문 처리 요청
// 2) 응답 기다림
// 3) 전달된 응답 데이터를 ResultSet 객체에 저장시키고 리턴함
// => oracle db는 전체데이터를 문자열로 전달함
rs = pstmt.executeQuery();
// ResultSet 객체에 저장된 데이터 처리
while(rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
double height = rs.getDouble("height");
String logtime = rs.getString("logtime");
String result = String.format("%s\t%d\t%.2f\t%s", name, age, height, logtime);
System.out.println(result);
}
} catch (SQLException 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();
}
}
}
SQL 처리 ; update
public int updateArticle(String name) {
String sql = "update dbtest set age=age+1 where name=?";
int result = 0;
Connection conn = getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// sql문 완성하기
pstmt.setString(1, name);
// SQL문 처리 작업
// 1) SQL문 처리 요청
// 2) 응답 기다림
// 3) 전달된 응답 데이터를 리턴함
// => insert 처리 데이터수를 oracle db가 전달함
// => 기본값이 auto commit
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
SQL 처리 ; delete
public int deleteArticle(String name) {
String sql = "delete dbtest where name=?";
int result = 0;
Connection conn = getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// sql문 완성하기
pstmt.setString(1, name);
// SQL문 처리 작업
// 1) SQL문 처리 요청
// 2) 응답 기다림
// 3) 전달된 응답 데이터를 리턴함
// => insert 처리 데이터수를 oracle db가 전달함
// => 기본값이 auto commit
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 60일차 (0) | 2022.12.08 |
|---|---|
| [Java/Eclipse] Java 59일차 (0) | 2022.12.07 |
| [Java/Eclipse] Java 58일차 (0) | 2022.12.06 |
| [Java/Eclipse] Java 56일차 (0) | 2022.12.02 |
| [Java/Eclipse] Java 55일차 (0) | 2022.12.01 |