글목록

2014. 11. 14.

[Java] Connect to Oracle DB via JDBC driver


1. Make Java Project



2. Download JDBC Driver


http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
 

3. Add JDBC Driver 





4. Java setting




   
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 = "jeon";
          String pw = "jeon";
     
          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();
          }
      }
  

}

댓글 없음:

댓글 쓰기