Click to See Complete Forum and Search --> : Java Q: whats JSP package?


Prince
02-16-2002, 02:18 AM
I am creating a class for JSP. Purpose of this class is to connect to the database and declare some initial variables. So if i want to do connect to DB and run some query, I just pass SQL query and all is done.

But when i run this line (to load JDBC-ODBC drivers):

Class.forName("Sun.jdbc.JdbcOdbcDriver");


its gives me error:
unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown


I am only importing:
import java.sql.*;

I cannot import:
javax.servlet.jsp.*;


Am i importing the wrong methods? or is the JDK that came with Jbuilder6 enterprise is not enterprise JDK?

Prince
02-16-2002, 02:18 AM
Complete Source code of file:

package princems;

import java.sql.*;


public class connectODBC
{
// connectODBC or connectDB ?
public connectODBC()
{
}

public void connectDB(String DBurl, String DBlogin, String DBpass)
{
Class.forName("Sun.jdbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(DBurl, DBlogin, DBpass);
Statement stmt = con.createStatement();
ResultSet rs;
}

public boolean runQuery(String DBquery)
{
return true;
}

public boolean runUpdate(String DBupdate)
{
return true;
}

}

mefisto3
02-17-2002, 06:50 PM
The problem is that the Class.forName throws an exception and you didnt catch it.

so basically you will need something like this:

try
{
Class.forName("");
}
catch (ClassNotFoundException e)
{
System.err.println("Could not load the db driver.");
}

Prince
02-18-2002, 03:21 AM
but how can I load the "Class.forName"?

mefisto3
02-18-2002, 05:38 AM
Ok, i assume u have added the try/catch block to your code - that will take care of the error u were having. Even though u got it during run time, it is really a compilation error, but JSPs are not compiled till run time.

Now, u can load any class you want. I assume that the class u r loading is in a jar file. Even if its not it doesnt matter. U have to add it to a CLASSPATH. There are a number of ways to do it, depending on how u run your application. As u said u use JBuilder6. I am not familiar with ver 6 (i use 4) but i guess u have to go to project properties and add a new library (call it wahtever u want) and point its path to the jar file where your JDBC driver is. JBuilder will then automatically add it to your classpath.

If u use a servlet engine such as tomcat then i would guess u have a dir called WEB-INF under which u should have your JSPs. Just add dir lib under WEB-INF and put the jar file in there. All files in the lib dir are added to classpath when tomcat is started.

Or you can just add the classpath manualy. Just type:
set CLASSPATH=%CLASSPATH%;c:\path to your jar file\file.jar.

Let me know if you have any more problems.