Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • PreparedStatement Interface

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 269
    Comment on it

    PreparedStatement interface:

    It is used to execute a SQL parameterized query and it compiles only one time and we can use it many times. It extends the Statement interface. It is more secure then Statement interface and safely provide values to the SQL parameters, with the help of setter methods (i.e. setInt(int,int)setString(int,String), etc.).

    Example of parameterized query:


     String inStr="insert into employee values(?,?,?,?)";  

    The question mark represents parameter for PreparedStatement query.

    Example of PreparedStatement Interface to insert records:

        import java.sql.*;  
        class PrepareStmt{  
        public static void main(String args[])throws Exception{  
        Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
        PreparedStatement ps=conn.prepareStatement("insert into emp130 values(?,?,?)");  
        BufferedReader br=new BufferedReader(new InputStreamReader(;  
        System.out.println("enter  Employee id:");  
        int Emp_id=Integer.parseInt(br.readLine());  
        System.out.println("enter Employee name:");  
        String Emp_name=br.readLine();  
        System.out.println("enter Employee salary:");  
        float salary=Float.parseFloat(br.readLine());  
        int i=ps.executeUpdate();  
        System.out.println(i+" records affected");  
        System.out.println("Do you want to continue: y/n");  
        String st=br.readLine();  

    Each question mark represents one value and it is known as parameter marker. Values for question mark should be provided before the SQL statement is executed

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: