Friday, 13 March 2015

Java Oracle Connection

import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
 
//create  the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","System","root");
 
//create the statement object
Statement stmt=con.createStatement();
 
//execute query
ResultSet rs=stmt.executeQuery("select * from STUDENTS");
while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3)+"   "+rs.getString(4)+"  "+rs.getString(5));
 
//close the connection object
con.close();
 
}
catch(Exception e)
{ System.out.println(e);
}
 
}

No comments: