File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes a weird thing, please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "a weird thing, please help." Watch "a weird thing, please help." New topic
Author

a weird thing, please help.

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I used a class (Connect) to make connection to a database. Then I use a jsp to call a method (returnTable) of (Connect), which should return an object of ResultSet. The code looks like this:
<%@ page import="Connect" %>
<%@ page import="java.sql.*" %>
<% Connect c = new Connect();
ResultSet rs=c.returnTable(); %>
The result is that c is not null but rs is null. I tried to use another java class to do the same thing as the jsp file. Everything is fine and rs is not null.
Why would the error happen in jsp? Thanks.
[ July 22, 2003: Message edited by: Yuan Ye ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
<%@ page import="Connect" %>
This isn't legal syntax, at least not with Java 1.4. You need to put your Connect class in a package, and use the package name in your "page import" tags.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
Ok, I fixed the way of import, but the problem remains the same. I post the methods below.
When I call the two methods from jsp or servlet.
returnSS() will be fine. However returnTable() returns null. But if I call the two methods in a java application, both are fine. "con" is a valid Connection object.

public String returnSS(){
{return "hello";}
}
public ResultSet returnTable(){
ResultSet rs = null;
try{
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM ACCOUNT");
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return rs;
}
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I have found the problem is in the line of "Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");". Which is just fine when I use in a java application. But if I use this line in jsp or servlet in Tomcat. It results exception. Could anyone help me to solve this problem? Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Sometimes a "Class.forName(~~~~~).newInstance()" may be needed to actually cause things to get rolling. Usually it's older systems that have that problem.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Is the .jar file containing that class installed in your web application's WEB-INF/lib directory?
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
OH, god. Thank you Ron. You are right. After I copy those .jar files into WEB-INF/lib, everything is fine. I was thinking since the .jar files are in classpath, Tomcat will be able to find it. Obvious this is not enough. Thanks again.
 
Consider Paul's rocket mass heater.
 
subject: a weird thing, please help.