开发者

i cannot get glassfish to render my jsp code

I have glassfish v3

I have the following code

<%@page import="java.io.*;" %>
<%@page import="java.sql.*;" %>

<%

Connection con=null;
ResultSet rs开发者_运维知识库t=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());
}


The following line will throw a SQLException with a message like "invalid column index" (the exact message depends on the JDBC driver).

out.print(rst.getString(0));

The index namely starts at 1, not 0. Fix your code accordingly.

out.print(rst.getString(1));
out.print(rst.getString(2));
out.print(rst.getString(3));

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:

  • How to avoid Java code in JSP files?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