> List of tutorials for developing a Fat-Client

Code for Data-Base-Access (DBA) Object reflecting attributes of the table 'Project' – Java Fat Client Development

* For this document and all references (links) please obey the hints and regulations concerning copyright, disclaimer and trademarks.

  • The owner of this web-site (www.javascout.biz) is not responsible for the content of web-sites linked within this document or other documents of www.javascout.biz.

  • If this document or other documents of this web-site (www.javascout.biz) infringes your rights or you think that rights of others (third parties) are infringed, please inform the author.
    An e-mail can be sent by clicking onto the 'hungry mailbox' in the upper right corner.

Last revision of this document:
2006-07-26

This is document contains the code for Class JS_ErrDB_Project_DBA.
For easy 'cut and paste' ;-)

package js_errdb.dba;

import java.sql.*;


import js_base.dba.JSBS_DBA;

import js_base.structures.JSBS_MinimalParameters;

/**
 
*
 * @author
kurt@javascout.biz
 * @date 2006-07-22
 *
 * @description
 *  Data-Base-Access (DBA) Object for the table 'Project'.
 *
 *  Please see the description at the variables-declaration
.
 *
 *  User-Key: ProjectCode & LanguageCode
.

 
*
 * @change-log
 * when         who               why
 * --------------------------------------------------------
 *
 */

public class JS_ErrDB_Project_DBA extends JSBS_DBA {
/*
 * VARIABLES
 * -------------------- */
/*
 * Project-Code; that is a shortcut for the Project. */
    
public String ProjectCode = "";
/*
 * Language-Code; that is the ISO-CODE for the language the Error-Messages are in. */
    public String LanguageCode = "";
/*
 * Directory where the file with the language-specific error-messages is
 * written out when the file with the XML-structure is generated. */
    public String TargetDirectory = "";
/*
 * CONSTANTS for database-table name, attribute names and attribute lists for SELECT and UPDATE
 * --------------------
 * For a detailed description of each attribute please se under VARIABLES */
/*
 * Name of the database-table */

    public final static
String CONST_TABLE_NAME = "Project";

/* Project-Code */
    public final static
String CONST_ProjectCode = "ProjectCode";

/* Language-Code */
    public final static
String CONST_LanguageCode = "LanguageCode";

/* Directory for the target-file in the XML-structure */
    public final static
String CONST_TargetDirectory = "TargetDirectory";

/*
 * String with all attributes;
 * can be used when accessing database-tables for SELECT and INSERT. */

    final static
String CONST_ALL_ATTRIBUTES_LIST =
        CONST_COMMON_ATTRIBUTES_LIST + ", " +
        CONST_ProjectCode + ", "
+
        CONST_LanguageCode + ", "
+
        
CONST_TargetDirectory
;
/*
 * String with all attributes to be used for UPDATE on database-records. */

    final static
String CONST_ALL_ATTRIBUTES_LIST_FOR_UPDATE =
        "update " + CONST_TABLE_NAME + " set " +
        CONST_COMMON_ATTRIBUTES_LIST_FOR_UPDATE +
        CONST_ProjectCode + " =?, " +
        CONST_LanguageCode + " =?, " +
        CONST_TargetDirectory + " =? "+
        " where "
;

/*
 * String to get all attributes at a SELECT operation. */

