如何从数据库下载数据到JSP表?

时间:2014-02-10 16:53:08

标签: java mysql jsp servlets jdbc

很快:这是简单的网络应用程序。在主页面上有一个按钮,在点击它之后它必须是另一个页面,其中包含来自数据库的数据的表格。     我正在使用servlets / jsp MySQL     这是代码

主页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
  <head>
    <title>University</title>
  </head>
  <body>
    <h2 style="text-align: center">Welcome to university!</h2>
    <p style="text-align: center"><img src="Images/university.jpg"></p>
        <form>
            <p style="text-align: center">
                <button formmethod="GET" formaction="SelectStudents.do">See all students</button>
            </p>
        </form>
  </body>
</html>

带表格的页面

<%@ page import="java.util.List" %>
<%@ page import="model.StudentDAO" %>
<%@ page import="model.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Students</title>
</head>
<body>
    <table border="1" >
        <caption>Students</caption>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Surname</th>
            <th>Age</th>
            <th>Country</th>
            <th>City</th>
            <th>Street</th>
            <th>House</th>
            <th>Flat</th>
            <th>Group</th>
        </tr>
        <%
            List<Student> students = new StudentDAO().selectAll();
            for (Student s : students) {
                %>
                <tr>
                    <td><%=s.getId()%>></td>
                    <td><%=s.getName()%>></td>
                    <td><%=s.getSurname()%>></td>
                    <td><%=s.getAge()%>></td>
                    <td><%=s.getAddress().getCountry()%>></td>
                    <td><%=s.getAddress().getCity()%>></td>
                    <td><%=s.getAddress().getStreet()%>></td>
                    <td><%=s.getAddress().getHouseNumber()%>></td>
                    <td><%=s.getAddress().getFlatNumber()%>></td>
                    <td><%=s.getGroup().getName()%>></td>
                </tr>
                <%
            }
        %>
    </table>
</body>
</html>

班级学生

public class Student {

    private int id;
    private String name;
    private String surname;
    private int age;
    private Address address;
    private Group group;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String name) {
        this.surname = surname;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getAddress() {
        return address;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Group getGroup() {
        return group;
    }
}

JDBCConnecto

package model;

import java.sql.*;

public class ConnectionManager {

    private static final String JDBC_LOADER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/";
    private static final String LOGIN = "root";
    private static final String PASSWORD = "15021990";

    private Connection connection;

    public ConnectionManager() throws ClassNotFoundException, SQLException{
        Class.forName(JDBC_LOADER);
        connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
    }

    public Connection getConnection() throws SQLException{
        return connection;
    }
}

DAO

package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StudentDAO {

    private static final String SELECT_ALL =
            "SELECT student.id, student.name, student.surname, student.age, \n" +
                    "\t   address.country, address.city, address.street, address.house, address.flat, \n" +
                    "\t   class.name\n" +
                    "\t   FROM University.student join University.address \n" +
                    "\t   on university.student.address = university.address.id join university.class\n" +
                    "\t   on university.student.class = university.class.id";

    public List<Student> selectAll() {
        List<Student> result = new ArrayList<Student>();
        Connection c = null;
        try {
            c = new ConnectionManager().getConnection();
            Statement s = c.createStatement();
            ResultSet students = s.executeQuery(SELECT_ALL);

            while (students.next()) {
                int id = students.getInt(1);
                String name = students.getString(2);
                String surname = students.getString(3);
                int age = students.getInt(4);
                String country = students.getString(5);
                String city = students.getString(6);
                String street = students.getString(7);
                int house = students.getInt(8);
                int flat = students.getInt(9);
                String groupName = students.getString(10);

                Address address = new Address();
                address.setCountry(country);
                address.setCity(city);
                address.setStreet(street);
                address.setHouseNumber(house);
                address.setFlatNumber(flat);

                Group group = new Group();
                group.setName(groupName);

                Student student = new Student();
                student.setId(id);
                student.setName(name);
                student.setSurname(surname);
                student.setAge(age);
                student.setAddress(address);
                student.setGroup(group);

                result.add(student);
            }
        } catch (SQLException e) {
            System.out.print(e.getErrorCode());
        } catch (ClassNotFoundException e) {

        } finally {
            try {
                if (c != null)
                    c.close();
            } catch (SQLException e) {
                System.out.print(e.getErrorCode());
            }
        }
        return result;
    }
}

的Servlet

package controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class StudentsSelect extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        RequestDispatcher view = request.getRequestDispatcher("table.jsp");
        try {
            view.forward(request, response);
        } catch (ServletException e) {

        } catch (IOException e) {

        }
    }
}

问题是按下按钮后,table.jsp中没有关于学生的信息。

1 个答案:

答案 0 :(得分:0)

您显然忘记添加MySQL connector(选择平台独立和点击下载zip包,如果您没有下载它)。将MySQL连接器jar文件添加到项目中的lib目录。这应该可以解决你的问题。