943,545 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 1236
  • JSP RSS
Jun 21st, 2009
0

Retrieval and comparison of a value stored in a MySQL database

Expand Post »
Hello everyone, I'm working on a project where I wish to find out who the instructor for a particular course is. The user is has to input the course name and course id for that course id then the script will search for the instructor. If the value returned is null (instructor not found), the user will be asked to refine his search. But if all goes well the user will be supplied with the information he needs.

But for some reason when I try the code I came up with, all I get is a blank screen.

This is what came up with so far:
JSP Syntax (Toggle Plain Text)
  1.  
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5.  
  6. <%@ page import="java.sql.*" %>
  7. <%
  8.  
  9. Connection con = null; // object.
  10. ResultSet rst = null; // object.
  11. Statement stmt = null; // object.
  12.  
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. String url = "jdbc:mysql://localhost:3306/test2";
  16. con = DriverManager.getConnection(url, "root", "123");
  17. stmt = con.createStatement();
  18. String id = request.getParameter("id");
  19. String Course_name = request.getParameter("Course_name");
  20.  
  21. rst = stmt.executeQuery("select Instructor, id, Course_name from untitled where Course_name = " + Course_name + " and id = " + id);
  22.  
  23. %>
  24.  
  25.  
  26.  
  27. <html>
  28. <head>
  29. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  30. <title>JSP Page</title>
  31. </head>
  32. <body>
  33. <h1>Login Result:</h1>
  34.  
  35.  
  36. <%
  37.  
  38. String Instructor = rst.getString("Instructor");
  39.  
  40. if (Instructor == null) {
  41.  
  42. %> Sorry, no match for that user in our database. Please try again.<%
  43.  
  44. } else if (Instructor != null){
  45.  
  46. %>
  47.  
  48. Hello %= Instructor%>, go to <a href="index.jsp">staff home</a> to begin.--> <%
  49.  
  50.  
  51. }
  52.  
  53. rst.close();
  54. stmt.close();
  55. con.close();
  56.  
  57. } catch (ClassNotFoundException e) {
  58. System.err.println("Could not load database Driver!");
  59. } catch (SQLException e) {
  60. System.err.println("Could not connect to to the database!");
  61. }
  62.  
  63. %>
  64.  
  65.  
  66.  
  67. </body>
  68. </html>

Any help is appreciated thanks in advance.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 21st, 2009
2

Re: Retrieval and comparison of a value stored in a MySQL database

I don't know if this the case, but you MUST first call:
rs.next() to get the row from the ResultSet.
Meaning that when you do:
String Instructor = rst.getString("Instructor"); you probably get an exception, go inside catch, and nothing is displayed:
Also about this:
JSP Syntax (Toggle Plain Text)
  1. if (Instructor == null)
  2. Sorry, no match for that user in our database. Please try again.

If there is no match the rs.next() will return false. So you need an if statement checking that. If it is false, THEN there is no match. If there is no match then the rs.next() will be false not the Instructor null. If you manage to call rs.next() and the rst.getString("Instructor") then there was a row returned so there are values to retrieve.
When Instructor is null doesn't mean that there were no rows. It means that you got a row from the database and the table column with name "Instructor" doesn't have a value. Of course if "Instructor" is Primary Key that would be impossible but I am talking in general.

So I suggest, first put some message inside the catch that are printed at the page:
JSP Syntax (Toggle Plain Text)
  1. catch (ClassNotFoundException e) {
  2. System.err.println("Could not load database Driver!");
  3. %> <%= e.getMessage()%> <%
  4. } catch (SQLException e) {
  5. System.err.println("Could not connect to to the database!");
  6. %> <%= e.getMessage()%> <%
  7. }

And then and MOST IMPORTANT do whatever you want to do from the beginning but this time follow the tutorial at the top of the JSP forum with title JSP database connectivity according to Model View Controller (MVC) Model 2
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jun 21st, 2009
0

Re: Retrieval and comparison of a value stored in a MySQL database

THANK YOU!!!! THANK YOU!!!! THANK YOU!!!! THANK YOU!!!!

Putting the %> <%= e.getMessage()%> <% helped me to pin-point and solve my problem almost instantly. You're a JSP genius!!!

This is what my code looks like now:

JSP Syntax (Toggle Plain Text)
  1. <%@ page import="java.sql.*" %>
  2. <%
  3.  
  4. Connection con = null; // object.
  5. ResultSet rst = null; // object.
  6. Statement stmt = null; // object.
  7.  
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver");
  10. String url = "jdbc:mysql://localhost:3306/test2";
  11. con = DriverManager.getConnection(url, "root", "123");
  12. stmt = con.createStatement();
  13. String id = request.getParameter("id");
  14. String Course_name = request.getParameter("Course_name");
  15.  
  16. rst = stmt.executeQuery("select Instructor, id, Course_name from untitled where Course_name = '" + Course_name + "' and id = " + id);
  17.  
  18. %>
  19.  
  20.  
  21. <html>
  22. <head>
  23. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  24. <title>JSP Page</title>
  25. </head>
  26. <body>
  27. <h1>Login Result:</h1>
  28.  
  29. <%
  30. if (rst.next() == false) {
  31.  
  32. %> Sorry, no match for that user in our database. Please try again.<% }
  33.  
  34.  
  35. else {
  36.  
  37. String Instructor = rst.getString("Instructor");
  38.  
  39. %>
  40.  
  41. Hello <%= Instructor%>, go to <a href="index.jsp">staff home</a> to begin. <%
  42. }//end else
  43.  
  44. rst.close();
  45. stmt.close();
  46. con.close();
  47.  
  48. }//end if try
  49.  
  50. catch (ClassNotFoundException e) {
  51. System.err.println("Could not load database Driver!");
  52. %> <%= e.getMessage()%> <%
  53.  
  54. }
  55.  
  56. catch (SQLException e) {
  57. System.err.println("Could not connect to to the database!");
  58. %> <%= e.getMessage()%> <%
  59.  
  60. }
  61.  
  62. %>
  63.  
  64.  
  65. </body>
  66. </html>
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 22nd, 2009
0

Re: Retrieval and comparison of a value stored in a MySQL database

Click to Expand / Collapse  Quote originally posted by javaAddict ...
And then and MOST IMPORTANT do whatever you want to do from the beginning but this time follow the tutorial at the top of the JSP forum with title JSP database connectivity according to Model View Controller (MVC) Model 2
Now you must follow the link above and start all over
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jun 22nd, 2009
0

Re: Retrieval and comparison of a value stored in a MySQL database

Will do!
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Oct 14th, 2010
0
Re: Retrieval and comparison of a value stored in a MySQL database
is this search function?? are u using any search form??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
atikahuk is offline Offline
2 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: How to make a field in mysql database as link to a file after doing a search
Next Thread in JSP Forum Timeline: FileName Checker





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC