wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static method and classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static method and classes" Watch "Static method and classes" New topic
Author

Static method and classes

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I have a JSP which is trying to access one fo my java utility classes. I am using Weblogic 5.1
In my JSP I have the following:
(with page imports, etc.)

Connection conn = JSPUtils.getConnection();
My java class file looks like this:
package com.fedex.util;
import weblogic.db.jdbc.*;
import java.sql.*;
import java.util.*;
import java.text.*;

/**
* A utility class for commonly peformed operations within the JSP pages
*
*/

public class JSPUtils

{

private Connection connection;
private String jdbcClass = "weblogic.jdbc.pool.Driver";
private String jdbcURL = "jdbc:weblogic ool ostgresPool";


/**
* Returns a connection to the database
*
*/

public Connection getConnection()

{
try
{
Class.forName(jdbcClass).newInstance();
connection = DriverManager.getConnection(jdbcURL);
}

catch (Exception e) {

}
return connection;
}
}
But on my jsp i am gettign the error:
non-static method getConnection() cannot be referenced
from a static context.
Why woudl I have to make getConnection() static. I coudl have sworw I have done something like this before w/o having to make anything static.
Is it becasue I have member variables and the last time that I did this I did not?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

This is a pretty basic Java concept.
A reference of type ClassName.myMethod() is a static reference. For it to work, myMethod must be static.
To call instance (non-static) methods, you need an instance of the class. For example:

or some such.
I'm going to shuffle this off to Java in General (beginner) for any further discussion since it's really nothing to do with JSPs.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static method and classes
 
Similar Threads
Using CMP, How to Establish Connection to the Oracle Database?
Static method and classes
Using CMP, How To Establish Connection to the Oracle Database?
does gc still have a chance to do its job if i kill the java process
sl314.util.sql.ConnectionPool