• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static method and classes

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic