使用Servlet的MySQL到JSP

时间:2017-11-30 18:48:29

标签: java mysql jsp servlets

我正在尝试从MySQL的一个表中检索数据,并在.jsp文件中显示整行。现在,当我运行代码时,它会打印没有值的表。 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) { }
   }
  }
}

这是我的reivew页面:

<%@ 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>

我正在尝试从MySQL数据库中提取值以打印到review.jsp页面。关于我做错了什么的任何想法?

1 个答案:

答案 0 :(得分:1)

您好,我认为这是一个错字

通过此req.setAttribute("Course", Course);

更改此req.setAttribute("course", Course);

最佳,