Guest User

JDBC connection

a guest
Sep 2nd, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1.     import java.sql.*;  
  2.     class OracleCon{  
  3.     public static void main(String args[]){  
  4.     try{  
  5.     //step1 load the driver class  
  6.     Class.forName("oracle.jdbc.driver.OracleDriver");  
  7.      
  8.     //step2 create  the connection object  
  9.     Connection con=DriverManager.getConnection(  
  10.     "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  11.      
  12.     //step3 create the statement object  
  13.     Statement stmt=con.createStatement();  
  14.      
  15.     //step4 execute query  
  16.     ResultSet rs=stmt.executeQuery("select * from emp");  
  17.     while(rs.next())  
  18.     System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
  19.      
  20.     //step5 close the connection object  
  21.     con.close();  
  22.      
  23.     }catch(Exception e){ System.out.println(e);}  
  24.      
  25.     }  
  26.     }
Add Comment
Please, Sign In to add comment