I am trying to implement PreparedStatement, which won’t work with sql DB.
Suppose I have the following sql query:
String selectSqlQuery = "SELECT * FROM customer WHERE f1 = ? AND f2 =? AND f3 > ?";
and the following code:
//----
prest = con.prepareStatement(selectSqlQuery );
prest.setString(1, "val1");
prest.setString(2, "val2");
prest.setInt(3, 108);
ResultSet rs = prest.executeQuery();
//---
My question is how to implement setString and setInt methods for injecting params?
For now I save parameters’ indexes and values into HashMap, but after it I can’t make injection into sql query string.
Since you’re writing your own driver, you can play with your class a little. Let’s change the approach. If you have a query like this one:
Replace the
?to turn it intoAbout your set methods, those will have to save your new parameters in an
Objectarray, again matching against the index.Before executing the query, take advantage of your formatted string and set the parameters:
The
formatmethod will replace the{number}substrings for the corresponding element in the index represented by thenumberbetween brackets.