I l @ ve RuBoard |
Instead of using executeQuery on a Statement or PreparedStatement , you can use executeUpdate , which returns nothing. This method is used for any non- select SQL function. Here are a few examples showing what you can do with executeUpdate : Statement st = conn.createStatement(); st.executeUpdate("delete from employees"); PreparedStatement pst = conn.prepareStatement("INSERT INTO employees (lname_txt, fname_txt, employee_num) "+ "values (?, ?, ?)"); pst.setString(1, "Jones"); pst.setString(2, "Bob"); pst.setInt(3, 22); pst.executeUpdate(); pst.close(); pst = conn.prepareStatement("UPDATE employees set employee_num = ? where lname_txt=? and fname_txt = ?"); pst.setString(1, "Jones"); pst.setString(2, "Bob"); pst.setInt(3, 23); pst.executeUpdate(); pst.close(); The first example simply deletes all of the employee records. The second example adds a new employee record. Just as in a select , ? instances are replaced with the appropriate values using the setX methods . The second example also shows how a long SQL statement would be broken across several lines for readability. The final example shows how to update a row using parameters for both the where and set clauses. Although it wasn't used in the previous code, executeUpdate returns a value, an integer that is the number of rows modified. This is useful, for example, if you want to either modify an existing record or create a new one. You can run an update statement and, if no rows are returned, you know to run an insert. |
I l @ ve RuBoard |