Record Retrieve from Oracle via OracleResultSet - Java JDBC

Java examples for JDBC:Oracle

Description

Record Retrieve from Oracle via OracleResultSet

Demo Code


import java.util.*;
import java.sql.*;
import oracle.jdbc.*;
class RecordRetrive
{
  OracleConnection oconn;//from w ww  . ja v  a2  s  . c  o  m
  PreparedStatement pstmt;
  OracleResultSet ors;
  String scomm;
  String vusername,vpassword,url;
  Scanner sc = new Scanner(System.in);
  public RecordRetrive()
  {
    try
    {
      DriverManager.registerDriver(new OracleDriver());
    }
    catch(SQLException ex)
    {
      System.out.println(ex.toString());
    }
    System.out.print("Enter User Name: ");
    vusername = sc.next();
    System.out.print("Enter Password: ");
    vpassword = sc.next();
    url = "jdbc:oracle:thin:@localhost:1521:ORCL";  
    try
    {
      oconn = (OracleConnection)DriverManager.getConnection(url,vusername,vpassword);  
    
      scomm = "select * from DEPT";
      pstmt = (PreparedStatement)oconn.prepareStatement(scomm);   
      ors=(OracleResultSet)pstmt.executeQuery(); //WHEN RETRIVE USE executeQuery() method and When INSERT DEN USE executeUpdate() method.
      while(ors.next()){
          System.out.println(ors.getString(1)+" "+ors.getString(2)+" "+ors.getString(3));
      }
      ors.close();
      pstmt.close();
      oconn.close();
    }
      catch(SQLException ex)
    {
      System.out.println(ex.toString());
    }
  }
}
public class RetriveRecord 
{
    public static void main(String args[])
    {
        RecordRetrive rt=new RecordRetrive();
    }
}

Related Tutorials