package kr.ac.mlec.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/*
drop table t_test;
create table t_test(
id varchar2(10) primary key
, name varchar2(20) not null
);
id : hong, name : 홍길동 삽입
insert into t_test(id, name) values('hong', '홍길동')
*/
public class InsertMain01 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 1. 드라이버 로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. 데이터베이스 접속 및 Connection 객체 얻기
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "hr";
String password = "hr";
conn = DriverManager.getConnection(url, user, password);
// 3. SQL을 갖는 Statement 객체 얻기
stmt = conn.createStatement();
String sql = "insert into t_test(id, name) values('hong3', '홍길순')";
// 4. statement 객체를 이용하여 sql문 실행, 결과 반환
int cnt = stmt.executeUpdate(sql);
System.out.println("총 " + cnt + "개 행이 삽입되었습니다");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5. 데이터베이스 접속 해제
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
'Java' 카테고리의 다른 글
[Chapter07-1] 객체지향 프로그래밍1 (0) | 2021.12.06 |
---|---|
[Session] 세션 시간 및 연장 설정 (0) | 2021.05.12 |
[간단한 for문] list (0) | 2021.04.30 |
[jdbc] 게시판 만들기 (VO, DAO, Main) (0) | 2021.04.29 |
[jdbc] update 자바 오라클 연동 (0) | 2021.04.28 |