본문 바로가기

Program

Java Oracle Connection 11g....

간단합니다. 
긁어가셔서 붙여넣으시고 테스트만 하셔요

오라클 하위버전도 전부 똑같습니다. 드라이버만 달라져요
물론 ojdbc6.jar 는 클래스패스에 넣으시고요

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class TestJDBC {
public static void main(String[] args){
Connection connection = null;
try {
   // Load the JDBC driver
   String driverName = "oracle.jdbc.driver.OracleDriver";
   Class.forName(driverName);

   // Create a connection to the database
   String serverName = "127.0.0.1"; //<--수정
   String portNumber = "1521"; //<--필요시 수정
   String sid = "sid";     //<--수정
   String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
   String username = "id";//<--수정
   String password = "pw";//<--수정
   connection = DriverManager.getConnection(url, username, password);
   System.out.println("connection ok");
} catch (ClassNotFoundException e) {
System.out.println("Could not find the database driver");
   // Could not find the database driver
} catch (SQLException e) {
System.out.println("Could not connect to the database");
}finally{
try{connection.close();}catch(Exception e){}
}
}