Java Reference
In-Depth Information
Example
int result = statement.executeUpdate(insert);
if (result==0)
System.out.println("* Insertion failed! *");
As a simple illustration of database modifi cations in action, the next example is
an extension of our earlier example ( JDBCSelect ).
Example
After displaying the initial contents of the database, this example executes the
SQL statements shown in examples (i)-(iii) above and then displays the modifi ed
database. This time, a single try block is used to surround all code after the loading
of the JDBC driver. This makes the code somewhat less cumbersome, but (as noted
at the end of the last example) does not allow us to display problem-specifi c SQL
error messages. The only other change to the code is the introduction of method
displayTable , which encapsulates the selection and display of all data from the table
(in order to avoid code duplication).
import java.sql.*;
public class JDBCChange
{
private static Statement statement;
private static ResultSet results;
public static void main(String[] args)
{
Connection connection = null;
try
{
//Step 1…
connection = DriverManager.getConnection(
"jdbc:odbc:Finances","","");
}
catch(ClassNotFoundException cnfEx)
{
System.out.println(
"* Unable to load driver! *");
System.exit(1);
}
//For any of a number of reasons, it may not be
//possible to establish a connection…
catch(SQLException sqlEx)
{
System.out.println(
"* Cannot connect to database! *");
 
Search WWH ::




Custom Search