英文:
Retrieve Data from Database in Java Server Page
问题
我试图从数据库中获取数据并在浏览器中打印出一个表格。代码位于一个 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>
在日志文件中,我找到了两条以下错误消息:
在 jsp 文件中的第 [41] 行发生错误:[/index.jsp]
无法将 userid 解析为变量
38:
39: <%
40: try{
41: connection = DriverManager.getConnection(connectionUrl+database, userid, password);
42: statement=connection.createStatement();
43: String sql ="select * from kunde";
44: resultSet = statement.executeQuery(sql);
...
堆栈跟踪,带有根本原因
org.apache.jasper.JasperException: 无法为 JSP 编译类:
在 jsp 文件中的第 [41] 行发生错误:[/index.jsp]
无法将 userid 解析为变量
38:
39: <%
40: try{
41: connection = DriverManager.getConnection(connectionUrl+database, userid, password);
42: statement=connection.createStatement();
43: String sql ="select * from kunde";
44: resultSet = statement.executeQuery(sql);
...
和
[警告] 拒绝访问用户 'XY'@'localhost'(未使用密码)
英文:
i am trying to print out a table in the browser with fetching data from a database. The code is in a jsp-file.
My output looks the following:
It is still missing the values from the table. Now i am wondering why the data from the table is not fetched. I checked the data, and the table is filled with values.
Here is my command-line output with data (blurred):
This is my code in the jsp-file so far:
<%@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>
In the log-files i found two of the following error messages:
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, userid, 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, userid, password);
42: statement=connection.createStatement();
43: String sql ="select * from kunde";
44: resultSet = statement.executeQuery(sql);
And
[Warning] Access denied for user 'XY'@'localhost' (using password: NO)
答案1
得分: 1
我解决了这个问题。
我需要将 mysql-connector-java-5.1.25-bin.jar
文件添加到我的 tomcat 的 lib 文件夹中。
英文:
I solved the problem.
I needed to add the mysql-connector-java-5.1.25-bin.jar
file to my tomcat's lib.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论