从数据库中检索数据在Java服务器页面中

huangapple go评论153阅读模式
英文:

Retrieve Data from Database in Java Server Page

问题

我试图从数据库中获取数据并在浏览器中打印出一个表格。代码位于一个 JSP 文件中。

我的输出如下所示:

从数据库中检索数据在Java服务器页面中

仍然缺少表中的值。现在我想知道为什么没有获取到表中的数据。我检查了数据,表中填充了值。

以下是带有数据的命令行输出(已模糊处理):

从数据库中检索数据在Java服务器页面中

以下是我在 JSP 文件中的代码:

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <%@page import="java.sql.DriverManager"%>
  3. <%@page import="java.sql.ResultSet"%>
  4. <%@page import="java.sql.Statement"%>
  5. <%@page import="java.sql.Connection"%>
  6. <%
  7. String id = request.getParameter("userid");
  8. String driver = "com.mysql.jdbc.Driver";
  9. String connectionUrl = "jdbc:mysql://localhost:3306/";
  10. String database = "store";
  11. String userid = "root";
  12. String password = "";
  13. try {
  14. Class.forName(driver);
  15. } catch (ClassNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. Connection connection = null;
  19. Statement statement = null;
  20. ResultSet resultSet = null;
  21. %>
  22. <!DOCTYPE html>
  23. <html>
  24. <head>
  25. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  26. <title>JSP Page</title>
  27. </head>
  28. <body>
  29. <h1>Retrieve data from database in jsp</h1>
  30. <table border="1">
  31. <tr>
  32. <th>Name</th>
  33. <th>Strasse</th>
  34. <th>PLZ</th>
  35. <th>Ort</th>
  36. </tr>
  37. <%
  38. try{
  39. connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  40. statement=connection.createStatement();
  41. String sql ="select * from kunde";
  42. resultSet = statement.executeQuery(sql);
  43. while(resultSet.next()){
  44. %>
  45. <tr>
  46. <td><%=resultSet.getString("name") %></td>
  47. <td><%=resultSet.getString("strasse") %></td>
  48. <td><%=resultSet.getString("plz") %></td>
  49. <td><%=resultSet.getString("ort") %></td>
  50. </tr>
  51. <%
  52. }
  53. connection.close();
  54. }
  55. catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. %>
  59. </table>
  60. </body>
  61. </html>

在日志文件中,我找到了两条以下错误消息:

  1. jsp 文件中的第 [41] 行发生错误:[/index.jsp]
  2. 无法将 userid 解析为变量
  3. 38:
  4. 39: <%
  5. 40: try{
  6. 41: connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  7. 42: statement=connection.createStatement();
  8. 43: String sql ="select * from kunde";
  9. 44: resultSet = statement.executeQuery(sql);
  10. ...
  11. 堆栈跟踪,带有根本原因
  12. org.apache.jasper.JasperException: 无法为 JSP 编译类:
  13. jsp 文件中的第 [41] 行发生错误:[/index.jsp]
  14. 无法将 userid 解析为变量
  15. 38:
  16. 39: <%
  17. 40: try{
  18. 41: connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  19. 42: statement=connection.createStatement();
  20. 43: String sql ="select * from kunde";
  21. 44: resultSet = statement.executeQuery(sql);
  22. ...

  1. [警告] 拒绝访问用户 '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:

从数据库中检索数据在Java服务器页面中

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):
从数据库中检索数据在Java服务器页面中

This is my code in the jsp-file so far:

  1. &lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
  2. &lt;%@page import=&quot;java.sql.DriverManager&quot;%&gt;
  3. &lt;%@page import=&quot;java.sql.ResultSet&quot;%&gt;
  4. &lt;%@page import=&quot;java.sql.Statement&quot;%&gt;
  5. &lt;%@page import=&quot;java.sql.Connection&quot;%&gt;
  6. &lt;%
  7. String id = request.getParameter(&quot;userid&quot;);
  8. String driver = &quot;com.mysql.jdbc.Driver&quot;;
  9. String connectionUrl = &quot;jdbc:mysql://localhost:3306/&quot;;
  10. String database = &quot;store&quot;;
  11. String userid = &quot;root&quot;;
  12. String password = &quot;&quot;;
  13. try {
  14. Class.forName(driver);
  15. } catch (ClassNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. Connection connection = null;
  19. Statement statement = null;
  20. ResultSet resultSet = null;
  21. %&gt;
  22. &lt;!DOCTYPE html&gt;
  23. &lt;html&gt;
  24. &lt;head&gt;
  25. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  26. &lt;title&gt;JSP Page&lt;/title&gt;
  27. &lt;/head&gt;
  28. &lt;body&gt;
  29. &lt;h1&gt;Retrieve data from database in jsp&lt;/h1&gt;
  30. &lt;table border=&quot;1&quot;&gt;
  31. &lt;tr&gt;
  32. &lt;th&gt;Name&lt;/th&gt;
  33. &lt;th&gt;Strasse&lt;/th&gt;
  34. &lt;th&gt;PLZ&lt;/th&gt;
  35. &lt;th&gt;Ort&lt;/th&gt;
  36. &lt;/tr&gt;
  37. &lt;%
  38. try{
  39. connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  40. statement=connection.createStatement();
  41. String sql =&quot;select * from kunde&quot;;
  42. resultSet = statement.executeQuery(sql);
  43. while(resultSet.next()){
  44. %&gt;
  45. &lt;tr&gt;
  46. &lt;td&gt;&lt;%=resultSet.getString(&quot;name&quot;) %&gt;&lt;/td&gt;
  47. &lt;td&gt;&lt;%=resultSet.getString(&quot;strasse&quot;) %&gt;&lt;/td&gt;
  48. &lt;td&gt;&lt;%=resultSet.getString(&quot;plz&quot;) %&gt;&lt;/td&gt;
  49. &lt;td&gt;&lt;%=resultSet.getString(&quot;ort&quot;) %&gt;&lt;/td&gt;
  50. &lt;/tr&gt;
  51. &lt;%
  52. }
  53. connection.close();
  54. }
  55. catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. %&gt;
  59. &lt;/table&gt;
  60. &lt;/body&gt;
  61. &lt;/html&gt;

In the log-files i found two of the following error messages:

  1. An error occurred at line: [41] in the jsp file: [/index.jsp]
  2. userid cannot be resolved to a variable
  3. 38:
  4. 39: &lt;%
  5. 40: try{
  6. 41: connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  7. 42: statement=connection.createStatement();
  8. 43: String sql =&quot;select * from kunde&quot;;
  9. 44: resultSet = statement.executeQuery(sql);
  10. Stacktrace:] with root cause
  11. org.apache.jasper.JasperException: Unable to compile class for JSP:
  12. An error occurred at line: [41] in the jsp file: [/index.jsp]
  13. userid cannot be resolved to a variable
  14. 38:
  15. 39: &lt;%
  16. 40: try{
  17. 41: connection = DriverManager.getConnection(connectionUrl+database, userid, password);
  18. 42: statement=connection.createStatement();
  19. 43: String sql =&quot;select * from kunde&quot;;
  20. 44: resultSet = statement.executeQuery(sql);

And

  1. [Warning] Access denied for user &#39;XY&#39;@&#39;localhost&#39; (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.

huangapple
  • 本文由 发表于 2020年9月14日 23:52:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定