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. :)

Dani AI

Generated

Brief expert summary: the blank page was caused by a swallowed SQLException and two common pitfalls visible in the original code: reading a ResultSet before advancing its cursor, and building SQL by concatenating user input (which also produced a quoting error). was correct to point out that ResultSet.next() must be called because the cursor starts before the first row (Oracle JDBC tutorial on ResultSet.next()) (docs.oracle.com). Building SQL via string concatenation is brittle and unsafe; parameterized queries stop both syntax problems and SQL injection attempts (OWASP SQL Injection Prevention Cheat Sheet) (cheatsheetseries.owasp.org). Printing e.getMessage() (as added) was the right quick-debug move.

Practical best practices to adopt now: prefer PreparedStatement with bound parameters rather than concatenating request values; validate/parse id as a number before binding; check rs.next() and only then call getters; use try-with-resources to ensure Connection/Statement/ResultSet are closed automatically (Oracle on try-with-resources) (oracle.com). Move database logic out of JSP into a servlet/DAO and use JSTL/EL for rendering (Model‑2/MVC) to keep presentation and logic separate (Servlets & JSP best practices) (oracle.com). For production, use a pooled DataSource instead of DriverManager for scalable connections (Tomcat JDBC pool overview) (tomcat.apache.org).

Quick checklist to fix this thread’s problem immediately: replace the concatenated query with a parameterized statement and bind Course_name and id; parse id safely and handle parsing errors; call rs.next() and only then read Instructor; log exceptions to server logs (avoid blank-screen swallowing) and return a friendly message to users; later refactor into servlet + DAO and enable connection pooling. These steps resolve the reported symptom and make the lookup safer and maintainable.

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.