0

I am creating my first java web application.. Actually the problem I am facing is in ManageAllStudents. It should show the list of information of students after retrieving it from the database.

I'm using the below code but it doesn't show the information or doesn't retrieve the information from the database.

Kindly guide me what I am missing. This is my code:

 <% try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          String url =  "jdbc:odbc:stdProjectDataDSN";
          Connection c = DriverManager.getConnection(url);
          String sql = "Select * from students ORDER BY ID DESC";
          PreparedStatement pStmt = c.prepareStatement(sql);  
          ResultSet rs = pStmt.executeQuery(sql);

          while (rs.next()) {
 %>
           <tr height="40">   
               <th> <%=rs.getString("ID")%> </th>
               <th> <%=rs.getString("Name")%> </th>
               <th> <%=rs.getString("RollNumber")%> </th>
               <th> <%=rs.getString("PhoneNumber")%> </th>
               <th> <%=rs.getString("StudyProgram")%> </th>
               <th> <%=rs.getString("Status")%> </th>
               <th> <a href="ManageAllStudent.jsp">update</a> </th>
               <th> <a href="ManageAllStudent.jsp">delete</a> </th> 
           </tr>
 <%
         }
         rs.close();
         pStmt.close();
         c.close();
     }
     catch (Exception e) {
     }
 %>
Fred
  • 3,324
  • 1
  • 19
  • 29
mobi001
  • 517
  • 1
  • 8
  • 19

2 Answers2

1
PreparedStatement pStmt = c.prepareStatement(sql);  
ResultSet rs = pStmt.executeQuery();

There is no reason to call executeQuery(sql) you already use PreparedStatement. Probably problem is here. So try it change.

Second, you have to call close() method in finally block because if it will crash, catch block will called but your close() methods no.

Finally block guarantees that will be called always.


Update: You need to avoid writing raw java code in JSP file. Check this.

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • **HTTP Status 500** type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 104 in the jsp file: /build/web/ManageAllStudent.jsp rs cannot be resolved 101: String Name; 102: String PhoneNumber; 103: 104: while (rs.next()) { 105: %> 106: 107: – mobi001 Jun 28 '12 at 17:38
  • its too long i cant post the all the basic is above mentioned – mobi001 Jun 28 '12 at 17:39
  • so you writing raw java code in JSP, you need to avoid this, see my answer, there is a link. – Simon Dorociak Jun 28 '12 at 17:41
  • it means i have to write the class individually? – mobi001 Jun 28 '12 at 17:45
  • 1
    yes you should have clear java classes and call only methods. so fot example you should create this like method that returns List. – Simon Dorociak Jun 28 '12 at 17:52
1

I got what iam missing i didnot configure my database in control panel->administrative tools...

So when i set it up it just work:)

thank to all specially @hawaii.five-0 who gave me a wonderful & helpful sugesstions

mobi001
  • 517
  • 1
  • 8
  • 19