File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method and classes

 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic