HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"The JDBC API"
Vol. 2, Issue 4, p. 18

	

Listing 1: Update.java

import java.sql.*;

class Update {
  public static void main(String argv[]) {
    try {
      Class.forName(argv[0]);
      String url = argv[1];
      Connection con = DriverManager.getConnection(url, 
                                                   "borg",
                                                   "");
      Statement s = con.createStatement();   
      s.executeUpdate("INSERT INTO t_test " +
                      "(test_id, test_val) " +
                      "VALUES(5, 'Yet another " +
                      "silly test')");
      System.out.println("Insert succeeded.");
      s.close();
      con.close();
    }
    catch( Exception e ) {
      e.printStackTrace();
    }
  }
}

Listing 2: Simple Query

ResultSet r;
Statement s = connection.createStatement();

r = s.executeQuery("SELECT test_id, test_val FROM t_test");

Listing 3: Simple Update

Statement s = connection.createStatement();
int row_count;

row_count = s.executeUpdate("UPDATE t_test SET test_val = "+
                            "'womble' WHERE test_id = 5");

Listing 4: Result set processing

ResultSet r;
Statement s = connection.createStatement();

r = s.executeQuery("SELECT test_id, test_val FROM t_test");
while( r.next() ) {
  System.out.println("Got: test_id=" + r.getInt(1) + 
                     ", test_val=" + 
                     r.getString("test_val"));
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.