本次小项目:1.对于项目的分层 书写 2.需要mySQl数据库书写 3.JDBC链接数据库操作 4.html登录,跳转功能书写 5.用户实体类以及接口
一.对于项目的分层 书写
对于项目的分层 书写
bean包里:用户实体类
dao包里:用户功能类及接口
selvet包里:登录和注册的功能类
until包里:JDBC工具类
error.jsp:错误跳转页面
goods.jsp:商品页面
index.jsp:主页面
login.jsp:登录页面
zhuce.jsp:注册页面
javaweb项目包里
lib包里:数据库和selvet Jar包
二.需要mySQl数据库书写
建库: create database;
建表
三.JDBC链接数据库操作
// 定义 JDBCUtil 类
public class JDBCUtil {
// 定义数据库驱动类名
static String driver = "com.mysql.cj.jdbc.Driver";
// 定义数据库连接 URL
static String url = "jdbc:mysql:///你创建的数据库?useSSL=false&serverTimezone=UTC";
// 定义数据库用户名
static String username = "账号";
// 定义数据库密码
static String password = "设置密码";
// 定义连接对象,初始值为 null
static Connection conn = null;
public static Connection getCon() { // 定义获取连接对象的方法
try {
Class.forName(driver); // 加载数据库驱动程序
conn = DriverManager.getConnection(url, username, password); // 获取连接对象
} catch (Exception e) { // 处理异常
e.printStackTrace();
}
return conn; // 返回连接对象
}
//资源回收方法
// 定义关闭 ResultSet、PreparedStatement 和 Connection 对象的方法
public static void colse(ResultSet rs, PreparedStatement pstm, Connection con) throws SQLException {
if (rs != null) { // 如果 ResultSet 对象不为空
rs.close(); // 关闭 ResultSet 对象
}
if (pstm != null) { // 如果 PreparedStatement 对象不为空
pstm.close(); // 关闭 PreparedStatement 对象
}
if (con != null) { // 如果 Connection 对象不为空
con.close(); // 关闭 Connection 对象
}
}
// 定义关闭 PreparedStatement 和 Connection 对象的方法
public static void colse(PreparedStatement pstm, Connection con) throws SQLException {
if (pstm != null) { // 如果 PreparedStatement 对象不为空
pstm.close(); // 关闭 PreparedStatement 对象
}
if (con != null) { // 如果 Connection 对象不为空
con.close(); // 关闭 Connection 对象
}
}
}
四.html登录,跳转功能书写
1.index.jsp:主页面
2. login.jsp:登录页面
3. zhuce.jsp:注册页面
4.error.jsp:错误跳转页面
5. goods.jsp:商品页面
五.用户实体类以及接口
1.实体类
public class Userdaolmpl implements UserDao {
// 数据库连接、用户信息、数据库操作等相关变量的声明
Connection con = null; // 数据库连接
User user = null; // 用户信息
PreparedStatement pstm = null; // 数据库操作
ResultSet rs = null; // 数据库查询结果
int row = 0; // 数据库受影响行数
@Override
public User Login(String username, String password) {
// 获取数据库连接
con = JDBCUtil.getCon();
// 定义 SQL 查询语句
String sql = "select * from t_user where username = ? and password =?";
try {
// 准备预处理 SQL 语句
pstm = con.prepareStatement(sql);
// 设置预处理参数,防止 SQL 注入攻击
pstm.setObject(1, username);
pstm.setObject(2, password);
// 执行 SQL 查询,并获取查询结果
ResultSet rs = pstm.executeQuery();
// 处理查询结果
if (rs.next()) {
user = new User();
user.setUid(rs.getInt("uid"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
}
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
try {
// 关闭数据库连接、释放资源
JDBCUtil.colse(rs, pstm, con);
} catch (Exception e) {
e.printStackTrace();
}
}
// 返回用户信息
return user;
}
@Override
public int Zhuce(User user) {
// 获取数据库连接
con = JDBCUtil.getCon();
// 定义 SQL 插入语句
String sql = "insert into t_user(username,password,phone,address) values(?,?,?,?)";
try {
// 准备预处理 SQL 语句
pstm = con.prepareStatement(sql);
// 设置预处理参数,防止 SQL 注入攻击
pstm.setObject(1, user.getUsername());
pstm.setObject(2, user.getPassword());
pstm.setObject(3, user.getPhone());
pstm.setObject(4, user.getAddress());
// 执行 SQL 插入操作,并获取操作结果
row = pstm.executeUpdate();
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
try {
// 关闭数据库连接、释放资源
JDBCUtil.colse(pstm, con);
} catch (Exception e) {
e.printStackTrace();
}
}
// 返回插入结果
return row;
}
}
2.接口
public interface UserDao {
//登录
public User Login(String username, String password);
//注册
public int Zhuce(User user);
}
六.效果
1.主页面
2.
2.登录页面
3.注册页面
实现以上的功能就是本次小项目