在jsp中显示来自Postgres的表格

时间:2015-12-22 11:37:07

标签: java postgresql servlets java-ee

我想在jsp中显示表格。

静态内容很好,但表格没有显示 我想在MVC中做,我不想在jsp中使用java代码

DbConnetion

import java.sql.Connection;
import java.sql.DriverManager;

public class DbConnection {
    Connection db_conn;
    public Connection getConnection()
    {
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            String url = "jdbc:postgresql://localhost:5432/testowaaplikacja";
            String user = "postgres";
            String pass = "password";
                db_conn = DriverManager.getConnection(url,user,pass);  
                if((db_conn != null)&&(!db_conn.isClosed()))
            {
                System.out.println("Connected...");
            }
            return db_conn;
        }
        catch(Exception e)
        {
            //System.out.println("Error in connecting to database ");
            e.printStackTrace();

        }
        return null;
    }
}

模型

public class KsiegaWieczysta {

    private long nrksiegi;
    private String adres;
    private long nrdzialki;

    public long getNrksiegi() {
        return nrksiegi;
    }

    public void setNrksiegi(long nrksiegi) {
        this.nrksiegi = nrksiegi;
    }

    public String getAdres() {
        return adres;
    }

    public void setAdres(String adres) {
        this.adres = adres;
    }

    public long getNrdzialki() {
        return nrdzialki;
    }

    public void setNrdzialki(long nrdzialki) {
        this.nrdzialki = nrdzialki;
    }

}

DAO

public class KsiegiDAO {

    private static DbConnection db1;
    private static Connection connection;

    public List<KsiegaWieczysta> getReleaseData() {
        List<KsiegaWieczysta> ksiegi = new ArrayList<KsiegaWieczysta>();
        ResultSet resultSet = null;
        Statement statement = null;

        try {
            db1 = new DbConnection();
            connection = db1.getConnection();

            String query = "SELECT nrksiegi, address, nrdzialki FROM ksiegi";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(query);

            {
                while (resultSet.next()) {
                    KsiegaWieczysta ksiega = new KsiegaWieczysta();
                    ksiega.setNrksiegi(resultSet.getLong("nrksiegi"));
                    ksiega.setAdres(resultSet.getString("address"));
                    ksiega.setNrdzialki(resultSet.getLong("nrdzialki"));
                    ksiegi.add(ksiega);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return ksiegi;
    }
}

的Servlet

@WebServlet("/showtable")
public class ShowTable extends HttpServlet {

    KsiegiDAO ksiegiDAO = new KsiegiDAO();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<KsiegaWieczysta> ksiegi = ksiegiDAO.getReleaseData();
        request.setAttribute("ksiegi", ksiegi); // Will be available as
                                                    // ${ksiegi} in JSP
        request.getRequestDispatcher("/WEB-INF/showtable.jsp").forward(request, response);
    }


}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello
<table>
    <c:forEach items="${ksiegi}" var="ksiega">
        <tr>
            <td><c:out value="${ksiega.nrksiegi}" /></td>
            <td><c:out value="${ksiega.adres}" /></td>
            <td><c:out value="${ksiega.nrdzialki}" /></td>

        </tr>
    </c:forEach>

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

感谢您的帮助

0 个答案:

没有答案