Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java class

Status
Not open for further replies.

sensory21

Programmer
Jan 27, 2004
79
GB
Hi all,

can you see something wrong with my class as when I tried to compile it, I got the message error:
package javax.servlet does not exist?
Thanks for your time.

package Mypackage;
import java.util.*;
import java.sql.*;
import javax.servlet.UnavailableException;

public class ConnectionManager {

public ConnectionManager(){

}
static Connection getConnection(){

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "dbUrl";
String pwd = "dbPwd";
String user = "dbUser";
Connection conn = DriverManager.getConnection(url,pwd,user);
}
catch (Exception e){
throw new UnavailableException(e.getMessage());
}
}

}
 
You have not included the javax.servlet package in your CLASSPATH.

Theis package is contained within a file named servlet.jar or servlet-api.jar and can be downloaded from as part of the servlet API or comes with Tomcat / Resin etc.

To be honest though, I would not throw this exception from that code - bacause it is not really a servlet API exception - I would code it like such :

Code:
public class ConnectionManager {

    public ConnectionManager(){
            
        }
        static Connection getConnection() throws SQLException {
            
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                String url = "dbUrl";
                String pwd = "dbPwd";
                String user = "dbUser";
                Connection conn = DriverManager.getConnection(url,pwd,user);
    
    
}
 
Thanks very much Sedj for your help

I have so much to learn to write java classes!
 
That's the amended class: but now I've got 3 errors
- missing return statement

-ConnectionManager.java:13: unreported exception java.lang.ClassNotFoundException
; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver").newInstance();

-ConnectionManager.java:13: unreported exception java.lang.ClassNotFoundException
; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver").newInstance();

package Mypackage;
import java.lang.*;
import java.sql.*;
import java.io.*;

public class ConnectionManager {

public ConnectionManager(){
//default constructor
}
static Connection getConnection() throws SQLException
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "dbUrl";
String pwd = "dbPwd";
String user = "dbUser";
Connection conn = DriverManager.getConnection(url,pwd,user);
}
}
 
Try :

Code:
import java.sql.*;
public class ConnectionManager {

    public ConnectionManager(){}
        static Connection getConnection() throws SQLException {
            	try {
            	    Class.forName("com.mysql.jdbc.Driver");
            	    String url = "dbUrl";
            	    String pwd = "dbPwd";
            	    String user = "dbUser";
            	    return DriverManager.getConnection(url,pwd,user);
            	} catch (Exception e) {
            	    e.printStackTrace(System.err);
            	    throw new SQLException(e.toString());
            	}


	}
}
 
Dear Sedj, you are great, the class compile and Im learning good java too; but another question: now that Im using that class in my jsp file I get this Apache errors:

-cannot resolve symbol symbol : method prepareStatement (java.lang.String)
location: class Mypackage.ConnectionManager
PreparedStatement prep = C.prepareStatement("SELECT * FROM staff");

-cannot resolve symbol symbol : method close ()
location: class Mypackage.ConnectionManager
C.close();
^


That's what in my jsp file:

1-ConnectionManager C = new ConnectionManager();
2-PreparedStatement prep = C.prepareStatement("SELECT * FROM staff");
3-ResultSet RS = prep.executeQuery();

I thought that I could use the class as I have done in the line 1? or do I have to have the PreparedStatement in the class?
I would like to be able to use the connection class just to connect to mysql?

Many thanks
 
Try :

Code:
Connection c = ConnectionManager.getConnection();
PreparedStatement prep = c.prepareStatement("SELECT * FROM staff");
ResultSet rs = prep.executeQuery();

You do not have to instantiate a new ConnectionManager class because the getConnection() method is static.
 
Sedj, I do understand about
Connection c =
instead of
ConnectionManager c =

then I got this error:

getConnection() is not public in Mypackage.ConnectionManager; cannot be accessed from outside package
Connection C = ConnectionManager.getConnection();

So I wrote:
public static Connection getConnection() throws SQLException

but then I got this error:
javax.servlet.ServletException: java.sql.SQLException: No suitable driver

??
Cheers
Vero
 
Have you added the mysql driver to TOMCAT_HOME/common/lib ?
 
yes the connection was working fine when it was in the jsp file as:

/*Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = application.getInitParameter("dbUser");
Connection C = DriverManager.getConnection(url,pwd,user);
*/

but then I want to improve my programming skills and create classes instead of having all these codes in my jsp files
 
The problem is that your dbUrl String should be something like :

"jdbc:mysql://localhost:3306/test"

not :

"dbUrl
 
dbUrl refers to the value in the
WEB-INF>web.xml
<param-value>jdbc:mysql://localhost/intranet</param-value>

it's why I had this:
String url = application.getInitParameter("dbUrl");

but do I have to change it in the class as you said:
String url = jdbc:mysql://localhost/intranet;

or could I have this in the class?
String url = application.getInitParameter("dbUrl");
 
Of course you can have it as :

String url = application.getInitParameter("dbUrl");

- its only a String - but in your code that you originally posted it was down as :

String url = "dbUrl";

which is not going to work ...
 
You are right but now it doesn't compile

ConnectionManager.java:15: cannot resolve symbol
symbol : variable application

 
I've now tried
String url = "jdbc:mysql://localhost/intranet";

and still getting the error:

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\intranet\WEB-INFclasses\Mypackage>javac ConnectionManager.java

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\intranet\WEB-INFclasses\Mypackage>java ConnectionManager
Exception in thread "main" java.lang.NoClassDefFoundError: ConnectionManager (wr
ong name: Mypackage/ConnectionManager)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

when you asked me earlier if I had added the mysql driver to TOMCAT_HOME/common/lib ?
is it a jar file?
Our administrator has changed from Tomcat 4.1 to Tomcat 5.0 and I guess he kept the same settings as we all work with mysql, can you give me some more info?
 
Yes the driver is in a jar file - mmsql-*.jar or something like that.

To fix your above error, type :

cd "C:\Program Files\Apache Software Foundation\Tomcat 5.\webapps\intranet\WEB-INF\classes"
java Mypackage.ConnectionManager

(you will need a main method - ie

Code:
    	public static void main(String args[]) throws Exception {
			System.err.println(ConnectionManager.getConnection());
		}
 
Ive found this on java.sun:

String url = getServletConfig().getInitParameter("dbUrl");

and still error message:
cannot resolve symbol symbol : method getServletConfig()

that's the look of my class now:

package Mypackage;
import java.sql.*;
import java.io.*;
import java.text.*;
import javax.servlet.*;

public class ConnectionManager
{
public ConnectionManager()
{
//default constructor
}
public static Connection getConnection() throws SQLException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = getServletConfig().getInitParameter("dbUrl");
String pwd = getServletConfig().getInitParameter("dbPwd");
String user = getServletConfig().getInitParameter("dbUser");
return DriverManager.getConnection(url,pwd,user);
}
catch (Exception e)
{
e.printStackTrace(System.err);
throw new SQLException(e.toString());
}
}
}

it's so difficult, can you see the problem?
 
to answer your previous reply;
this piece of code wouldn't work if I didn't have the mysql driver, would it?
/*Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = application.getInitParameter("dbUser");
Connection C = DriverManager.getConnection(url,pwd,user);
*/
and this works fine! I just want to put it in a class and use this in the jsp file:
Connection C = ConnectionManager.getConnection();

but can't do it!
 
getServletConfig() is a method of HttpServlet - so you cannot use it it a standalone application ... only in a jsp/servlet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top