简体   繁体   中英

MySQL to JSP using Servlet

I am trying to retrieve data from one column of my table in MySQL and display the whole row in a .jsp file. Right now, when I run the code it prints the table with no values. As Shown here .

import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

@WebServlet("/review")
public class findservlet1 extends HttpServlet {
  /**
     * 
     */
    private static final long serialVersionUID = 8402378218178447403L;

public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {

   // res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    Connection conn = null;
       //using movie_resolved view
    try{
      // Get a Connection to the database
        String DB_CONNECTION_URL = "jdbc:mysql://localhost:3306/mydatabase";
        String DB_USERNAME = "root";
        String DB_PASSWORD = "root";
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(DB_CONNECTION_URL, DB_USERNAME, DB_PASSWORD);
     // Create a Statement object
         PreparedStatement st = (PreparedStatement) con.prepareStatement("SELECT * FROM coursereview");

         ResultSet rs = st.executeQuery();

     if (rs.next())
      {

         com.java.bean.coursereview Course = new com.java.bean.coursereview ();
         Course.setCourse_id(rs.getString("courseid"));
         Course.setCourserate(rs.getString("courserate"));
         Course.setProfessor(rs.getString("professor"));
         Course.setProrate(rs.getString("prorate"));
         Course.setReview(rs.getString("review"));
        req.setAttribute("Course", Course);
        RequestDispatcher view = req.getRequestDispatcher("review.jsp");
          view.forward(req, res);
      }


    }
    catch(ClassNotFoundException e) {
     out.println("Couldn't load database driver: " + e.getMessage());
    }
    catch(SQLException e) {
      out.println("SQLException caught: " + e.getMessage());
    }
    finally {
      // Always close the database connection.
      try {
        if (conn != null) conn.close();
      }
      catch (SQLException ignored) { }
   }
  }
}

Here is my reivew page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="styles/style1.css">
<meta http-equiv="Content-Type" content="text/css; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<div class="banner">
    <img class="banner-image" src="images/uncclogo400.jpg"></div>
  <h2>Review a Course</h2>
  <table>
        <TR>
            <TD>Course ID: </TD>
            <TD>${course.courseid}</TD>
        </TR>

        <TR>
            <TD>Course Rate: </TD>
            <TD>${course.courserate}</TD>
        </TR>
         <TR>
            <TD>Professor: </TD>
            <TD>${course.professor}</TD>
        </TR>
        <TR>
            <TD>Professor Rate: </TD>
            <TD>${course.prorate}</TD>
        </TR>
        <TR>
            <TD>Review: </TD>
            <TD>${course.review}</TD>
        </TR>

        </table>
</body>
</html>

I am trying to pull the values from the MySQL DB to print to the review.jsp page. Any ideas on what I am doing wrong?

Hello, I think it's a typo

Change this req.setAttribute("Course", Course); by this req.setAttribute("course", Course);

Best,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM