我正在尝试在JSP页面中列出数据库表。
db =球队 表格=信息
列:ID,名称,年份
连接成功,其列表部分不起作用。
我尝试更改代码,并且出现相同的问题。
连接成功,但不会列出项目。
该错误最有可能在创建表的部分中出现。
Document : index
Created on : Nov 6,2019,8:24:33 AM
Author : joy
--%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" 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>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
try {
String connectionURL = "jdbc:mysql://localhost:3306/teams";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getconnection() method that takes parameters of
string type connection url,user name and password to connect to database. */
connection = DriverManager.getconnection(connectionURL,"root","");
// check weather connection is established or not by isClosed() method
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
<div align="center">
<table border="1" cellpadding="5">
<caption>List of Teams</h2</caption>
<tr>
<th>ID</th>
<th>Name</th>
<th>Year</th>
</tr>
<c:forEach items="${teams}" var="info">
<tr>
<td>${info.id}</td>
<td>${info.name}</td>
<td>${info.year}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
结果应显示表中具有3列的所有数据。