home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************************************************************
- * *
- * *
- * *
- * Class name: dbFunctions() *
- * Purpose: This class was designed to encapsulate database navigation and related methods. *
- * *
- * Imports: java.awt.* *
- * symjava.sql.* *
- * java.util.Properties *
- * *
- * Methods include: private boolean establishDBConnection() *
- * public String getNextID() *
- * public boolean goFirstRecord(String, String, String) *
- * public boolean goLastRecord(String, String) *
- * public void gotoRecord(TextField[], Checkbox[], List[], Button[]) *
- * public int idLookup(String, String) *
- * public int insertNewValues(TextField[], Checkbox[], List[], Button[]) *
- * public boolean nextRecord(ResultSet) *
- * public void populateMgrList(int, List) *
- * public boolean populateScreen(TextField[], Checkbox[], List[], TextField ltfStatusBar) *
- * private ResultSet sendSQLStatement(String, Statement) *
- * public int updateRecord(TextField[], Checkbox[], List[]) *
- * *
- * *
- * Variable naming convention: Variable beginning with lower case g indicate global variables *
- * First letter of variable indicates the type of object (ie. String svar, boolean bvar) *
- * *
- * Additional Notes: *
- * THIS SOFTWARE HAS BEEN COMPILED AND EXECUTED SUCCESSFULLY IN SPECIFIC SYMANTEC *
- * ENVIRONMENTS, AND IS BEING PROVIDED ONLY AS SAMPLE CODE. *
- * *
- * SYMANTEC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, *
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF *
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SYMANTEC SHALL NOT *
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING *
- * THIS SOFTWARE OR ITS DERIVATIVES. *
- * *
- * Please see the Sample Code Configuration item in the Read Me file for additional *
- * information about the sample database used in this code. *
- * *
- * Copyright (c) 1996 Symantec. All Rights Reserved. *
- * *
- **********************************************************************************************************************/
-
- import java.awt.*;
- import symjava.sql.*;
- import java.util.Properties;
-
- class dbFunctions {
- // DB engine connection variables...
- Driver dvrdbDriver;
- Connection conserverConnection;
-
- // Global ResultSets and Statements
- Statement gstemployee = null, gstdescSelect = null;
- PreparedStatement gpsdept = null, gpsmgr = null;
- ResultSet grsemployee;
-
- // Global Misc. variables defined
- int girecordCount = 0, gicurrentRecord = 0;
- String gssearchClause = new String();
- String gsand = new String();
- String gswhere = new String();
- boolean gbexecuteSearch = false, gbInsert = false;
-
- public dbFunctions() {
- if (!establishDBConnection())
- System.out.println("Failed to establish connection! Check status of server, server URL, and ODBC DSN configuration.");
- else
- if (!goFirstRecord(gssearchClause, gswhere, gsand))
- System.out.println("Failed retrieving first record on startup");
- }
-
- /* Delete method intentionally omitted due to referential integrity contraints that may be imposed on the engine.
- If included, such a method might look similiar to the following:
-
- public boolean deleteRecord(TextField tfempId) {
- Statement stdelete;
- ResultSet rsdelete;
- try {
- stdelete = conserverConnection.createStatement();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- String sdelete = new String("Delete from employee where emp_id = '" + tfempId.getText() + "'");
- rsdelete = sendSQLStatement(sdelete, stdelete);
- if (rsdelete.equals(null)) {
- System.out.println("Delete select statement failed!");
- return false;
- }
- else {
- if (!nextRecord(rsdelete)){
- System.out.println("Failure selecting record to delete!");
- return false;
- }
- try {
- stdelete.executeUpdate(sdelete);
- stdelete.close();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- return true;
- }
- }
- */
-
- private boolean establishDBConnection() {
- String serverURL = "jdbc:dbaw://localhost:8889/watcom/SQL Anywhere 5.0 Sample/SQL Anywhere";
- String dbawDriver = "symantec.itools.db.jdbc.Driver";
- Properties pprops = new Properties();
- pprops.put("user","dba");
- pprops.put("password","sql");
- try {
- dvrdbDriver = (Driver)Class.forName(dbawDriver).newInstance();
- conserverConnection = dvrdbDriver.connect(serverURL, pprops);
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- return false;
- }
- catch (IllegalAccessException e) {
- System.out.println(e.getMessage());
- return false;
- }
- catch (InstantiationException e) {
- System.out.println(e.getMessage());
- return false;
- }
- // Initialize regular and prepared statements....
- try {
- gstemployee = conserverConnection.createStatement();
- gpsdept = conserverConnection.prepareStatement("Select dept_name from department order by dept_name");
- gpsmgr = conserverConnection.prepareStatement("Select emp_fname, emp_lname from employee where dept_id = ? order by emp_lname");
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- return true;
- }
-
- public String getNextID() {
- Statement stnextId = null;
- int iempId = 0;
- try {
- stnextId = conserverConnection.createStatement();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return null;
- }
- String sdescSelect = new String("Select emp_id from employee order by emp_id desc");
- ResultSet rsemployee = sendSQLStatement(sdescSelect, stnextId);
- if (rsemployee.equals(null)) {
- System.out.println("Descending Select statement failed!");
- return null;
- }
- else {
- if (!nextRecord(rsemployee)) {
- System.out.println("Failure finding last record!");
- return null;
- }
- }
- try {
- iempId = rsemployee.getInt(1);
- stnextId.close();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return null;
- }
- String sempId = Integer.toString(iempId + 1);
- return sempId;
- }
-
- public boolean goFirstRecord(String ssearchClause, String swhere, String sand) {
- Statement stcount = null;
- String semployee = new String();
- try {
- stcount = conserverConnection.createStatement();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- String sselectCount = new String("Select count(e1.emp_id) from employee e1 " + swhere + " " + ssearchClause);
- ResultSet rscount = sendSQLStatement(sselectCount, stcount);
- if (rscount.equals(null)) {
- System.out.println("Count statement failed!");
- return false;
- }
- else {
- if (!nextRecord(rscount)) {
- System.out.println("Failure counting records!");
- return false;
- }
- else {
- try {
- girecordCount = rscount.getInt(1);
- stcount.close();
- gicurrentRecord = 1;
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- }
- semployee = "Select e1.emp_id, e1.emp_lname, e1.emp_fname, e1.street, e1.city, e1.state, e1.zip_code, e1.phone, " +
- "e1.ss_number, e1.status, e1.sex, e1.salary, e1.bene_health_ins, e1.bene_life_ins, e1.bene_day_care, " +
- "e1.start_date, e1.termination_date, e1.dept_id, department.dept_name, e2.emp_lname, e2.emp_fname " +
- "from employee e1, department, employee e2 where e1.dept_id = department.dept_id and e1.manager_id = e2.emp_id " +
- sand + " " + ssearchClause + " order by e1.emp_id";
- grsemployee = sendSQLStatement(semployee, gstemployee);
- if (grsemployee.equals(null)) {
- System.out.println("Initial Select statement failed!");
- return false;
- }
- else {
- if (!nextRecord(grsemployee)) {
- System.out.println("Failure finding first record!");
- return false;
- }
- }
- return true;
- }
- }
-
- public boolean goLastRecord(String ssearchClause, String sand) {
- String sdescEmployee = new String();
- sdescEmployee = "Select e1.emp_id, e1.emp_lname, e1.emp_fname, e1.street, e1.city, e1.state, e1.zip_code, e1.phone, " +
- "e1.ss_number, e1.status, e1.sex, e1.salary, e1.bene_health_ins, e1.bene_life_ins, e1.bene_day_care, " +
- "e1.start_date, e1.termination_date, e1.dept_id, department.dept_name, e2.emp_lname, e2.emp_fname " +
- "from employee e1, department, employee e2 where e1.dept_id = department.dept_id and e1.manager_id = e2.emp_id " +
- sand + " " + ssearchClause + " order by e1.emp_id desc";
- grsemployee = sendSQLStatement(sdescEmployee, gstemployee);
- if (grsemployee.equals(null)) {
- System.out.println("Descending Select statement failed!");
- return false;
- }
- else {
- if (!nextRecord(grsemployee)) {
- System.out.println("Failure finding first descending record!");
- return false;
- }
- }
- gicurrentRecord = girecordCount;
- return true;
- }
-
- public void gotoRecord(TextField[] tfList, Checkbox[] cbList, List[] lList, Button[] bList) {
- int imaxLoops, iloopCnt = 0;
- if (!gbexecuteSearch) {
- imaxLoops = tfList.length;
- for(iloopCnt = 2; iloopCnt < imaxLoops; iloopCnt++)
- tfList[iloopCnt].setEditable(false);
- imaxLoops = lList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++)
- lList[iloopCnt].disable();
- imaxLoops = cbList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++)
- cbList[iloopCnt].disable();
- imaxLoops = bList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++) {
- if (iloopCnt != 4) //The Goto button...
- bList[iloopCnt].disable();
- else
- bList[iloopCnt].setLabel("Execute");
- }
- tfList[1].requestFocus();
- gbexecuteSearch = true;
- }
- else {
- gsand = "and";
- gswhere = "where";
- if (tfList[1].getText().indexOf("%") == -1)
- gssearchClause = new String("e1.emp_lname = '" + tfList[1].getText() + "'");
- else
- gssearchClause = new String("e1.emp_lname like '" + tfList[1].getText() + "'");
- if (!goFirstRecord(gssearchClause, gswhere, gsand)) {
- System.out.println("Failed retrieving first record of result set");
- return;
- }
- if (girecordCount == 0) {
- gbexecuteSearch = true;
- return;
- }
- imaxLoops = tfList.length;
- for(iloopCnt = 2; iloopCnt < imaxLoops; iloopCnt++)
- tfList[iloopCnt].setEditable(true);
- imaxLoops = lList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++)
- lList[iloopCnt].enable();
- imaxLoops = cbList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++)
- cbList[iloopCnt].enable();
- imaxLoops = bList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++) {
- if (iloopCnt != 4) //The Goto button...
- bList[iloopCnt].enable();
- else
- bList[iloopCnt].setLabel("Search");
- }
- gbexecuteSearch = false;
- }
- }
-
- public int idLookup(String deptName, String deptmgr) {
- String sUpdate = new String();
- int ideptId = 0;
- Statement stUpdate = null;
- ResultSet rsUpdate = null;
- try {
- stUpdate = conserverConnection.createStatement();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return 0;
- }
- if (deptmgr.equals("dept"))
- sUpdate = new String("Select dept_id from department where dept_name = '" + deptName + "'");
- else
- sUpdate = new String("Select emp_id from employee where emp_fname || ' ' || emp_lname = '" + deptName + "'");
- rsUpdate = sendSQLStatement(sUpdate, stUpdate);
- if (rsUpdate.equals(null)) {
- System.out.println("Department lookup failed!");
- return 0;
- }
- else {
- if (!nextRecord(rsUpdate)){
- System.out.println("Failure selecting department record!");
- return 0;
- }
- try {
- ideptId = rsUpdate.getInt(1);
- stUpdate.close();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- return ideptId;
- }
-
- public int insertNewValues(TextField[] tfList, Checkbox[] cbList, List[] lList, Button[] bList) {
- Statement stinsert = null;
- int irowsInserted = 0, iloopCnt, imaxLoops, ideptId, imgrId;
- String sinsertStatement = new String();
- String stermDate = new String();
- String dept = new String();
- char chealth, clife, cday_care;
- if (!cbList[0].getState())
- chealth = 'N';
- else
- chealth = 'Y';
- if (!cbList[1].getState())
- clife = 'N';
- else
- clife = 'Y';
- if (!cbList[2].getState())
- cday_care = 'N';
- else
- cday_care = 'Y';
- if (tfList[13].getText().equals(""))
- stermDate = null;
- else
- stermDate = "'" + tfList[13].getText() + "'";
- dept = "dept";
- ideptId = idLookup(lList[0].getSelectedItem(), dept);
- if (ideptId == 0)
- irowsInserted = 0;
- dept = "";
- imgrId = idLookup(lList[1].getSelectedItem(), dept);
- if (imgrId == 0)
- irowsInserted = 0;
- sinsertStatement = "Insert into employee " +
- "(emp_id, emp_lname, emp_fname, street, city, state, zip_code, phone, " +
- "ss_number, status, sex, salary, bene_health_ins, bene_life_ins, bene_day_care, " +
- "start_date, termination_date, dept_id, manager_id) " +
- "values " +
- "('" + tfList[0].getText() + "', " +
- "'" + tfList[1].getText() + "', " +
- "'" + tfList[2].getText() + "', " +
- "'" + tfList[3].getText() + "', " +
- "'" + tfList[4].getText() + "', " +
- "'" + tfList[5].getText() + "', " +
- "'" + tfList[6].getText() + "', " +
- "'" + tfList[7].getText() + "', " +
- "'" + tfList[8].getText() + "', " +
- "'" + tfList[9].getText() + "', " +
- "'" + tfList[10].getText() + "', " +
- "'" + tfList[11].getText() + "', " +
- "'" + chealth + "', " +
- "'" + clife + "', " +
- "'" + cday_care + "', " +
- "'" + tfList[12].getText() + "', " +
- stermDate + ", '" + ideptId + "', '" + imgrId + "')";
- try {
- stinsert = conserverConnection.createStatement();
- irowsInserted = stinsert.executeUpdate(sinsertStatement);
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return 0;
- }
- imaxLoops = bList.length;
- iloopCnt = 0;
- for(iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++) {
- if (iloopCnt != 5) //The new_insert button...
- bList[iloopCnt].enable();
- else
- bList[iloopCnt].setLabel("New");
- }
- girecordCount++;
- gicurrentRecord = girecordCount;
- try {
- stinsert.close();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- irowsInserted = 0;
- }
- return irowsInserted;
- }
-
- public boolean nextRecord(ResultSet rsTemp) {
- try {
- rsTemp.next();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- return true;
- }
-
- public void populateMgrList(int deptId, List mgrNames) {
- ResultSet rsmgr = null;
- try {
- gpsmgr.setInt(1, deptId);
- rsmgr = gpsmgr.executeQuery();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- if (rsmgr.equals(null)) {
- System.out.println("Manager lookup failed!");
- }
- else {
- String smgrFromTable = new String();
- String smgrName = new String();
- try {
- smgrName = grsemployee.getString(21) + " " + grsemployee.getString(20);
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- int iindex = 0;
- try {
- while (nextRecord(rsmgr)) {
- smgrFromTable = rsmgr.getString("emp_fname") + " " + rsmgr.getString("emp_lname");
- mgrNames.addItem(smgrFromTable);
- if (smgrFromTable.equals(smgrName))
- mgrNames.select(iindex);
- iindex++;
- }
- }
- catch (SQLException e) {
- }
- }
- }
-
- public boolean populateScreen(TextField[] tfList, Checkbox[] cbList, List[] lList, TextField ltfStatusBar) {
- int imaxLoops = 0, iloopCnt = 0, iempId = 0, ilistLoop = 0;
- String stestVar, sstatusBarMessage;
- Character vchar = new Character('Y');
- Date dtTemp;
- Number nnumTemp;
- try {
- iempId = grsemployee.getInt("emp_id");
- tfList[0].setText(Integer.toString(iempId));
- imaxLoops = 11;
- for (iloopCnt = 1; iloopCnt < imaxLoops; iloopCnt++)
- tfList[iloopCnt].setText(grsemployee.getString(iloopCnt+1));
- int icolumnOffset = 13;
- imaxLoops = cbList.length;
- for (iloopCnt = 0; iloopCnt < imaxLoops; iloopCnt++) {
- stestVar = grsemployee.getString(iloopCnt+icolumnOffset);
- if (stestVar.equals(vchar.toString()))
- cbList[iloopCnt].setState(true);
- else
- cbList[iloopCnt].setState(false);
- }
- nnumTemp = grsemployee.getBigDecimal("salary", 2);
- tfList[11].setText("$" + nnumTemp.toString());
- dtTemp = grsemployee.getDate("start_date");
- tfList[12].setText(dtTemp.toString());
- dtTemp = grsemployee.getDate("termination_date");
- if (!grsemployee.wasNull())
- tfList[13].setText(dtTemp.toString());
- else
- tfList[13].setText("None");
-
- // Populate department name list
- ResultSet rsdept = null;
- try {
- rsdept = gpsdept.executeQuery();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- String sdeptFromTable = new String();
- String sdeptName = new String(grsemployee.getString("dept_name"));
- if (rsdept.equals(null)) {
- System.out.println("Department lookup failed!");
- return false;
- }
- else {
- lList[0].clear();
- lList[1].clear();
- int iindex = 0;
- try {
- while (nextRecord(rsdept)) {
- sdeptFromTable = rsdept.getString("dept_name");
- lList[0].addItem(sdeptFromTable);
- if (sdeptFromTable.equals(sdeptName))
- lList[0].select(iindex);
- iindex++;
- }
- }
- catch (SQLException e) {
- }
- try {
- populateMgrList(grsemployee.getInt("dept_id"), lList[1]);
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- // End
-
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return false;
- }
- sstatusBarMessage = "Record " + gicurrentRecord + " of " + girecordCount;
- ltfStatusBar.setText(sstatusBarMessage);
- return true;
- }
-
- private ResultSet sendSQLStatement(String sqlStatement, Statement stgeneric) {
- ResultSet rsgeneric;
- try {
- rsgeneric = stgeneric.executeQuery(sqlStatement);
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return null;
- }
- return rsgeneric;
- }
-
- public int updateRecord(TextField[] tfList, Checkbox[] cbList, List[] lList) {
- Statement stupdate = null;
- ResultSet rsupdate = null;
- char chealth, clife, cday_care;
- int irowsUpdated = 0, ideptId = 0, imgrId = 0;
- Integer iempId;
- String supdateStatement = new String();
- String stermDate = new String();
- String supdate = new String();
- String ssalary = new String();
- String dept = new String("dept");
- if (!cbList[0].getState())
- chealth = 'N';
- else
- chealth = 'Y';
- if (!cbList[1].getState())
- clife = 'N';
- else
- clife = 'Y';
- if (!cbList[2].getState())
- cday_care = 'N';
- else
- cday_care = 'Y';
- iempId = Integer.valueOf(tfList[0].getText());
- if (tfList[13].getText().equals("None"))
- stermDate = null;
- else
- stermDate = "'" + tfList[13].getText() + "'";
- if (tfList[11].getText().substring(0,1).equals("$"))
- ssalary = tfList[11].getText().substring(1, tfList[11].getText().length());
- else
- ssalary = tfList[11].getText().substring(0, tfList[11].getText().length());
- ideptId = idLookup(lList[0].getSelectedItem(), dept);
- if (ideptId == 0)
- irowsUpdated = 0;
- dept = "";
- imgrId = idLookup(lList[1].getSelectedItem(), dept);
- if (imgrId == 0)
- irowsUpdated = 0;
- try {
- stupdate = conserverConnection.createStatement();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return 0;
- }
- supdate = new String("Select dept_id from department where dept_name = '" + lList[0].getSelectedItem() + "'");
- rsupdate = sendSQLStatement(supdate, stupdate);
- if (rsupdate.equals(null)) {
- System.out.println("Department lookup failed!");
- return 0;
- }
- else {
- if (!nextRecord(rsupdate)){
- System.out.println("Failure selecting department record!");
- return 0;
- }
- }
- supdateStatement = "Update employee " +
- "set emp_lname = '" + tfList[1].getText() + "', " +
- "emp_fname = '" + tfList[2].getText() + "', " +
- "street = '" + tfList[3].getText() + "', " +
- "city = '" + tfList[4].getText() + "', " +
- "state = '" + tfList[5].getText() + "', " +
- "zip_code = '" + tfList[6].getText() + "', " +
- "phone = '" + tfList[7].getText() + "', " +
- "ss_number = '" + tfList[8].getText() + "', " +
- "status = '" + tfList[9].getText() + "', " +
- "sex = '" + tfList[10].getText() + "', " +
- "salary = '" + ssalary + "', " +
- "start_date = '" + tfList[12].getText() + "', " +
- "termination_Date = " + stermDate + ", " +
- "bene_health_ins = '" + chealth + "', " +
- "bene_life_ins = '" + clife + "', " +
- "bene_day_care = '" + cday_care + "', " +
- "dept_id = '" + ideptId + "', " +
- "manager_id = '" + imgrId + "' " +
- "where emp_id = " + iempId.intValue();
- supdate = "Select emp_id, emp_lname, emp_fname, dept_id, street, city, state, zip_code, phone, " +
- "ss_number, status, sex, salary, bene_health_ins, bene_life_ins, bene_day_care, " +
- "start_date, termination_date from employee " +
- "where emp_id = " + iempId.intValue();
- rsupdate = sendSQLStatement(supdate, stupdate);
- if (rsupdate.equals(null)) {
- System.out.println("Update select statement failed!");
- return 0;
- }
- else {
- if (!nextRecord(rsupdate)){
- System.out.println("Failure selecting record to update!");
- return 0;
- }
- try {
- irowsUpdated = stupdate.executeUpdate(supdateStatement);
- stupdate.close();
- }
- catch (SQLException e) {
- System.out.println(e.getMessage());
- return 0;
- }
- return irowsUpdated;
- }
- }
- }
-