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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class EMPTask {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs= null;
PreparedStatement pstmt = null;
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
String id ="student";
String pw ="1234";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(jdbcUrl, id, pw);
stmt= conn.createStatement();
//1.데이터 삽입_insert into
String sql_insert = "insert into emp values (?, ?, ?, ?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql_insert);
pstmt.setInt(1, 7777);
pstmt.setString(2, "WANGUN");
pstmt.setString(3, "DOG");
pstmt.setInt(4, 1111);
pstmt.setString(5, "10/10/03");
pstmt.setInt(6, 300);
pstmt.setInt(7, 100);
pstmt.setInt(8, 40);
pstmt.executeQuery();
System.out.println("데이터 삽입 완료");
//2.데이터 수정_update
String sql_update = "update emp set sal='1000' where ENAME='WANGUN'";
pstmt = conn.prepareStatement(sql_update);
pstmt.executeQuery();
System.out.println("데이터 수정 완료");
//3.데이터 삭제_delete
String sql_delete = "delete from emp where ENAME='WANGUN'";
pstmt = conn.prepareStatement(sql_delete);
pstmt.executeQuery();
System.out.println("데이터 삭제 완료");
//4.데이터 출력_select
String sql_select = "select * from emp order by empno";
rs = stmt.executeQuery(sql_select);
System.out.println("[부서리스트] ");
while(rs.next()) {
System.out.println("----------------------------");
System.out.println("사원번호: " + rs.getInt(1));
System.out.println("사원이름: " + rs.getString(2));
System.out.println("직업: " + rs.getString(3));
System.out.println("매니저 번호: " + rs.getString(4));
System.out.println("입사일: " + rs.getString(5));
System.out.println("급여: " + rs.getString(6));
System.out.println("커미션: " + rs.getString(7));
System.out.println("부서번호: " + rs.getString(8));
}
System.out.println("----------------------------");
}catch (ClassNotFoundException e) {
System.out.println("데이터베이스 드라이버를 찾을 수 없습니다.");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}finally {
if(rs!=null) try{rs.close();}catch(SQLException e){}
if(stmt!=null) try{stmt.close();}catch(SQLException e){}
if(conn!=null) try{conn.close();}catch(SQLException e){}
}
}
}
| cs |
내 블로그 목록
2018년 7월 15일 일요일
[JSP] JDBC 기초 문법 코드 _ INSERT, UPDATE, DELETE, SELECT
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기