시다바리
[Java] Java로 오라클 접속하는 예제 본문
/**
* Java로 오라클 접속하는 예제
* Myoung-Geun Jang(jangsalt@nextelecom.co.kr)
* 2008. 5. 9
*/
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
public class OracleConnection {
private static Driver JDBC_DRIVER = null;
Connection connection = null;
ResultSet rs = null;
Statement stmt = null;
String strSql = String.format("select * from (Table_Name)");
static {
loadDriver();
}
private static void loadDriver() {
try {
JDBC_DRIVER = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
System.out.println("드라이버 로딩 성공...");
} catch(Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public OracleConnection() {
System.out.println("Oracle DB 접속 중 ...");
String url = "jdbc:oracle:thin:@(Oracle IP):(Oracle Port):(SID)";
String user = "(ID)";
String pwd = "(Password)";
int test;
try {
connection = DriverManager.getConnection( url, user, pwd );
System.out.println("DB 연결 성공!");
} catch(Exception e) {
System.err.println(e.getMessage());
}
try {
stmt = connection.createStatement();
test = stmt.executeUpdate(insSql);
rs = stmt.executeQuery(strSql);
while(rs.next()){
System.out.println(rs.getString(1));
}
rs.close();
} catch( SQLException e ) {
System.err.println( "SQL Exception : " + e.getMessage() );
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
}
public static void main(String[] args) {
OracleConnection oc = new OracleConnection();
}
}