Thursday, September 29, 2011

Prepare_statement


Prepare_statement

// PrepareStatement retriving the values from table student in a database

import java.sql.*;
public class Prepare_statement

{
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");
PreparedStatement pstmt=con.prepareStatement("select *from student where sno=?");
System.out.println("preparedStatement object is created");
pstmt.setInt(1,15);
ResultSet rs=pstmt.executeQuery();
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"));
}
pstmt.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:
Table in Database:
create table student(sno number(10),name varchar2(20),place varchar2(20));
prepared statement is improves the performence

*/