troubleshooting Question

Static classes - Urgent

Avatar of Caro0101
Caro0101Flag for Canada asked on
Java
12 Comments1 Solution242 ViewsLast Modified:
Here is a class :

package srs;
import java.sql.*;
import java.lang.*;
import java.util.Vector;
import java.util.Date;
import java.lang.String;

public class DepartmentConnection
{

  private Vector detailVector = new Vector(5,5);
  private Statement statement = null;
  private Connection connection = null;  
 

  public DepartmentConnection()
  {
  }

 
  /************************************************************************
  **  METHOD: getDepartment
  **
  **  This method pulls the department information vector from the data bean
  **  and rips out the department name from it. It then stores that info into
  **  a new vector.
  **
  **  @param  detpInfoVector       the department information vector
  **  @return Vector               list of department name only
  **  @exception  SQLException     if anything goes wrong
  **************************************************************************/    
  public Vector getDepartment(Vector detpInfoVector) throws SQLException {

    int vectorSize;
    DepartmentListObject deptObject = new DepartmentListObject();
    Vector detpListVector = new Vector(5,5);

    vectorSize = detpInfoVector.size();

    if(!detpInfoVector.isEmpty()){
      for (int i=0; i<(vectorSize); i++) {
        deptObject = (DepartmentListObject)(detpInfoVector.elementAt(i));
        detpListVector.addElement(deptObject.getDept());
      }
    }
  return detpListVector;
  }

  /************************************************************************
  **  METHOD: getDepartmentDetail
  **
  **  This method queries the Dept_Info and pulls all the department information
  **  and stores it in a vector.
  **
  **  @return Vector               list of all department information
  **  @exception  SQLException     if anything goes wrong
  **************************************************************************/
  public Vector getDepartmentDetail() throws SQLException {
    if(connection==null)
      setConnection();
     
   Statement statement = connection.createStatement();
   
    //use NVL to return a blank if the results for the column are null
    String query = "SELECT * from Dept_Info order by Dept_List asc";
    ResultSet rset = statement.executeQuery(query);
    try {
      while (rset.next()) {
        DepartmentListObject deptObject = new DepartmentListObject();
        String deptartment = rset.getString("Dept_List");
        String deptCode = rset.getString("Dept_Code");
        deptObject.setDept(deptartment);
        deptObject.setDeptCode(deptCode);
        detailVector.addElement(deptObject);
      }
    }
    catch (Exception x) {
    }
    finally{
      if (rset != null) {
        rset.close();
      }
      if (statement != null) {
      statement.close();
      }
      if (connection != null) {
        connection.close();
      }
        connection = null;
    }
    //rset.close();
    //statement.close();
    //closeConnection();
    return detailVector;
  }

  /************************************************************************
  **  METHOD: setConnection
  **
  **  This method is used set the connection to the database.
  **
  **  @exception  SQLException     if anything goes wrong
  **************************************************************************/  
  public void setConnection() throws SQLException {
    try{
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection connection = DriverManager.getConnectionthe right parameters); //
    this.connection = connection;  

    }
    catch (SQLException e) {
      //System.out.println(e);
    }
  }


  /************************************************************************
  **  METHOD: closeConnection
  **
  **  This method is used close a connection to the database.
  **
  **  @exception  SQLException     if anything goes wrong
  **************************************************************************/  
  private void closeConnection() throws SQLException {
    if (statement != null) {
      statement.close();
    }
    if (connection != null) {
      connection.close();
    }
    connection = null;
   
  }
  public static int srsFormsInsert(String dateRequired, String dateRequested,
    String givenName, String surname , Integer phone , Integer phoneExtension ,
    String department , String department_code , String email ,
    String frequency , Integer copies , String repeat ,
    String purpose , String criteria , String report_info ,
    String sort_order , String program , String program_comments ) throws SQLException{
   
    if(connection==null)
      setConnection();

    Statement statement = null;
    String query;
    int record_id = 0;

    statement = connection.createStatement();

    query = ("INSERT INTO SRS_Forms (DATE_REQUIRED, DATE_REQUESTED, "
    + "GIVEN_NAME , SURNAME, PHONE ,PHONE_EXTENSION , DEPARTMENT ,DEPARTMENT_CODE , "
    + " EMAIL, FREQUENCY, COPIES, REPEAT, PURPOSE, CRITERIA, REPORT_INFO, SORT_ORDER, "
    + " PROGRAM, PROG_COMMENTS) "
    + " values( to_date('"+dateRequired+"','YYYY/MM/DD'),  " + "to_date('"+dateRequested+"','YYYY/MM/DD'), '"
    + givenName + "', '" + surname + "', '" + phone + "', '" + phoneExtension + "', '"
    + department + "', '"+ department_code + "', '" + email +  "', '"+ frequency
    + "', '"+ copies + "', '"+ repeat + "', '"+ purpose +  "', '"+ criteria
    + "', '"+ report_info + "', '"+ sort_order + "', '"+ program +  "', '"+ program_comments + "')" );

    int numrow=0;
    try{
      numrow = statement.executeUpdate(query);
    }
    catch (Exception e){
      System.out.println("UpdateDatabase method failed");
      e.printStackTrace();
    }
   
    statement.close();
    statement = connection.createStatement();
    query = "SELECT SRS_FORMS_SEQ.currval as recid FROM DUAL";  
    ResultSet rset = statement.executeQuery(query);
    try {
      while (rset.next()) {
        record_id = rset.getInt("recid");
      }
    }
    catch (Exception x) {
    }
    rset.close();
    statement.close();
   
    closeConnection();    
    return record_id;
  }
}

I get this error "Error(351,8): non-static variable connection cannot be referenced from a static context"

Can someone explain why? How would I go about having one setConnection that satisfy both a public & public static method

Thanks
ASKER CERTIFIED SOLUTION
Mick Barry
Java Developer
Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros