Thursday, September 29, 2011

Retrieve Records


Retrieve_Records

//Retrieving the values from table student in a database

import java.sql.*;
public class Retrieve_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
ResultSet rs=stmt.executeQuery("select *from student");
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

For Select Querys we have to use the "executeQuery" method
it return the Results into a ResultSet object
by using next() method we can move the next row from one row to anethor
here we used differnt type getter method(we can use getString() for all data types)
but its not recomended
TRy with different type select querys

*/



Using Index Retrive Data From The Table
Using Prepared Statement