Home    Downloads    Examples    Schema    Anatomy    Releases
 
Example 1

Example 1

The following example gives the flavor of Perseus. In this example a simple UPDATE SQL statement is executed. Connections are managed in the pre-JDBC2 style. The XML document specifies the SQL statement. The following Java code shows how the resulting class is used.

	  <sql-definitions>
	    <update name="UpdatePassword" package="com.x.test">
	      <documentation>
		A simple update statement.  While this is an actual
		SQL UPDATE statement, any non-SELECT statement is
		considered an update for the purposes of Perseus.
	      </documentation>
	      <sql>
		<text>UPDATE Customer SET password=</text>
		<in name="password" type="java.lang.String"/>
		<text> WHERE id=</text>
		<in name="customerId" java-type="long"/>
	      </sql>
	    </update>
	  </sql-definitions>
	

	  package com.x.run;

	  import com.gtsdesign.perseus.PerseusDriverDataSource;
	  import com.gtsdesign.perseus.PerseusConnectionFactory;

	  /**
	      This is the Perseus-generated SQL class.
	  **/
	  import com.x.test.UpdatePassword;

	  public class PasswordUpdater {

	      /**
		  The two lines of code in this method are all it
		  takes to create and use a SQL class.
		  The parameter order is determined by the order of
		  the parameters in the SQL statement.
	      */
	      public void update (String newPassword, long customerId)
	      throw java.sql.SQLException {
		  UpdatePassword updater = new UpdatePassword (
		      newPassword, customerId
		  );
		  try {
		      updater.go ();
		  } catch (java.sql.SQLException e) {
		      e.printStackTrace ();
		  }
	      }
		  
	      public static void main (String[] args) {
		  /*
		      Use the convenience method registerDriver() to
		      register the driver with the JDBC DriverManager.
		  */
		  try {
		    PerseusDriverDataSource.registerDriver ("some.driver.class");
		  } catch (Exception e) {
		      e.printStackTrace ();
		      System.exit(1);
		  }
		    
		  /*
		      We're using a JDBC I connection, so create a
		      PerseusDriverDataSource which presents a
		      DataSource interface for the JDBC I driver.
		  */
		  PerseusDriverDataSource ds = new PerseusDriverDataSource ();

		  /*
		      Register the PerseusDriverDataSource with the
		      PerseusConnectionFactory.  The
		      PerseusConnectionFactory is effectively a
		      singleton, and all Perseus connections are
		      acquired through it (yep, at the moment there
		      can only be one database connection at a
		      time).  The argument may be any object which
		      implements the javax.sql.DataSource interface.
		  */
		  PerseusConnectionFactory.setDataSource (ds);

		  String newPassword = args[0];
		  String customerId = args[1];
		  PasswordUpdater updater = new PasswordUpdater (newPassword, customerId);
		  updater.update (newPassword, customerId);
	      }
	  }