Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.
The more general execute() method helps in this case. This version returns a boolean value. If the value is true, then the statement was a query; false indicates that the statement was an update. Of course, you want to know the results in either case, so you can use one of the convenience methods to ask for it, as follows: boolean is_query = stmt.execute();
141
Chapter 7: Using JDBC to Interact with SQL Databases if(is_query) {
ResultSet rs = stmt.getResultSet();
...
} else {
int rows_updated = stmt.getUpdateCount();
...
}
Calling simple statements
With the simple Statement object, you don't have any SQL commands issued before you get to call execute.
So, for these statements, you need to use one of the execute statements that takes a string. The string contains the SQL that you want to run. A simple query runs like this:
Statement stmt = conn.getStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Product"); With the ResultSet in hand, you can now process the values as we discussed earlier in the chapter.
Calling prepared statements
In prepared statements, you already have the majority of the SQL data set. To execute a statement, you only need to fill in missing parameter values and call the executeQuery() method. This time, as you have already set the SQL data, you do not need to supply any values to executeQuery().
String cmd = "SELECT * FROM Product WHERE category = ?"; PreparedStatement stmt = conn.prepareStatement(cmd);
...
stmt.setString(1, "book");
ResultSet rs = stmt.executeQuery();
Calling stored procedures
Stored procedure calls add one more interesting twist: You can have values returned as a result set, but you also have OUT parameters to deal with. To start with, you set up the query and execute the action just as you do with the prepared statement:
String cmd = "CALL LIST_CATEGORY(?, ?)";
CallableStatement stmt = conn.prepareCall(cmd);
stmt.registerOutParameter(2, Types.INTEGER);
stmt.setString(1, "books");
ResultSet rs = stmt.executeQuery();
After executing the statement, you will need to read the value of the OUT parameter in position index 2. In the preceding code, you have marked it as being an integer value, so you use the getInt() method from the CallableStatement interface to read the value back out.
int num_items = stmt.getInt(2);
The position index here must be the same as the one you declared when registering the OUT parameter earlier.
Tip
If you are using the generic execute() method rather than executeQuery(), the specification 142
Chapter 7: Using JDBC to Interact with SQL Databases recommends that you always fetch the ResultSet before accessing the OUT parameter values.
Making updates to the database
Making changes to the existing database is similar to querying the database. For simple queries, you pass in the SQL statement to be executed, where the pre−built versions will not need arguments. The one crucial difference is the return value of the methods. When making a query, you get back a collection of the rows that match. When making an update, you get a number representing the number of rows that have been affected by that update.
As far as JDBC is concerned, any change to the table structure is an update. Modifying, inserting, or deleting rows all count as updates. Also considered updates are the basic database commands, such as creating, altering, or dropping tables. Because these are just SQL commands, you can create the database and all its contents from JDBC. There is no need to build external scripts for your database management should you choose not to.