The Magic of PreparedStatement Interface

If you are working on a Java Project that requires data storage and manipulation, a Java interface could be of great help to you. The “PreparedStatement” interface, under “java.sql” package, pre compiles and stores your SQL statements in an object to efficiently execute the said statement multiple times. There are setter methods that you have to use in order to define the values needed in a statement. Let’s take this example:

PreparedStatement pstmt = con.prepareStatement(“UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?”);

The object “pstmt” stores the “UPDATE” query for further execution. Notice that the query has question marks (?) characters. The values here could be acquired from a variable or an explicit value that you set using the setter methods of the PreparedStatement Interface (i.e. pstmt.setDate(), pstmt.setObject(), pstmt.setString()). Going back to our example, here is a complete usage of the said interface:

PreparedStatement pstmt = con.prepareStatement(“UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?”);pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)

Upon execution, the sql query will execute an “UPDATE” query to “EMPLOYEES” table setting a “SALARY” of “153833.00” to a row with “ID” of “110592”. The first parameter of the given methods is the sequence of the question mark in the actual query statement. That is, the setBigDecimal with “1” parameter takes the question mark of “SALARY” and the setInt for “ID”.

This interface makes your query statements acquire values in a versatile way. Since we are dealing with multiple data in terms of data storage, this scheme would be of great help.

TIP: Check the Java Documentation for the required parameters of the setter methods. I had some difficulties when I initially used this in my code. Nonetheless, this is of great help. :D

2 Comments

  1. Hi ,
    i had a small doubt in my query..i submit it to u.. If u find sum time u juz go thru it and clear the pbm. It doesnt shows any error in Tomcat console(As my prevoius query shows an error ion console).. So i myself concluded that my query is messed up.. So i need ur help resolve my pbm… Here is my code…

    String SQL2=”UPDATE tbl_rmadetails SET serial_no=?, replaced_serial_no=?, status=?, customer_id=?, item_id=? WHERE customer_id=? AND item_id=? AND serial_no=?”;

    ps=con.prepareStatement(SQL2);

    ps.setInt(1,serial_no);
    ps.setInt(2,replaced_serial_no);
    ps.setString(3,status);
    ps.setString(4,custo);
    ps.setString(5,ite);

    ps.setString(6,custo);
    ps.setString(7,ite);
    ps.setInt(8,serial_no);
    System.out.println(“–Step2–”);
    x=ps.executeUpdate();

    the int variable x returns 0..ie..no rows r updates.. Whats wrong is there…

  2. Hi ,
    i had a small doubt in my query..i submit it to u.. If u find sum time u juz go thru it and clear the pbm. It doesnt shows any error in Tomcat console(As my prevoius query shows an error ion console).. So i myself concluded that my query is messed up.. So i need ur help resolve my pbm… Here is my code…

    String SQL2=”UPDATE tbl_rmadetails SET serial_no=?, replaced_serial_no=?, status=?, customer_id=?, item_id=? WHERE customer_id=? AND item_id=? AND serial_no=?”;

    ps=con.prepareStatement(SQL2);

    ps.setInt(1,serial_no);
    ps.setInt(2,replaced_serial_no);
    ps.setString(3,status);
    ps.setString(4,custo);
    ps.setString(5,ite);

    ps.setString(6,custo);
    ps.setString(7,ite);
    ps.setInt(8,serial_no);
    System.out.println(”–Step2–”);
    x=ps.executeUpdate();

    the int variable x returns 0..ie..no rows r updates.. Whats wrong is there…


Comments RSS TrackBack Identifier URI

Leave a comment