<% //连接数据库 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8"; String username = "root"; String password = "123456"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM vote"); while(rs.next()){ String title = rs.getString("title"); %> <input type="radio" name="option" value="<%=title%>"><%=title%><br> <% } } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } %> <a href="showVote.jsp">提交投票</a>选中后数据库数据变化
时间: 2024-02-14 18:18:30 浏览: 168
根据您的代码,您想要实现的功能是:在页面上展示投票选项列表(从数据库中查询出来),用户在选中一个选项后,点击“提交投票”按钮,将投票结果存储到数据库中。
您想要实现的选中后数据库数据变化,可能是指将用户的投票结果存储到数据库中。在提交投票的页面中,您可以编写一个表单,其中包含一个隐藏的投票ID字段和一个单选框选项,用户选中后提交表单。后台代码根据投票ID和选中的选项,将投票结果存储到数据库中。
这里提供一个简单的代码示例:
1. 提交投票页面(submitVote.jsp):
```html
<form action="voteHandler.jsp" method="post">
<% // 根据你的数据库结构修改以下代码
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8";
String username = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM vote");
while(rs.next()){
int id = rs.getInt("id");
String title = rs.getString("title");
%>
<input type="radio" name="option" value="<%=id%>"><%=title%><br>
<% }
} catch (Exception e) {
e.printStackTrace();
}
finally {
//关闭连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
<input type="hidden" name="voteId" value="<%=voteId%>">
<input type="submit" value="提交投票">
</form>
```
2. 处理投票请求(voteHandler.jsp):
```java
<% // 获取用户选择的选项和投票ID
String option = request.getParameter("option");
String voteId = request.getParameter("voteId");
// 根据你的数据库结构修改以下代码
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8";
String username = "root";
String password = "123456";
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
pstmt = conn.prepareStatement("INSERT INTO vote_result(vote_id, option_id) VALUES (?, ?)");
pstmt.setInt(1, Integer.parseInt(voteId));
pstmt.setInt(2, Integer.parseInt(option));
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally {
//关闭连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
response.sendRedirect("voteResult.jsp?voteId=" + voteId); // 投票成功后跳转到投票结果页面
%>
```
3. 投票结果页面(voteResult.jsp):
```html
<% // 获取投票ID并查询投票选项和结果
String voteId = request.getParameter("voteId");
// 根据你的数据库结构修改以下代码
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8";
String username = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT v.title, o.title AS option_title, COUNT(*) AS vote_count FROM vote v LEFT JOIN vote_option o ON v.id = o.vote_id LEFT JOIN vote_result r ON o.id = r.option_id WHERE v.id = " + voteId + " GROUP BY o.id");
while(rs.next()){
String title = rs.getString("title");
String optionTitle = rs.getString("option_title");
int voteCount = rs.getInt("vote_count");
%>
<p><%=optionTitle %>: <%=voteCount %> 票</p>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
//关闭连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
```
希望这个示例能够帮助您完成您的需求。
阅读全文