Java Reference
In-Depth Information
Fig. 7.1 Output from program JDBCSelect
Modifying the Database Contents
As mentioned in Sect. 7.4 , DML (Data Manipulation Language) statements in
SQL may be divided into two categories: those that retrieve data from a database
(SELECT statements) and those that change the contents of the database in some
way (INSERT, DELETE and UPDATE statements). So far, we have dealt only with
the former, which has meant submitting our SQL statements via the executeQuery
method. We shall now look at the latter category, for which we shall have to submit
our SQL statements via the executeUpdate method. Some examples are shown below.
(i) String insert = "INSERT INTO Accounts"
+ " VALUES (123456,'Smith',"
+ "'John James',752.85)";
int result = statement.executeUpdate(insert);
(ii) String change = "UPDATE Accounts"
+ " SET surname = 'Bloggs',"
+ "fi rstNames = 'Fred Joseph'"
+ " WHERE acctNum = 123456";
(iii) String remove = "DELETE FROM Accounts"
+ " WHERE balance < 100";
result = statement.executeUpdate(remove);
For the second of these examples, the value returned by executeUpdate has not
been saved and is simply discarded by the runtime system. In practice, though, the
integer returned is often used to check whether the update has been carried out.
Search WWH ::

Custom Search