package com.yyzy.dao;
import com.yyzy.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class UserInfoDao {
//注册用户
// insert into userinfo value(null,username,password,null)
public boolean reg(String username,String password){
String sql = "insert into userinfo value(null,?,?,null)";
Connection conn = JDBCUtil.getConn();
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1,username);
pre.setString(2,password);
int b = pre.executeUpdate();
if (b==1){
return true;
}else{
return false;
}
} catch (SQLException e) {
System.out.println("注册用户sql语句有误:"+e);
return false;
}
}//登录
// select * from userinfo where username = ?,password = ?
public boolean login(String username,String password){
String sql = "select * from userinfo where username = ? and password = ?";
Connection conn = JDBCUtil.getConn();
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1,username);
pre.setString(2,password);
ResultSet rs = pre.executeQuery();
while(rs.next()){
return true;
}
return false;
} catch (SQLException e) {
System.out.println("登录用户sql语句有误:"+e);
return false;
}
}
}
package com.yyzy.dao;import com.yyzy.entity.UserInfo;
import com.yyzy.entity.VoteInfo;
import com.yyzy.util.JDBCUtil;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class VoteInfoDao {
//修改用户投票编号
//update userinfo set city = ? where username = ?
public boolean updateCity(int vid,String username){
String sql = "update userinfo set vid = ? where username = ?";
Connection conn = JDBCUtil.getConn();
try {
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1, vid);
pre.setString(2, username);
int i = pre.executeUpdate();
if (i == 1){
return true;
}else {
return false;
}
} catch (SQLException e) {
System.out.println("城市编号修改sql语句解析失败:"+e);
return false;
}
}//统计投票的结果
//select div,count(did) from userinfo group by did
public List<VoteInfo> QueryTicket(){
String sql = "select u.vid,v.city,count(vid) as number from userinfo as u,voteinfo as v where u.vid = v.uid group by u.vid";
List<VoteInfo> list = new ArrayList<VoteInfo>();
Connection conn = JDBCUtil.getConn();
try {
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()){
VoteInfo voteInfo = new VoteInfo(rs.getInt(1),rs.getString(2), rs.getInt(3));
list.add(voteInfo);
}
return list;
} catch (SQLException e) {
System.out.println("统计票sql语句解析失败:"+e);
return null;
}
}//投票的用户
public List QueryUser(){
String sql = "select id,username,password,city from userinfo,voteinfo where userinfo.vid = voteinfo.uid";
List list = new ArrayList();
Connection conn = JDBCUtil.getConn();
try {
PreparedStatement pre = conn.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()){
VoteInfo voteInfo = new VoteInfo();
voteInfo.setCity(rs.getString(4));
UserInfo userInfo = new UserInfo(rs.getInt(1),rs.getString(2),rs.getString(3),voteInfo);
list.add(userInfo);
}
return list;
} catch (SQLException e) {
System.out.println("查询投票用户sql语句解析失败:"+e);
return null;
}}
}
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
UserInfoDao ud;
@Override
public void init() throws ServletException {
ud = new UserInfoDao();
}@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("logname");
String password = req.getParameter("logpass");boolean b = ud.login(username,password);
if (b){
req.getSession().setAttribute("username", username);
resp.sendRedirect("vote.jsp");
}else {
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
}
@WebServlet("/QueryTicketServlet")
public class QueryTicketServlet extends HttpServlet {
VoteInfoDao vd;
@Override
public void init() throws ServletException {
vd = new VoteInfoDao();
}@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List list = vd.QueryTicket();
List obj = vd.QueryUser();
req.setAttribute("list", list);
req.setAttribute("obj", obj);req.getRequestDispatcher("total.jsp").forward(req, resp);
}
}
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
UserInfoDao ud;
@Override
public void init() throws ServletException {
ud = new UserInfoDao();
}@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("logname");
String password = req.getParameter("logpass");
boolean b = ud.reg(username, password);
resp.sendRedirect("login.jsp");}
}
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
VoteInfoDao vd;
@Override
public void init() throws ServletException {
vd = new VoteInfoDao();
}@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int vid = Integer.parseInt(req.getParameter("city"));
String username = req.getParameter("username");
boolean b1 = vd.updateCity(vid,username);if (b1){
resp.sendRedirect("vote.jsp");
}
}
}<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>翻转的登录注册页面</title>
<link rel="stylesheet" href="https://www.jq22.com/jquery/bootstrap-4.2.1.css">
<link rel="stylesheet" href="https://www.jq22.com/jquery/font-awesome.4.7.0.css">
<link rel="stylesheet" href="css/style.css">
</head><body>
<div class="section dwo">
<div class="container">
<div class="row full-height justify-content-center">
<div class="col-12 text-center align-self-center py-5">
<div class="section pb-5 pt-5 pt-sm-2 text-center">
<input class="checkbox" type="checkbox" id="reg-log" name="reg-log">
<div class="card-3d-wrap mx-auto">
<div class="card-3d-wrapper bgk">
<div class="card-front">
<div class="center-wrap">
<div class="section text-center">
<h4 class="mb-4 pb-3">登录</h4>
<form action="/LoginServlet" method="post">
<div class="form-group">
<input type="text" name="logname" class="form-style" placeholder="请输入用户名" id="logname" autocomplete="off">
<i class="fa fa-user-circle-o" aria-hidden="true"></i>
</div>
<div class="form-group mt-2">
<input type="password" name="logpass" class="form-style" placeholder="请输入密码" id="logpass" autocomplete="off">
<i class="fa fa-unlock-alt" aria-hidden="true"></i>
</div>
<input type="submit" class="btn mt-4" value="登录">
</form>
<div class="mb-0 mt-4 text-center">
<div class="flex-fill">
<p>没有帐号?<label for="reg-log">立即注册</p>
</div>
</div>
</div>
</div>
</div>
<div class="card-back">
<div class="center-wrap">
<div class="section text-center">
<h4 class="mb-4 pb-3">注册</h4>
<form action="/RegServlet" method="post">
<div class="form-group">
<input type="text" name="logname" class="form-style" placeholder="请输入用户名" id="logname" autocomplete="off">
<i class="fa fa-user-circle-o" aria-hidden="true"></i>
</div>
<div class="form-group mt-2">
<input type="password" name="logpass" class="form-style" placeholder="请输入密码" id="logpass" autocomplete="off">
<i class="fa fa-unlock-alt" aria-hidden="true"></i>
</div>
<input type="submit" class="btn mt-4" value="注册">
</form>
<p class="mb-0 mt-4 text-center">已有帐号?<label for="reg-log">立即登录</label></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body></html>
JavaWeb3
于 2023-12-10 21:17:56 首次发布