显示数据库中的数据

时间:2018-04-16 11:17:16

标签: mysql jsp spring-mvc

new.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>   
<!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=UTF-8">
<title>JSP List Users Records</title>
</head>
<body>
    <sql:setDataSource
        var="headway"
        driver="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/headway"
        user="root" password="toor"
    />

    <sql:query var="listUsers"   dataSource="$ {headway}">
        SELECT * FROM head;
    </sql:query>

    <div align="center">
        <table border="1" cellpadding="5">
            <caption><h2>List of users</h2></caption>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Profession</th>
            </tr>
            <c:forEach var="user" items="${listUsers.rows}">
                <tr>
                    <td><c:out value="${user.id}" /></td>
                    <td><c:out value="${user.name}" /></td>
                    <td><c:out value="${user.salary}" /></td>
                    <td><c:out value="${user.designation}" /></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

我正在尝试显示数据库中的数据。 但它显示错误:

org.apache.jasper.JasperException:在第20行处理JSP页面/new.jsp时发生异常

    user="root" password="toor"
 />

<sql:query var="listUsers"   dataSource="$ {headway}">
    SELECT * FROM head;
 </sql:query>

无法获得连接,DataSource无效:&#34; java.sql.SQLException:找不到合适的$ {headway}驱动程序&#34;

但实际上有数据库名称是进展

表名是头;

+----+-------+--------+-------------+
| id | name  | salary | designation |
+----+-------+--------+-------------+
|  1 | srinu |  50000 | test        |
|  2 | srinu | 500000 | devel       |
|  3 | pawan | 100000 | net         |
|  4 | ravi  |   2000 | .net        |
|  5 | ramya |  22222 | tester      |
|  6 | srinu |      0 | sql         |
|  7 | srinu | 200000 | test        |
|  8 | srinu |   1000 | java        |
|  9 | srinu |  11111 | html        |
| 10 | srinu |  11111 | html        |
| 11 | vv    |  29999 | hg          |
| 12 | s     |    147 | aa          |
+----+-------+--------+-------------+

1 个答案:

答案 0 :(得分:0)

您的错误显示为No suitable driver found for,这意味着它无法找到用于连接DB的源库。找到DB所需的合适驱动程序,并将其添加到项目和服务器类路径中。