    final static
String CONST_SELECT_ALL_ATTRIBUTES =
        "select " +
        CONST_ALL_ATTRIBUTES_LIST +
        " from " + CONST_TABLE_NAME
;

/*
 * METHODS
 * -------------------------- */
/* --------------------------
 * Method to transfer the values from the SQL-ResultSet (a class within the package
 * java.sql that is filled by a SELECT) to the variables of this class. */
    public void getValuesFromSQLResultSet(ResultSet parmSQLResultSet)
    throws SQLException {
/* Use the method from the superclass to transfer values of Common Attributes. */
      try {
        getSQLResultSetForCommonAttributes(parmSQLResultSet);
      }
      catch (SQLException SQLExc) {throw SQLExc;}
/* Transfer the values of table-specific attributes. */
      try {
        this.ProjectCode = parmSQLResultSet.getString(CONST_ProjectCode);
        this.LanguageCode = parmSQLResultSet.getString(CONST_LanguageCode);
        this.TargetDirectory = parmSQLResultSet.getString(CONST_TargetDirectory);
      }
      catch (SQLException SQLExc) {throw SQLExc;}
    }
/* --------------------
 * Method to transfer the values from the variables of this class to the PreparedStatement.
 * PreparedStatement is a class within the package java.sql that contains the SQL-Statement
 * and the new values for the attributes before a INSERT or UPDATE is called. */
    public void
setValuesToSQLStatement(PreparedStatement parmSQLStatement)
    throws SQLException {
/* Use the method from the superclass to transfer values of Common Attributes. */
      try {
        setSQLStatementWithCommonAttributes(parmSQLStatement);
      }
      catch (SQLException SQLExc) {throw SQLExc;}
/* Transfer the values of table-specific attributes. */
      try {
        parmSQLStatement.setString(9, ProjectCode);
        parmSQLStatement.setString(10, LanguageCode);
        parmSQLStatement.setString(11, TargetDirectory);
      }
      catch (SQLException SQLExc) {throw SQLExc;}
    }
/* --------------------
 * Method to INSERT the values in the variables of this object into the database-tables.
 * Prerequisite is, that the DataSetId (Primary Key of the table) was checked before that
 * another record with the same DataSetId does not already exist in the table. */
    
public void insert(Connection parmDBCon) {
/* Build the SQL-Command in a String.
 * The Command is put into an extra String (and not into the prepareStatement method)
 * to see more clearly what the command is. */
      final 
String locstrSQLCommand = "insert into " + CONST_TABLE_NAME + " ("
+
                                       CONST_ALL_ATTRIBUTES_LIST +
                                       ") VALUES ("
+
                                       "?, ?, ?, ?, ?, ?, ?, ?, "
+
                                       "?, ?, ?) ";

/* 'PreparedStatement'; a special class for INSERT and UPDATE operations toward the database. */
      PreparedStatement SQLStatement;
      try {
/* Construct the 'PreparedStatement' and fill it with the SQL-command. */
        SQLStatement = parmDBCon.prepareStatement(locstrSQLCommand);
/* Transfer the attributes from this object to the 'PreparedStatement'. */
        setValuesToSQLStatement(SQLStatement);
/* Perform the database-operation for INSERT. */
        SQLStatement.executeUpdate();
/* Database operation run without an error; report this to the calling method by an empty string. */
        ErrorMsg = "";
      }
      catch (SQLException SQLExc) {
/* An error occured while running the DB-operation; get the textual message. */
        ErrorMsg = SQLExc.getMessage();
      }
    }
/* --------------------
 * Method to UPDATE the values of the database-table-attributes with
 * the values of this DBA object. */
    
public void update(Connection parmDBCon) {
/* Build the SQL-Command in a String.
 * The Command is put into an extra String (and not into the prepareStatement method)
 * to see more clearly what the command is. */
      
final 
String locstrSQLCommand =
CONST_ALL_ATTRIBUTES_LIST_FOR_UPDATE
+
                                      CONST_DataSetID
+ " =?";
/* 'PreparedStatement'; a special class for INSERT and UPDATE operations toward the database. */
      PreparedStatement SQLStatement;
      try {
/* Construct the 'PreparedStatement' and fill it with the SQL-command. */
        SQLStatement = parmDBCon.prepareStatement(locstrSQLCommand);
/* Transfer the attributes from this object to the 'PreparedStatement'. */
        setValuesToSQLStatement(SQLStatement);
/* Transfer the primary key of the table to the 'PreparedStatement'. */
        SQLStatement.setDouble(12, DataSetID);
/* Perform the database-operation for UPDATE. */
        SQLStatement.executeUpdate();
/* Database operation run without an error; report this to the calling method by an empty string. */
        ErrorMsg = "";
      }
      catch (SQLException SQLExc) {
/* An error occured while running the DB-operation; get the textual message. */
        ErrorMsg = SQLExc.getMessage();
      }
    }
/* --------------------
 * Method to read a Data-Set identified by the DataSetID.
 * As this attribute is the primary key of the table, only one record can be found.
 * The return value signals if the requested record was found on the database-table. */
    
public boolean selectByDataSetID(Connection parmDBCon, double parmDataSetID) {
/* Build the SQL-Command in a String.
 * The Command is put into an extra String (and not into the prepareStatement method)
 * to see more clearly what the command is. */
      
final String locstrSQLCommand =
CONST_SELECT_ALL_ATTRIBUTES +
                                      " where " + CONST_DataSetID
+ " =?";
/* 'PreparedStatement'; a special class for operations toward the database. */
      PreparedStatement SQLStatement;
      try {
/* Construct the 'PreparedStatement' and fill it with the SQL-command. */
        SQLStatement = parmDBCon.prepareStatement(locstrSQLCommand);
/* Fill the variable(s) in the 'PreparedStatement'. */
        SQLStatement.setDouble(1, parmDataSetID);
/* Perform the database-operation for SELECT. */
        ResultSet SQLResultSet = SQLStatement.executeQuery();
/* 
 * Verify if a record was found */
        if (!SQLResultSet.next()) {
/* No database-record found; signal to the calling method by the return-value */
          ErrorMsg = "";
          return false;
        }
        
else {
/* Database-record found; transfer the values of the 'PreparedStatement'
 * to the variables of this object. */
          getValuesFromSQLResultSet(SQLResultSet);
/* Database operation run without an error; report this to the calling method by an empty string
 * and the return value. */
          ErrorMsg = "";
          return true;
        }
      }
      catch (SQLException SQLExc) {
/* An error occured while running the DB-operation; get the textual message. */
        ErrorMsg = SQLExc.getMessage();
/* Database operation run with an error; report this to the calling method by the return value. */
        return false;
      }
    }
/* --------------------
 * Method to read a Data-Set identified by the User-Known-Key;.
 * for this object it is the combination of: ProjectCode, LanguageCode.
 * As this combination can occur only once for a certain time, only one record can be found.
 * The return value signals if the requested record was found on the database-table. */
    
public boolean selectByUserKnownKey(Connection parmDBCon, JSBS_MinimalParameters parmMinParm,
                                      String parmProjectCode, String parmLanguageCode
) {
/* Build the SQL-Command in a String.
 * The Command is put into an extra String (and not into the prepareStatement method)
 * to see more clearly what the command is. */
      
final String locstrSQLCommand = CONST_SELECT_ALL_ATTRIBUTES +
                                      " where " + CONST_ProjectCode
+ " =?" +
                                      " and " + CONST_LanguageCode + " =?" +
                                      " and " + CONST_ValidTill + " >=?" +
                                      " order by " + CONST_ValidFrom + ", " + CONST_ValidTill + " desc";
/* Convert the actual date (in the passed parameters) to the java.sql.Date format. */
      Date locdteWorkDate = new Date(parmMinParm.calWorkDate.getTime().getTime());
/* 'PreparedStatement'; a special class for operations toward the database. */
      PreparedStatement SQLStatement;
      try {
/* Construct the 'PreparedStatement' and fill it with the SQL-command. */
        SQLStatement = parmDBCon.prepareStatement(locstrSQLCommand);
/* Fill the variable(s) in the 'PreparedStatement'. */
        SQLStatement.setString(1, parmProjectCode);
        SQLStatement.setString(2, parmLanguageCode);
        SQLStatement.setDate(3, locdteWorkDate);
/* Perform the database-operation for SELECT. */
        ResultSet SQLResultSet = SQLStatement.executeQuery();
/* 
 * Verify if a record was found */
        if (!SQLResultSet.next()) {
/* No database-record found; signal to the calling method by the return-value */
          ErrorMsg = "";
          return false;
        }
        
else {
/* Database-record found; transfer the values of the 'PreparedStatement'
 * to the variables of this object. */
          getValuesFromSQLResultSet(SQLResultSet);
/* 
 * Verify that the validity of the found dataset does not start in the future. */
          if (this.ValidFrom.getTime() > locdteWorkDate.getTime()) {
/* Found dataset not already valid; return a status of 'Not Found'. */
            ErrorMsg = "";
            return false;
          }
/* Database operation run without an error; report this to the calling method by an empty string
 * and the return value. */
          ErrorMsg = "";
          return true;
        }
      }
      catch (SQLException SQLExc) {
/* An error occured while running the DB-operation; get the textual message. */
        ErrorMsg = SQLExc.getMessage();
/* Database operation run with an error; report this to the calling method by the return value. */
        return false;
      }
    }
}