Java Reference
In-Depth Information
9.
Undo the last deletion.
In the String pw, delete the last character.
10.
The resulting message (or lack of message) depends on the driver used and whether the database you are trying
to access is password protected. (For instance, the default for MS Access databases is no password-protection but the
DB2 and Oracle examples returns a “Password is incorrect” message.) A similar error will be generated for mistakes
with the user ID value.
11.
Undo the last deletion.
Tutorial: Using a Statement Object to Insert Data
So far, we have created all the objects needed to insert, modify, retrieve, and delete data in a database. Now we just
have to use those objects and methods. This section assumes knowledge of SQL. (If you are not familiar with SQL,
please see Appendix B.)
Statement objects have two primary methods (executeQuery and executeUpdate) that allow the programmer to
perform SQL statements in a Java class. Each method expects an SQL statement as a string. executeQuery is used for
SQL select statements and will return a result set with the requested database information. executeUpdate is used for
SQL insert , update , and delete statements and does not return data from the database. However, executeUpdate does
provide the option of returning the number of rows affected by the SQL statement.
Each of the statements assumes that the database (schema in SQL-speak) tntdb has been created and a
connection has been established. In addition, we assume that a table named employee was created in tntdb with the
following SQL statement (please note that if Oracle is being used, payrate must be defined as NUMBER not DOUBLE):
CREATE TABLE tntdb.employee
(empnum CHAR(10), empname CHAR(25),
street CHAR(15), city CHAR(12),
state CHAR(2), zip CHAR(5),
payrate DOUBLE, exempts INT)
Enough talking, let's get our hands dirty.
In DBAccess, before the con.close() statement, add the following statement:
1.
stmt.executeUpdate("INSERT INTO tntdb.employee " +
"VALUES('111', 'Mary Worker', '1 Main St.'," +
"'Enid', 'OK', '77777', 17.50, 3)");
Remember, the SQL statement is simply a string parameter for the executeUpdate method. RAD does not check
for SQL syntax errors and, unfortunately, the statement syntax is complex. For instance, notice that both single and
double quotes are used. Double quotes surround the entire SQL statement, but single quotes surround the values
being inserted into character fields.
In addition, the SQL statement must be passed as a single string. If the string spans multiple lines, each line is
a separate string (surrounded by double quotes) and must be concatenated to the others. Within the SQL statement,
commas separate each value and there must be at least one space after the table name. Finally, the entire string is
enclosed in parentheses. Expect many mistakes typing SQL statements, and try not to get too upset at yourself.
2.
Run DBAccess as a Java application.
The three messages will appear in the Console but there will be no indication that the SQL statement
successfully executed.
 
Search WWH ::




Custom Search