我无法让 glassfish 渲染我的 jsp 代码
我有 glassfish v3
我有以下代码
<%@page import="java.io.*;" %>
<%@page import="java.sql.*;" %>
<%
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost:3306/achme_health";
con=DriverManager.getConnection(url,"root","");
stmt=con.createStatement();
rst=stmt.executeQuery("select patient_no,fname,lname from patients");
while(rst.next()){
out.print(rst.getString(0));
out.print(rst.getString(1));
out.print(rst.getString(2));
}
}catch(Exception e){
System.out.println("-1");
System.out.println(e.getMessage());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的行将抛出一个
SQLException
,并带有类似“无效列索引”的消息(确切的消息取决于 JDBC 驱动程序)。即索引从 1 开始,而不是 0。相应地修复您的代码。
与具体问题无关,请注意,您只是将异常消息打印到日志中(您读过吗?),而不是抛出整个异常和/或转储整个堆栈跟踪。这对调试没有帮助。我建议修改你糟糕的异常处理方法。此外,Java 代码最好放置在真正的 Java 类中,而不是 JSP 文件中。
另请参阅:
The following line will throw a
SQLException
with a message like "invalid column index" (the exact message depends on the JDBC driver).The index namely starts at 1, not 0. Fix your code accordingly.
Unrelated to the concrete problem, please note that you're only printing the exception message to the logs (did you read it?) instead of throwing the whole exception and/or dumping the entire stacktrace. This is unhelpful in debugging. I'd suggest to revise your poor exception handling approach. Also that Java code is preferably to placed in a real Java class rather than a JSP file.
See also: