基本上我正在尝试遍历我的数据库并显示里面的所有信息。我理解这个概念,但我的实现显然是错误的,因为 Tomcat 不断给我以下错误:
Root cause:
javax.servlet.ServletException: Operation not allowed after ResultSet closed
任何帮助将非常感激。我知道问题出在我的 while 循环上,但我的知识有限,我无法发现我需要进行的更改。请随时更改代码。
<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://addr.to.db:3306/dbname";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>
<html>
<head>
<title>JSP Server Response</title>
</head>
<body>
<h1>JSP Server Response:</h1>
<!--Get information from HTML form for manipulation-->
<%
String songName = request.getParameter("newSongName");
String artistName = request.getParameter("newArtistName");
String password = request.getParameter("pass");
String radioResults = request.getParameter("dbIO");
%>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "userName", "password");
statement = connection.createStatement();
String query = "SELECT * FROM song_info;";
rs = statement.executeQuery(query);
//Add or Delete Information from DB
if(radioResults.equalsIgnoreCase("insert"))
{
statement.executeUpdate("INSERT INTO song_info (songTitle, artistName) VALUES ('"+artistName+"','"+songName+"')");
}
else if(radioResults.equalsIgnoreCase("delete"))
{
statement.executeUpdate("DELETE FROM song_info WHERE artistName='"+songName+"'");
}
%>
<table>
<tr>
<th>Song Name</th><th>Artist Name</th>
</tr>
<% while(rs.next()){ %>
<tr>
<td align=center> <%= rs.getString("songTitle")%></td>
<td align=center> <%= rs.getString("artistName")%></td>
</tr>
<%}%>
</table>
<%
rs.close();
statement.close();
connection.close();
%>
Information Submitted to Server:
Artist Name: <%=songName%> <br />
Song Name: <%=artistName%> <br />
Password: <%=password%><br />
Radio Button Selection: <%=radioResults%><br /><br />
Password Correct:
</body>
</html>