投票系统
实现结果图
(1)JDBC数据库连接工具类conn.java
package com.gx.bean;
import java.sql.*;
public class conn {
public Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3308/manage","root","yww1352465324");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
return conn;
}
public PreparedStatement prepare(Connection conn,String sql){
PreparedStatement pstmt = null;
try{
if (conn != null) {
pstmt = conn.prepareStatement(sql);
}
}catch (SQLException e){
e.printStackTrace();
}
return pstmt;
}
public Statement getStatement(Connection conn){
Statement stmt = null;
try{
if (conn != null) {
stmt = conn.createStatement();
}
}catch (SQLException e){
e.printStackTrace();
}
return stmt;
}
public ResultSet getResultSet(Statement stmt,String sql){
ResultSet rs = null;
if(stmt!=null){
try {
rs = stmt.executeQuery(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return rs;
}
public void excuteUpdate(Statement stmt,String sql){
if(stmt!=null){
try {
stmt.executeUpdate(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
conn=null;
}
}
public void close(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
rs=null;
}
}
public void close(Statement stmt){
if(stmt!=null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
stmt=null;
}
}
}
(2)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
(3)index.jsp
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %><%--
Created by IntelliJ IDEA.
User: 12526
Date: 2021/1/15
Time: 17:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>投票系统</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" />
<center