Simple JDBC applications for Java DB

  Abstract: simple JDBC applications for Java DB 

  </ Td> </ tr> <tr> <td height="3302" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td height="62" align="left" valign="top"> </ td> </ tr> </ table> 

  Jdk1.6.0 downloaded today, should be carried out slowly after learning of the new features in 1.6 and some of these classic examples.    Let us first look at the java DB on the most simple examples: 
  Simple JDBC Application (OSS SimpleApp.java, documents and derby.jar, derbynet.jar, derbyclient.ar documents sought from the jdk1.6.0) 

  This example is a minimum JDBC applications.    On the procedures: 

  •   Built-in mode (the default) or as a server environment, the client operation, which depends on the parameters passed to the procedure 
  •   If you are running in the embedded pattern, then start the engine Derby 
  •   If you are running in client mode, connected to the Derby network server 
  •   Create and connect to the database 
  •   Create a Table 
  •   Inserting 
  •   Updated data 
  •   Enquiries data 
  •   Delete Table 
  •   Close connections 
  •   If you are running in the embedded mode, the closure of Derby. 

  Following is the source: 

  Import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; / * * @ author janet * / public class SimpleApp (/ * default mode is embedded * / public String framework = "embedded"; public String driver = "org.apache.derby.jdbc.EmbeddedDriver" public String protocol = "jdbc: derby: "; public static void main (String [] args) (new SimpleApp (). go (args);) void go (String [] args) (/ * parameter to determine the procedure to use as embedded or as a client use * / parseArguments (args); System.out.println ( "SimpleApp starting in the" + + framework "mode."); try (/ * load drivers, if it is embedded model, which will activate the Derby, because It also is not running. * / Class.forName (driver). newInstance (); System.out.println ( "Loaded the appropriate driver."); Connection conn = null; Properties props = new Properties (); props.put ( "user", "user1"); props.put ( "password", "user1"); / / create = true to create database derbyDB conn = DriverManager.getConnection (protocol + "derbyDB; create = true", props); System.out.println ( "Connected to and created database derbyDB"); conn.setAutoCommit (false); / / automatic mode to the Statement s = conn.createStatement () / * Create a table, by adding a few records and update 1 * / s.execute ( "create table derbyDB (num int, addr varchar (40 ))"); System.out.println (" Created table derbyDB "); s.execute (" insert into derbyDB values (1956, 'Webster St .')"); System.out.println ( "Inserted 1956 Webster"); s.execute ( "insert into derbyDB values (1910,' Union St .')"); System.out.println (" Inserted 1910 Union "); s.execute (" update derbyDB set num = 180, addr = 'Grand Ave.' where num = 1956 "); System.out.println (" Updated 1956 Webster to 180 Grand "); s. execute ( "update derbyDB set num = 300, addr = 'Lakeshore Ave.' where num = 180"); System.out.println ( "Updated 180 Grand to 300 Lakeshore"); / * Check for and the results * / ResultSet rs = s.executeQuery ( "SELECT num, addr FROM derbyDB ORDER BY num"); if (! rs.next ()) (throw new Exception ( "Wrong number of rows");) if (rs.getInt (1 )! = 300) (throw new Exception ( "Wrong row returned");) if (! rs.next ()) (throw new Exception ( "Wrong number of rows");) if (rs.getInt (1)! = 1910) (throw new Exception ( "Wrong row returned");) if (rs.next ()) (throw new Exception ( "Wrong number of rows");) System.out.println ( "Verified the rows") ; s.execute ( "drop table derbyDB ");// delete Table System.out.println (" Dropped table derbyDB "); rs.close (); s.close (); System.out.println (" Closed result set and statement "); conn.commit (); conn.close (); System.out.println (" Committed transaction and closed connection "); boolean gotSQLExc = false; if (framework.equals (" embedded ")) ( try (DriverManager.getConnection ( "jdbc: derby:; shutdown = true ");// closed database services) catch (SQLException se) (gotSQLExc = true;) if (! gotSQLExc) (System.out.println (" Database did not shut down normally ");) else (System.out.println (" Database shut down normally ");))) catch (Throwable e) (System.out.println (" exception thrown: "); if (e instanceof SQLException) (printSQLError ((SQLException) e);) else (e.printStackTrace ();)) System.out.println ( "SimpleApp finished");) static void printSQLError (SQLException e) (while (e! = null) (System.out.println (e.toString ()); e = e.getNextException ();)) private void parseArguments (String [] args) ( 
  / / System.setProperty ( "derby.system.home", "c: \ \ DBdata ");// this database can be set up data storage directory 

  Int length = args.length; for (int index = 0; index <length; index + +) (if (args [index]. EqualsIgnoreCase ( "jccjdbcclient")) (framework = "jccjdbc"; driver = "com.ibm.db2 . jcc.DB2Driver "protocol =" jdbc: derby: net: / / localhost: 1527 / ";) if (args [index]. equalsIgnoreCase (" derbyclient ")) (framework =" derbyclient "; driver =" org. apache.derby.jdbc.ClientDriver "protocol =" jdbc: derby: / / localhost: 1527 / ";)))) Here is how to run this procedure: First, how in the embedded environment (integrated into desktop applications) I run this example is the working directory c: \ java, first derby.jar copied to the c: \ java \ jar under.    Run.bat write a batch file, as follows: 
  Set CLASSPATH = c: \ java \ jar \ derby.jar; CLASSPATH%% 

  Open windows XP command line window into the working directory.    Run: 
  C: \ java> run.bat 
  C: \ java> set CLASSPATH = c: \ java \ jar \ derby.jar; 
  C: \ java> javac SimpleApp.java 

  C: \ java> java SimpleApp 
  SimpleApp starting in embedded mode. 
  Loaded the appropriate driver. 
  Connected to and created database derbyDB 
  Created table derbyDB 
  Inserted 1956 Webster 
  Inserted 1910 Union 
  Updated 1956 Webster to 180 Grand 
  Updated 180 Grand to 300 Lakeshore 
  Verified the rows 
  Dropped table derbyDB 
  Closed result set and statement 
  Committed transaction and closed connection 
  Database shut down normally 
  SimpleApp finished 

  Operating procedures will be generated in the current directory 

  •   DerbyDB (directory) 
      This directory constitutes a derbyDB database directory. You can not change the document in any directory. 
  •   DerbyDB \ log (directory) 
      This directory is a database kept the transaction log. 
  •   DerbyDB \ seg0 (directory) 
      This directory is a database derbyDB preserve data 
  •   DerbyDB \ service.properties 
      An internal document, preserve some configuration parameters 
  •   Derby.LOG 
      Log files 

  Second, how in the server environment running this example 

  (1) start Derby Network Server 

  Derbynet.jar will be copied to the c: \ java \ jar, run.bat document read: 
  Set CLASSPATH = c: \ java \ jar \ derby.jar c: \ java \ jar \ derbynet.jar; CLASSPATH%% 

  Opened a new DOS command line window, this window activated Derby Network Server, such as: 

  C: \ java> run.bat 

  C: \ java> set CLASSPATH = c: \ java \ jar \ derby.jar c: \ java \ jar \ derbynet.jar; 

  C: \ java> java org.apache.derby.drda.NetworkServerControl start 
  In 1527 for the server port to accept connections. 

  (2) with Derby client mode operation of this procedure: 

  Derbyclient.jar will be copied to the c: \ java \ jar, run.bat document read: 
  Set CLASSPATH = c: \ java \ jar \ derbyclient.jar; CLASSPATH%% 

  Opened a new DOS command line window, and then to Derby client mode activated SimpleApp 

  C: \ java> run.bat 
  C: \ java> set CLASSPATH = c: \ java \ jar \ derbyclient.jar; 

  C: \ java> java SimpleApp derbyclient 
  SimpleApp starting in derbyclient mode. 
  Loaded the appropriate driver. 
  Connected to and created database derbyDB 
  Created table derbyDB 
  Inserted 1956 Webster 
  Inserted 1910 Union 
  Updated 1956 Webster to 180 Grand 
  Updated 180 Grand to 300 Lakeshore 
  Verified the rows 
  Dropped table derbyDB 
  Closed result set and statement 
  Committed transaction and closed connection 
  SimpleApp finished 

  (3) the use of IBM DB2 JDBC Universal Driver running this example 

  10.0 in the Derby version of IBM DB2 JDBC Universal Driver Derby Network Server is the only client-driven. Driver needs from IBM's website (IBM DB2 JDBC Universal Driver, for Apache Derby Network Server). From 10.1, Derby network client driver together with the distribution of Derby, and is recommended by the client-driven process, but also to support DB2 Universal Driver and the same can be used like the following: 

  Download db2jcc.jar and db2jcc_license_c.jar, in the c: \ java \ jar directory, run.bat with the following: 
  Set CLASSPATH = c: \ java \ jar \ db2jcc.jar c: \ java \ jar \ db2jcc_license_c.jar; CLASSPATH%% 

  C: \ java> run.bat 

  C: \ java> set CLASSPATH = c: \ java \ jar \ db2jcc.jar c: \ java \ jar \ db2jcc_license_c.jarr; 

  C: \ java> java SimpleApp jccjdbcclient 

  A successful run produces the following output: 
  SimpleApp starting in jccjdbc mode. 
  Loaded the appropriate driver. 
  Connected to and created database derbyDB 
  Created table derbyDB 
  Inserted 1956 Webster 
  Inserted 1910 Union 
  Updated 1956 Webster to 180 Grand 
  Updated 180 Grand to 300 Lakeshore 
  Verified the rows 
  Dropped table derbyDB 
  Closed result set and statement 
  Committed transaction and closed connection 
  SimpleApp finished </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply