JDBC - a little bit more
* now, we can query a database using JDBC
* how about updating or changing a
database?
* how about if you have SEVERAL things
to do in a row?
* [maybe] how you can ask questions
ABOUT a database using JDBC
* to UPDATE or otherwise change a database,
you'll use the Statement method
executeUpdate, and note that it returns an int
* see example AddToLog.java
* PreparedStatement - subclass of Statement class
* it is FASTER when doing several similar actions
in a row than having a separate Statement for each
* it CAN be safer, because it helps reduce the
chances of some kinds of SQL injection
* PreparedStatement myPStmt =
con.prepareStatement(
"insert into log_table " +
"values " +
"(?, sysdate)");
* you give it the command string AT
prepared-statement-creation time, WITH
? for each value you want to set later
* you SET each ? using
a set* (setString, setDouble, setDate, etc.)
with the position of the question mark (1, 2, ...)
and the desired value for that question mark
myPStmt.setString(1, "moo");
* and, once you've set all the ?s,
run either executeQuery or executeUpdate
(depending on the kind of SQL statement)
with NO arguments)
* demo'd in AddManyToLog.java
(and NOTE: you CAN and should use PreparedStatement
instances for select statements when appropriate,
also!)