登录
在Navicat创建数据库
![](https://img-blog.csdnimg.cn/5c9387c0a5b445058c212cd06787e72d.png)
登录页面
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/2/15
Time: 11:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h2>登录</h2>
<form action="login" method="post">
账户:<input type="text" name="username" value="" /><br/>
密码:<input type="password" name="password" value="" /><br/>
<input type="submit" value="登录" />
</form>
</body>
</html>
登陆成功页面
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/2/17
Time: 20:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主页面</title>
</head>
<body>
<h2>登陆成功!</h2>
</body>
</html>
登陆失败页面
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/2/17
Time: 20:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>错误</title>
</head>
<body>
<h2>登录失败!</h2>
<a href="login.jsp">返回登录</a>
</body>
</html>
在User类中写入实体、属性、方法
package bean;
public class User {
private Integer uid;
private String username;
private String password;
private String phone;
private String address;
public User() {
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
}
UserDaoImpl中写登录和注册的代码
package dao;
import bean.User;
import util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDaoImpl{
private Connection con=null;//数据库连接对象
private PreparedStatement ps=null;//预处理对象
private ResultSet rs=null;//结果集对象
private int row=0;//增删改受影响的行数
User login=null;
public User login(String username, String password) {
try {
con= JDBCUtil.getCon();
//3.定义sql语句
String sql = "select * from t_user where username=? and password=?";
//4.获取预处理对象
ps = con.prepareStatement(sql);
//5.传参
ps.setObject(1, username);
ps.setObject(2, password);
//6.执行sql语句
rs = ps.executeQuery();
//7.结果集处理
if (rs.next()) {
login=new User();
//从结果集获取数据 ,封装到实体类对象中
int uid=rs.getInt("uid");
login.setUid(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 {
JDBCUtil.close(rs,ps,con);
}
return login;
}
public int register(User user) {
try {
//1.获取数据库连接
con= JDBCUtil.getCon();
//2.定义sql语句
String sql="insert into t_user(username,password,phone,address) values(?,?,?,?)";
//3.获取预处理对象
ps=con.prepareStatement(sql);
//4.传参
ps.setObject(1,u