There are two main ways of manipulating your database: executeQuery and executeUpdate
executeQuery returns a resultSet and can be used to search your database.
executeUpdate returns an integer. It can be used to to INSERT, UPDATE or DELETE files. The number that it returns is the number of records updated. 0 means no records were updated.
As you’ve already see, the following finds all the records in table Book
ResultSet rs = statement.executeQuery("SELECT * FROM BOOK");
The following code finds all the books by ‘Joe Bloggs’
ResultSet rs = statement.executeQuery("SELECT * FROM BOOK WHERE author = 'Joe Bloggs' ");
The following code will add a record to a table Book. It assumes that any other fields in the table have default values set.
statement.executeUpdate("INSERT INTO BOOK(title,Author,isFiction) VALUES ('Spot', 'Eric Hill',TRUE)");
This code changes the author of ‘Spot’ to ‘Joe Bloggs’
statement.executeUpdate("UPDATE BOOK SET Author = 'Joe Bloggs' WHERE title ='Spot' ");
This code deletes all the books with the title ‘Spot’
statement.executeUpdate("DELETE FROM BOOK WHERE title = 'Spot'");