Using prepaid statements - I

10 August 2008

SQL statements are executed on a database tables using Statement object. If the sql statement takes parameter, then it is a wise decision to use PreparedStatement as they are more flexible and makes coding easier.

Creating SQL statements that take parameters provides flexible statements to work with. We can use the same statement and supply it with different values each time we execute it.
We use place holders (question marks) in the query, which are supplied values to make queries flexible. Values are supplied using setXx(…) methods for example:

void setDouble(int parameterIndex, double x) 
void setFloat(int parameterIndex, float x) 
void setInt(int parameterIndex, int x) 
void setLong(int parameterIndex, long x)

One has to specify the index as well while setting values. If we miss a parameter or supply an extra parameter, then org.postgresql.util.PSQLException is thrown. Time for an example. I will connect to Postgres database and will display the contents of a table. Then I will use PreparedStatement statement to update a record and for confirmation, I will again display the contents of the table.

contiued …

del.icio.us:Using prepaid statements - I  digg:Using prepaid statements - I  spurl:Using prepaid statements - I  wists:Using prepaid statements - I  simpy:Using prepaid statements - I  newsvine:Using prepaid statements - I  blinklist:Using prepaid statements - I  furl:Using prepaid statements - I  reddit:Using prepaid statements - I  fark:Using prepaid statements - I  blogmarks:Using prepaid statements - I  Y!:Using prepaid statements - I  smarking:Using prepaid statements - I  magnolia:Using prepaid statements - I  segnalo:Using prepaid statements - I  gifttagging:Using prepaid statements - I

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Using prepaid statements - I

Leave a Reply