project : day35
package : jdbc.simple
class : JDBCTest
//1. Driver class 로딩
//2. 연결
//3. Statement 조회
//4. query전송
//4-1 -erlect의 경우 ResultSet
//5. 연결 닫기
package jdbc.simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
// 1. Driver class 로딩
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Driver 로딩 성공");
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
try {
// 2. 연결
conn = DriverManager.getConnection(url, "scott", "tiger");
// 3. Statement 조회
stmt = conn.createStatement();
// 4. query전송
String insertQuery = "insert into address values('111-444','서울','동작구')";
int cnt = stmt.executeUpdate(insertQuery);
System.out.println(cnt + "개의 로우가 insert되었습니다.");
String selectQuery = "select zipcode,si,dong from address";
rset = stmt.executeQuery(selectQuery);
// 4-1 -select의 경우 ResultSet
while (rset.next()) {
String zipcode = rset.getString(1);
String si = rset.getString(2);
String dong = rset.getString(3);
System.out.println(zipcode + " - " + si + " - " + dong);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 5. 연결 닫기
if (rset != null) {
try {
rset.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
'프로그래밍 > JSP Programming' 카테고리의 다른 글
HTML테그 실행하기 (0) | 2012.04.16 |
---|---|
HTML의 개요 (0) | 2012.04.16 |
JSP시작하기 (0) | 2012.04.16 |
Web Programming (0) | 2012.04.16 |
이클립스 톰캣연동하기 (0) | 2012.04.16 |