自定义类导入 API 是否包含在 .class 文件中?

发布于 2024-10-11 09:25:46 字数 1717 浏览 2 评论 0 原文

我有一个问题,我有一个导入 java.sql. 的自定义类;我正在创建一个jsp页面,在jsp页面中,我做了自定义类的页面导入,但是当我尝试调用我的自定义类数据库方法时,它无法工作。只有当我进行了 java.sql 的页面导入时,它才起作用。那么自定义类导入的 API 是否包含在 .class 文件中?

An error occurred at line: 6 in the jsp file: /resetpw.jsp
Statement cannot be resolved to a type
3: 
4: <%
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.

编辑。添加了如果我没有执行 java.sql.* 的页面导入,将会出现什么错误;

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: <%
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.*;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-10-18 09:25:46

使用 java.sql 包中的 Statement 和 Resultset 类很简单。所以你必须在你的JSP中导入java.sql包。

如果您在自定义类中完成所有数据库工作,并在 JSP 中调用它的方法,而 JSP 不需要任何此类,那么您就可以在 Jsp 页面中省略 java.sql 的导入。

一个类中导入的 API 不可用于调用第一个类的另一个类或页面。它们不包含在 .class 文件中。

希望你明白我的意思。

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.

夜灵血窟げ 2024-10-18 09:25:46

哈利·乔伊是对的,你需要导入声明。这是 JSP 页面的一些框架:

<%@ 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 ...

这应该可以工作,但是我强烈建议在容器中使用 JNDI 和 JSTL SQL。以下是一些可能有用的教程:

  1. 来自 IBM 的 JSTL SQL 教程
  2. JAVA2S 中不带 JNDI 的 JSTL SQL
  3. Tomcat 中的 JNDI

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:

  1. JSTL SQL tutorial from IBM
  2. JSTL SQL without JNDI from JAVA2S
  3. JNDI in Tomcat
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文