#判断存在即删除数据库
drop database if exists mydb;
#创建数据库
create database mydb;
#创建表
create table t_user
(
uid int primary key auto_increment,
username varchar(12),
password varchar(12),
phone varchar(12),
address varchar(24)
);
INSERT into user (username,password,phone,address) VALUES ('张三','666','12345678','河南');
INSERT into user (username,password,phone,address) VALUES ('李四','888','12345678','河南');
package com.w.beng;
public class User{
public Integer uid;
public String username;
public String password;
public String phone;
public String address;
public User() {
}
public User(Integer uid, String username, String password, String phone, String address) {
this.uid = uid;
this.username = username;
this.password = password;
this.phone = phone;
this.address = address;
}
/**
* 获取
* @return uid
*/
public Integer getUid() {
return uid;
}
/**
* 设置
* @param uid
*/
public void setUid(Integer uid) {
this.uid = uid;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取
* @return phone
*/
public String getPhone() {
return phone;
}
/**
* 设置
* @param phone
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "User{uid = " + uid + ", username = " + username + ", password = " + password + ", phone = " + phone + ", address = " + address + "}";
}
}
主页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
学习页面
登录
注册
登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
登录
注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
注册
package com.w.util;
import java.sql.*;
public class JDBCUtil {
static Connection conn=null;
public static Connection getcon(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&serverTimezone=UTC","root","root");
}catch (Exception e){
e.printStackTrace();
}
return conn;
}
public static void colse(ResultSet rs, PreparedStatement pstm,Connection con) throws SQLException {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (con != null) {
con.close();
}
}
public static void colse(PreparedStatement pstm,Connection con) throws SQLException {
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}
}
package com.w.dao.impl;
import com.w.beng.User;
import com.w.dao.UserDao;
import com.w.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDaoImpl implements UserDao {
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
User login=null;
int row=0;
@Override
public User login(String username, String password) {
con= JDBCUtil.getcon();
String sql="select * from t_user where username=? and password=?";
try {
pstm =con.prepareStatement(sql);
pstm.setObject(1,username);
pstm.setObject(2,password);
rs=pstm.executeQuery();
if(rs.next()){
login=new User();
login.setUid(rs.getInt("uid"));
login.setUsername(rs.getString("username"));
login.setPassword(rs.getString("password"));
login.setPhone(rs.getString("phone"));
login.setAddress(rs.getString("address"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
JDBCUtil.colse(rs,pstm,con);
}catch (Exception e){
e.printStackTrace();
}
}
return login;
}
@Override
public int register(User user) {
con = JDBCUtil.getcon();
String sql = "insert into t_user(username,password,phone,address) values(?,?,?,?)";
try {
pstm = con.prepareStatement(sql);
pstm.setObject(1, user.getUsername());
pstm.setObject(2, user.getPassword());
pstm.setObject(3, user.getPhone());
pstm.setObject(4, user.getAddress());
row = pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
JDBCUtil.colse(pstm, con);
} catch (Exception e) {
e.printStackTrace();
}
}
return row;
}
}
package com.w.dao;
import com.w.beng.User;
public interface UserDao {
public User login(String username, String password);
public int register(User user);
}
package com.w.Servlet;
import com.w.beng.User;
import com.w.dao.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/denglu")
public class denglu extends HttpServlet {
UserDaoImpl userDao=null;
User login=null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码格式
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
//以文本或网页形式相应
resp.setContentType("text/html;charset=utf-8");
//获取name为user中输入的数
String username=req.getParameter("user");
//获取name为pwd中输入的数
String password=req.getParameter("pwd");
userDao=new UserDaoImpl();
login =userDao.login(username,password);
if(login!=null){
resp.sendRedirect("zhuye.jsp");
System.out.println(login);
}else {
resp.sendRedirect("error.jsp");
}
}
}
package com.w.Servlet;
import com.w.dao.impl.UserDaoImpl;
import com.w.beng.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
@WebServlet("/zhuce")
public class zhuce extends HttpServlet {
Connection con = null;
PreparedStatement pstm = null;
User user = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
user =new User();
String username=req.getParameter("user");
String password=req.getParameter("pwd");
String phone=req.getParameter("phone");
String address=req.getParameter("address");
user.setUsername(username);
user.setPassword(password);
user.setPhone(phone);
user.setAddress(address);
UserDaoImpl userDao=new UserDaoImpl();
int row=userDao.register(user);
if(row>0){
resp.sendRedirect("denglu.jsp");
}else {
resp.sendRedirect("zhuce.jsp");
}
}
}
输入正确则
错误则