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());
}
}

}
 
OK; so that is the class you told me earlier to try but before I didn't have the correct Strings now it's amended and still doesn't compile: cannot resolve symbol variable application - I thought this was correct?

package Mypackage;
import java.sql.*;

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

 
Thats not the code I gave you ...

Right, bin all of it, and have your class as :

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

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


    	}

}


and your JSP as :
Code:
String url = "jdbc:mysql://localhost:3306/intranet";
String user = "fred";
String password = "bloggs";
Connection c = ConnectionManager.getConnection(url, user, password);
PreparedStatement prep = c.prepareStatement("SELECT * FROM staff");
ResultSet rs = prep.executeQuery();

Try this first, and then if that works, you can add in these lines to replace the static url, user, password values in the JSP :


String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = applicaton.getInitParameter("dbUser");
 
I wish

now it compiles but when I run it I get this Apache error:

javax.servlet.ServletException: Mypackage.ConnectionManager.getConnection(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection;
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.jsp.staff_jsp._jspService(staff_jsp.java:256)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

 
I didn't think it would be that difficult, any more ideas?
 
Hi,

Its a small suggestion, Before calling the DriverManager.getConnection() make sure what
all drivers are loaded. May be this will help you out.

Call the below function and print the values to make sure you have the proper driver.

Code:
 List drivers = Collections.list(DriverManager.getDrivers());
    for (int i=0; i<drivers.size(); i++) {
        Driver driver = (Driver)drivers.get(i);
    
        // Get name of driver
        String name = driver.getClass().getName();
	System.out.println("Driver Name |" + name + "|");
            
    }

Cheers
Venu
 
what's your full stack trace ? (Look in the logs) There should be a root cause ...
 
Hi,

thanks so much for your help, I am very grateful and it works now with the last piece of code that Sedj gave me; I think Tomcat got confused as it didn't record the last compilation but I restarted it last night and now it works fine. The administrator told me that with Tomcat 5.0 you don't need to restart anymore but it looks like you do sometimes!

Also for information, apparently you cannot use this:
String url = application.getInitParameter("dbUrl");
in a class, just in a jsp/servlet.

So now the class is working but in fact in my jsp page the only piece of code that I don't need now is:
Class.forName("com.mysql.jdbc.Driver").newInstance();

the rest is the almost the same in my jsp page:
String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = application.getInitParameter("dbUser");
Connection C = ConnectionManager.getConnection(url, user, pwd);

is this good programming?

Many thanks again
Vero
 
Seems fine to me ...

Glad its finally working !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top