数据库内容未显示

时间:2019-11-03 13:54:40

标签: mysql jsp jdbc

我正在尝试使用jsp将数据库的内容显示在网页上 它显示为空白,但代码的html部分除外,例如标头“ id name”等。 我认为我的代码是正确的。请帮助

我在很多网站上都尝试过相同的代码。不幸的是,它们最终都以相同的方式出现

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>



<table class="table">
<thead>
    <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Address</th>
    </tr>
    </thead>
    <tbody>
    <%
    String host="jdbc:mysql://localhost:3306/dbname";
    Connection conn=null;
    Statement stat=null;
    ResultSet res=null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
        e.printStackTrace();
        }
    try{
    conn=DriverManager.getConnection(host,"root","password");
    stat=conn.createStatement();
    String data="select * from emp";
    res=stat.executeQuery(data);
    while(res.next())
    {%>
    <tr>
    <td><%=res.getInt(1) %></td>
    <td><%=res.getString("name") %></td>
    <td><%=res.getString("email") %></td>
    <td><%=res.getString("phone") %></td>
    <td><%=res.getString("address") %></td>

    </tr>

    <% 
    }
    } catch (Exception e) {
        e.printStackTrace();
        }
    %>



</tbody>
</table>
</html>

仅html代码部分的标题(或表名)显示在网页上

1 个答案:

答案 0 :(得分:-1)

不,您不能使用此代码执行此操作。你必须去JSTL。在jstl中,您可以执行此操作。