首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从mysql到jsp页面显示表数据

问从mysql到jsp页面显示表数据
EN

Stack Overflow用户
提问于 2015-03-28 19:18:50
回答 4查看 4.8K关注 0票数 1

我有过

代码语言:javascript
复制
<%@ page import="com.mysql.*" %>
<%@ page import="java.sql.*" %>

<html>
<body>
<div id="content">


    <p>Displaying table contents: </p>

    <table border="0" cellpadding="10">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Company</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>


            <%
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = null;
                conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/Connection", "username", "password");
                Statement stmt = null;
                stmt = conn.createStatement();
                String query = "select * from employeee";
                ResultSet rs = null;
                rs = stmt.executeQuery(query);
                while(rs.next()){
            %>
            <tr>
                <%
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    String company = rs.getString("company");
                    int salary = rs.getInt("salary");
                %>
                <td><%=id %></td>
                <td><%=name %></td>
                <td><%=company %></td>
                <td><%=salary %></td>
            </tr>               

            <%      
                }
            %>

        </tbody>
    </table>
</div>
</body>
</html>

我得到了一个Class Not Found Exception for com.mysql.jdbc.Driver

我将外部jar添加到项目中,并使用相同的代码在一个普通的java文件中获得结果。

这里怎么了?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-03-28 19:30:30

将mysql jar放在lib文件夹中。在本地,您可能会将其作为外部jar来完成。

票数 1
EN

Stack Overflow用户

发布于 2015-03-28 19:23:22

首先,JSP是您应用程序的表示层,它不应该知道数据库。将JSP中的数据库代码删除到bean中。JSP读取bean并显示数据。

也就是说,JDBC驱动程序是运行JSP的服务器的外部依赖项,必须添加到服务器的类路径中。

您是在服务器中还是在项目中添加了它,所以这实际上是在war文件中吗?

票数 1
EN

Stack Overflow用户

发布于 2015-03-28 21:39:41

我将查看像DataTables (http://www.datatables.net/)这样的框架,并将数据库访问代码移到它属于服务器的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29321483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档