900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java servlet 用户登录_JSP+Servlet+JavaBean实现用户注册登录

java servlet 用户登录_JSP+Servlet+JavaBean实现用户注册登录

时间:2022-05-18 17:29:21

相关推荐

java servlet 用户登录_JSP+Servlet+JavaBean实现用户注册登录

整个项目的整体结构

image.png

文件结构

1. 首先创建一张数据表,用于存储用户信息

create table tb_user(

id int(4) primary key auto_increment,

username varchar(50),

password varchar(50),

sex varchar(50),

question varchar(50),

answer varchar(50),

email varchar(50));

image.png

2. JavaBean设计

实体类设计(放在entity包下面)

public class User {

private int id;

private String username;

private String password;

private String sex;

private String question;

private String answer;

private String email;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

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 getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getQuestion() {

return question;

}

public void setQuestion(String question) {

this.question = question;

}

public String getAnswer() {

return answer;

}

public void setAnswer(String answer) {

this.answer = answer;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

}

2.数据库连接工具类(放在util包下面)

public class DataBaseUtil {

//连接数据库

public static Connection getConn() {

Connection conn = null;

try {

//加载数据库驱动

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

String url = "jdbc:mysql://localhost:3306/jdbcdemo?useUnicode=true&characterEncoding=UTF-8";

try {

conn = DriverManager.getConnection(url, "root", "root");

if (conn != null) {

System.out.println(1123);

}

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

//关闭数据库

public static void closeConn(Connection conn){

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。