如何解决从Java Server页面中的数据库检索数据
我正在尝试在浏览器中打印出表格,并从数据库中获取数据。该代码在一个jsp文件中。
我的输出如下:
它仍然缺少表中的值。现在我想知道为什么不从表中获取数据。我检查了数据,并在表中填充了值。
到目前为止,这是我在jsp文件中的代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("userid");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "store";
String userid = "root";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Retrieve data from database in jsp</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Strasse</th>
<th>PLZ</th>
<th>Ort</th>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database,userid,password);
statement=connection.createStatement();
String sql ="select * from kunde";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td><%=resultSet.getString("name") %></td>
<td><%=resultSet.getString("strasse") %></td>
<td><%=resultSet.getString("plz") %></td>
<td><%=resultSet.getString("ort") %></td>
</tr>
<%
}
connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>
在日志文件中,我发现以下两个错误消息:
An error occurred at line: [41] in the jsp file: [/index.jsp]
userid cannot be resolved to a variable
38:
39: <%
40: try{
41: connection = DriverManager.getConnection(connectionUrl+database,password);
42: statement=connection.createStatement();
43: String sql ="select * from kunde";
44: resultSet = statement.executeQuery(sql);
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [41] in the jsp file: [/index.jsp]
userid cannot be resolved to a variable
38:
39: <%
40: try{
41: connection = DriverManager.getConnection(connectionUrl+database,password);
42: statement=connection.createStatement();
43: String sql ="select * from kunde";
44: resultSet = statement.executeQuery(sql);
和
[Warning] Access denied for user 'XY'@'localhost' (using password: NO)
解决方法
我解决了这个问题。
我需要将<li id="customize-control-cta-type" class="customize-control customize-control-radio" style="display: list-item;">
<span class="customize-control-title">Call To Action buttons on banner</span>
<div class="customize-control-notifications-container" style="display: none;">
<ul></ul>
</div>
<span id="_customize-description-cta-type" class="description customize-
control-description">How many buttons do you want?
</span>
<span class="customize-inside-control-row">
<input id="_customize-input-cta-type-radio-one" type="radio" aria-
describedby="_customize-description-cta-type" value="one" name="_customize-
radio-cta-type" data-customize-setting-link="cta-type">
<label for="_customize-input-cta-type-radio-one">one</label>
</span>
<span class="customize-inside-control-row">
<input id="_customize-input-cta-type-radio-two" type="radio" aria-
describedby="_customize-description-cta-type" value="two" name="_customize-
radio-cta-type" data-customize-setting-link="cta-type" checked="checked">
<label for="_customize-input-cta-type-radio-two">two</label>
</span>
</li>
文件添加到tomcat的lib中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。