Caro0101
asked on
Static classes - Urgent
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)(det pInfoVecto r.elementA t(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(que ry);
try {
while (rset.next()) {
DepartmentListObject deptObject = new DepartmentListObject();
String deptartment = rset.getString("Dept_List" );
String deptCode = rset.getString("Dept_Code" );
deptObject.setDept(deptart ment);
deptObject.setDeptCode(dep tCode);
detailVector.addElement(de ptObject);
}
}
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.registerDriv er(new oracle.jdbc.driver.OracleD river());
Connection connection = DriverManager.getConnectio nthe 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/M M/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(qu ery);
}
catch (Exception e){
System.out.println("Update Database method failed");
e.printStackTrace();
}
statement.close();
statement = connection.createStatement ();
query = "SELECT SRS_FORMS_SEQ.currval as recid FROM DUAL";
ResultSet rset = statement.executeQuery(que ry);
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
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)(det
detpListVector.addElement(
}
}
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(que
try {
while (rset.next()) {
DepartmentListObject deptObject = new DepartmentListObject();
String deptartment = rset.getString("Dept_List"
String deptCode = rset.getString("Dept_Code"
deptObject.setDept(deptart
deptObject.setDeptCode(dep
detailVector.addElement(de
}
}
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.registerDriv
Connection connection = DriverManager.getConnectio
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+"'
+ 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(qu
}
catch (Exception e){
System.out.println("Update
e.printStackTrace();
}
statement.close();
statement = connection.createStatement
query = "SELECT SRS_FORMS_SEQ.currval as recid FROM DUAL";
ResultSet rset = statement.executeQuery(que
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
Instead of:
private Connection connection = null;
declare
private static Connection connection = null;
private Connection connection = null;
declare
private static Connection connection = null;
Didn't see that post before I posted sorry. Please check out guidelines for posting answers as has been suggested to you before keith.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dear keithlong
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.
See: https://www.experts-exchange.com/jsp/communityNews.jsp
Paragraph: Site Update for Wednesday, November 06, 2002
By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.
This Q hasn't been active lately, thus I've posted it in the CS Cleanup topic area.
I'll return to this Q in seven days to see if any coment has been added, if not then it's my intention that this question is:
-Answered by: objects
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
Thanks !
DigitalXtreme
Community Support Moderator
Experts Exchange
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.
See: https://www.experts-exchange.com/jsp/communityNews.jsp
Paragraph: Site Update for Wednesday, November 06, 2002
By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.
This Q hasn't been active lately, thus I've posted it in the CS Cleanup topic area.
I'll return to this Q in seven days to see if any coment has been added, if not then it's my intention that this question is:
-Answered by: objects
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
Thanks !
DigitalXtreme
Community Support Moderator
Experts Exchange
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
To be PAQ'ed and points refunded
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
guanwz
I will leave a recommendation in the Cleanup topic area that this question is:
To be PAQ'ed and points refunded
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
guanwz
i reckon i provided a valid solution.
Looks like KeithLong was in first
Sorry guanz - i don't support the refunding of points [almost always actually as it encourages bad questioner practices, but certainly not here]
Sorry guanz - i don't support the refunding of points [almost always actually as it encourages bad questioner practices, but certainly not here]
Seems like i'm getting false updates ?!
> Looks like KeithLong was in first
I provided corrected code, and DigitalXtreme recomended my comment be accepted.
I provided corrected code, and DigitalXtreme recomended my comment be accepted.
>>I provided corrected code
Not absolutely sure what you mean here...
Not absolutely sure what you mean here...
Points awarded PAQd.
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
You need to make the variables static and the methods used by other static methods static. Static methods can not access member variables of a Class instance.
i.e.
private static Connection connection = null;
private static Statement statement = null;
private static void closeConnection() throws SQLException
Still doesn't compile due to typos on 107 and I don't have the DepartmentListObject or the Oracle driver on my machine
Regards
Keith Long
Technical Director [CEO]
www.technogenius.co.uk