Á¦¸ñ : JDBC°ÁÂ1(JDBC ¿¬°á)
/*******************************************************
create table test(
name varchar2(20),
no number(8)
)
*******************************************************/
import db.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.io.IOException;
import java.sql.*;
import java.sql.SQLException;
public class jdbc1 {
public static void main(String[] args) {
Connection conn= null;
PreparedStatement ps =null;
ResultSet rs =null;
String stmt = null;
String strRet = null;
int intRet = 0;
// 1. jdbc driver ¸¦ µî·ÏÇÑ´Ù.
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e ) {
System.out.println(e);
}
// 2. DB¿Í ¿¬°áÇÑ´Ù.
try{
// OCI
// listener¸¦ ¶ç¿ï Çʿ䰡 ¾ø´Ù.
conn = DriverManager.getConnection(
"jdbc:oracle:oci8:@","id","passwd");
// thin
// listener¸¦ ¶ç¿ï Çʿ䰡 ÀÖ´Ù.
// testhost : host À̸§
// 1521 : port ¹øÈ£
// ORA815 : sid
/*
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@testhost:1521:ORA815","id","passwd");
*/
} catch (SQLException e) {
System.out.println(e);
}
// 3.±âÁ¸ÀÇ test Å×À̺íÀ» »èÁ¦ÇÑ´Ù.
try {
ps = conn.prepareStatement("drop table test");
ps.executeUpdate();
System.out.println("±âÁ¸ test Å×ÀÌºí »èÁ¦");
} catch (SQLException e) {
System.out.println(e);
}
// 4.test Å×ÀÌºí »ý¼º
try {
ps = conn.prepareStatement(
"create table test( " +
" name varchar2(20)," +
" no number(8) " +
") "
);
ps.executeUpdate();
System.out.println("Å×½ºÆ® Å×ÀÌºí »ý¼º");
// 5.dbÀ» ´Ý´Â´Ù.
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
--------------------------------------------------
¼³¸í.
Á¦°¡ »ç¿ëÇϰí ÀÖ´Â DB´Â oracle8iÀÔ´Ï´Ù.
1.jdbc driver ¸¦ µî·ÏÇÑ´Ù.
2.DB¿Í ¿¬°áÇÑ´Ù.
a. OCIÀÎ °æ¿ì´Â listener¸¦ ¶ç¿ï Çʿ䰡 ¾øÀ¸¸ç
¾Æ·¡¿Í °°ÀÌ ÄÚµùÇÏ¸é µË´Ï´Ù.
conn = DriverManager.getConnection(
"jdbc:oracle:oci8:@","id","passwd");
b. thinµå¶óÀ̹öÀÎ °æ¿ì, ÀÌ·±°æ¿ì´Â ÇÁ·Î±×·¥ÀÇ À§Ä¡¿Í
DBÀÇ À§Ä¡°¡ ´Ù¸¥°æ¿ì¿¡ »ç¿ëÇÕ´Ï´Ù.
listener°¡ ¶ç¿öÀú ÀÖ¾î¾ß Çϸç
tnsnames.ora¿¡ ±â·ÏµÈ Á¤º¸¸¦ ÀÌ¿ëÇÏ¿© DB¸¦ ¿¬°áÇÕ´Ï´Ù.
tnsnames.oraÆÄÀÏÀ» Àá½Ã »ìÆìº¸¸é
testhost =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = testhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = testhost)
)
)
¿Í °°½À´Ï´Ù.
tnsnames.oraÀÇ Á¤º¸¸¦ ÀÌ¿ëÇÏ¿© ¾Æ·¡¿Í °°ÀÌ DB¸¦ ¿¬°áÇÕ´Ï´Ù.
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@testhost:1521:ORA815","id","passwd");
3.½ÇÇàÀ» ¿øÇÏ´Â ¹®ÀåÀ» conn.prepareStatementÀÇ ÀÎÀÚ°ªÀ¸·Î ³Ñ±äÈÄ
ps.executeUpdate()¸¦ ½ÇÇàÇϸé DB¹®ÀÚÀÌ ½ÇÇàµË´Ï´Ù.
Connection conn= null;
=> db connectionÀ» °¡Áö°í ÀÖ½À´Ï´Ù.
PreparedStatement ps =null;
=> sql ¹®ÀåÀÇ Á¤º¸¸¦ °¡Áý´Ï´Ù.
ResultSet rs =null;
=> ½ÇÇàµÈ sql¹®ÀåÀÇ DB recordÁ¤º¸¸¦ °¡Áý´Ï´Ù.
5.dbÀ» ´Ý´Â´Ù.
ps.close();
conn.close();
|