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:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

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

        Connection con = null; // object.
        ResultSet rst = null; // object.
        Statement stmt = null; // object.

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test2";
            con = DriverManager.getConnection(url, "root", "123");
            stmt = con.createStatement();
            String id = request.getParameter("id");
            String Course_name = request.getParameter("Course_name");
            
            rst = stmt.executeQuery("select Instructor, id, Course_name from untitled where Course_name = " + Course_name + " and id = " + id);
            
%>



<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Login Result:</h1>


        <%
        
        String Instructor = rst.getString("Instructor");

            if (Instructor == null) {

        %> Sorry, no match for that user in our database. Please try again.<%          
            
            } else if (Instructor != null){

        %> 
        
             Hello %= Instructor%>, go to <a href="index.jsp">staff home</a> to begin.--> <%


            }

        rst.close();
        stmt.close();
        con.close();

        } catch (ClassNotFoundException e) {
            System.err.println("Could not load database Driver!");
        } catch (SQLException e) {
            System.err.println("Could not connect to to the database!");
        }

        %>

       

 </body>
</html>

Any help is appreciated thanks in advance. :)

Recommended Answers

All 5 Replies

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:

if (Instructor == null) 
    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:

catch (ClassNotFoundException e) {
            System.err.println("Could not load database Driver!");
%> <%= e.getMessage()%> <%
        } catch (SQLException e) {
            System.err.println("Could not connect to to the database!");
%> <%= e.getMessage()%> <%
        }

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

commented: This guy is one of the best reasons to come to Daniweb!! +2
commented: Good advice. +16

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!!! :icon_mrgreen:

This is what my code looks like now:

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

        Connection con = null; // object.
        ResultSet rst = null; // object.
        Statement stmt = null; // object.

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test2";
            con = DriverManager.getConnection(url, "root", "123");
            stmt = con.createStatement();
            String id = request.getParameter("id");
            String Course_name = request.getParameter("Course_name");

            rst = stmt.executeQuery("select Instructor, id, Course_name from untitled where Course_name = '" + Course_name + "' and id = " + id);

%>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Login Result:</h1>
        
        <%
            if (rst.next() == false) {

        %> Sorry, no match for that user in our database. Please try again.<% } 
            
            
            else {

            String Instructor = rst.getString("Instructor");    
               
         %>

        Hello <%= Instructor%>, go to <a href="index.jsp">staff home</a> to begin. <%
            }//end else

                    rst.close();
                    stmt.close();
                    con.close();
                
       }//end if try

catch (ClassNotFoundException e) {
                    System.err.println("Could not load database Driver!");
        %> <%= e.getMessage()%> <%
        
                }

catch (SQLException e) {
                    System.err.println("Could not connect to to the database!");
        %> <%= e.getMessage()%> <%
        
                }

        %>
        
        
    </body>
</html>

Will do!

is this search function?? are u using any search form??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.