0

I have glassfish v3

I have the following code

<%@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());
}
mu is too short
  • 426,620
  • 70
  • 833
  • 800
user326096
  • 307
  • 1
  • 4
  • 11

1 Answers1

0

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • ive adjusted my code to the following: still nothing shows <% System.out.println("show me something"); %> – user326096 Apr 16 '11 at 21:44
  • Did you fix the incorrect `getString()` index? This line prints to system log, not to the browser screen. What happens instead? What do you retrieve in browser? Rightclick page in browser and choose *View Source*, what do you see? – BalusC Apr 16 '11 at 21:51
  • How does the generated HTML page look? – Thorbjørn Ravn Andersen Apr 16 '11 at 21:58