글목록

2015. 1. 23.

[Java] Connect to Oracle DB via JDBC driver

 

1. Download Oracle JDBC Driver


Get Oracle JDBC driver here – ojdbcxxx.jar


2. Java JDBC connection example

             
package oracle;

import java.sql.*;

public class test {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
          Connection conn = null;
          ResultSet rs = null;
          Statement stmt = null;
          String url = null;
          String id = "Toplogic";
          String pw = "Toplogic";
     
          try{
     
              url ="jdbc:oracle:thin:@localhost:1521/orcl";
              Class.forName("oracle.jdbc.driver.OracleDriver");
              conn = DriverManager.getConnection(url,id,pw);
             
              System.out.println("DB connection complete");
                  
              stmt = conn.createStatement();
              rs = stmt.executeQuery("select * from TEST");
              
              while(rs.next()){
                  String name = rs.getString(1);
                  String pwd = rs.getString(2);
                  System.out.println(name +", "+pwd);
              }
    
              stmt.close();
              conn.close();
    

          } catch (Exception e){
              e.printStackTrace();
          }
      }
  

}

댓글 없음:

댓글 쓰기