|
-
Java Q: whats JSP package?
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?
-
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;
}
}
-
Reef Shark
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.");
}
-
but how can I load the "Class.forName"?
-
Reef Shark
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|