为banksystem增加一个用户注册操作

前端代码

enroll界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/12/7
Time: 22:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
<style type="text/css">
body{background-color: #F5DEB3;}
div{
position: absolute;
top: 200px;
left: 580px;
border-radius: inherit;
}
</style>
</head>
<body>
<!--对于祖册功能 其实就是一个表单-->
<div>
<h1>注册界面</h1>
<form action="login" method="post">
<h2>请输入用户id:<input type="text" name="id"/></h2>
<h2>请输入密码:&nbsp;&nbsp;&nbsp;<input type="text" name="password"></h2>
<h2>请输入用户姓名:<input type="text" name="username"></h2>
<input type="reset" value="重置">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="enroll" name="method">
</form>
</div>
</body>
</html>

enrollsuccess界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 9:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册成功</title>
</head>
<body>
<a href="login.jsp"><h1>注册成功,请点击重新登录</h1></a>
</body>
</html>

enrollError界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 9:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册失败</title>
</head>
<body>
<a href="login.jsp"><h1>注册失败,用户ID已存在,请点击重新注册</h1></a>
</body>
</html>

后端代码

userDao层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@Override
public boolean enroll(String ID, String password, String userName) throws SQLException {
//预编译sql 在预编译SQL前 必须判断是否已经有人使用过这个ID了 因为这个银行操作系统不可能出现相同的id
//先通过查询 岔开是否数据库里已经有个这个ID
PreparedStatement pstm = null;
ResultSet resultSet = null;
boolean flag = false;
//对于刚开户的用户 其余额理应是0
double money = 0;
//预编译SQL
String sql = "SELECT * FROM bank_system WHERE ID = ?";
//编写这个Sql查询需要的参数
Object[] Params = {ID};
//调用BaseDao层进行查询,查看查询结果
Connection connection ;
connection = BaseDao.getConnection();
try {
resultSet = BaseDao.exectue(connection, pstm, resultSet, sql, Params);
} catch (Exception e) {
e.printStackTrace();
}
//如果查询结果不为空 那么说明在数据库里已经存在了这个数据 直接返回创建失败
if (resultSet.next()) {
return false;
} else {
//如果没有ID,就继续向下执行
//编写创建成员的sql
PreparedStatement pstm1 = null;
Connection connection1 ;
connection1 = BaseDao.getConnection();
//预编译sql
String sql1 = "INSERT INTO bank_system(ID,PASSWORD,username,money)\n" +
"VALUES(?,?,?,?)";
Object[] params = {ID, password, userName, money};
//调用BaseDao来操作数据
try {
int flag1 = BaseDao.exectue(connection1, pstm1, sql1, params);
//如果返回非0 这说明创建成功
if (flag1 != 0) {
flag = true;
} else {
flag = false;
}


} catch (Exception e) {
e.printStackTrace();
}
}
//在使用晚资源后 及时关闭所有资源
try {
BaseDao.closeResource(connection,pstm,resultSet);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(flag);
return flag;
}

userService层

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public boolean enroll(String ID, String password, String username) {
boolean flag = false;
System.out.println("执行到了service层");
userDaoimp userDaoimp = new userDaoimp();
try {
flag = userDaoimp.enroll(ID, password, username);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
System.out.println("service");
return flag;
}

servlet层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public void enroll(HttpServletRequest req,HttpServletResponse resp){
boolean flag = false;
String ID = req.getParameter("id");
String password = req.getParameter("password");
String username = req.getParameter("username");
System.out.println(ID);
System.out.println(password);
System.out.println(username);
userServiceImp userServiceImp = new userServiceImp();
flag = userServiceImp.enroll(ID,password,username);
if (flag){
try {
resp.sendRedirect("enrollsuccess.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
resp.sendRedirect("enrollerror.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}public void enroll(HttpServletRequest req,HttpServletResponse resp){
boolean flag = false;
String ID = req.getParameter("id");
String password = req.getParameter("password");
String username = req.getParameter("username");
System.out.println(ID);
System.out.println(password);
System.out.println(username);
userServiceImp userServiceImp = new userServiceImp();
flag = userServiceImp.enroll(ID,password,username);
if (flag){
try {
resp.sendRedirect("enrollsuccess.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
resp.sendRedirect("enrollerror.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信