are custom classes imported API included in .class files?
i have a question, i have a custom class which imports java.sql.; and i am creating a jsp page, in the jsp page, i did a page import of the custom class , however when i tried to call my custom class database methods it cant work. only when i did a page import of java.sql. did it work. so are custom classes imported API included in .class files?
An error occurred at line: 6 in the jsp file: /resetpw.jsp
Statement cannot be resolved to a type
3:
4:开发者_Python百科 <%
5: db.connect();
6: Statement stmt = db.getConnection().createStatement();
7: ResultSet rs = stmt.executeQuery("SELECT * FROM created_accounts");
8:
9:
An error occurred at line: 7 in the jsp file: /resetpw.jsp
ResultSet cannot be resolved to a type
4: <%
5: db.connect();
6: Statement stmt = db.getConnection().createStatement();
7: ResultSet rs = stmt.executeQuery("SELECT * FROM created_accounts");
8:
9:
10:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:319)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:565)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:309)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.31 logs.
edited. added what error will come up if i did not do a page import of java.sql.*;
Its simple you are using Statement and Resultset class which are in java.sql package. So you must import java.sql package in your JSP.
If you do all db work in your custom class and call a method of it in JSP which doesn't require any of this classes then and only then you can omit importing of java.sql in Jsp page.
No API imported in one class is not available to another class or page which is calling the first class. They are not included in .class file.
Hope you get my point.
Harry Joy is right, you need import statements. Here is some skeleton of a JSP page:
<%@ page language ="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
...some stuff...
<%
String paramT1=request.getParameter("t1");
String paramT2=request.getParameter("t2");
%>
...some stuff..
<%
try{
Class.forName("your_jdbc_drier_class");
Connection con=DriverManager.getConnection("connection_url","username","password");
PreparedStatement st;
st = con.prepareStatement("Insert into ch values (1,2)");
st.setString(1,fname);
st.setString(2,lname);
st.executeUpdate();
}
catch(Exception e1)
{
out.println("cannot display the records");
}
%>
... some stuff ...
This should work, however I'd strongly suggest to use JNDI in the container and JSTL SQL. Here are some tutorials which might be helpful:
- JSTL SQL tutorial from IBM
- JSTL SQL without JNDI from JAVA2S
- JNDI in Tomcat
精彩评论