title: java课程大作业 银行操作系统

使用javaweb 实现前端和数据库的交互

前端资源

我自己写的前端资源是十分简单的 简约的 以HTML为主 简单的css 并没有JavaScript。使用的是.jsp

来实现前后端交互

登录界面

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
59
60
61
62
63
64
65
66
67
68
69
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 8:59
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>login</title>
<style type="text/css">
h1{align-content: center;text-align: center;}
body{background-color:wheat}
#login{

width: 280px;
height: auto;
background-color:wheat;

position: fixed;
top: 260px;
left: 600px;

border-width: 10px;
border-style: solid;
border-color:wheat;
}
#reset{
position: fixed;
top: 400px;
left: 620px;
}
#login1{
position: fixed;
top: 400px;
left: 820px;
}
#title{
position: fixed;
top: 180px;
left: 640px;

}

</style>

</head>
<body>
<!--action接的是登录的servlet-->
<div>
<div>
<h1 id="title">银行系统登录</h1>
</div>
<div id="login">
<form method="post" action="login">
<h4>银行卡号:<input type="text" name="id"/><br/></h4>
<h4>银行密码:<input type="password" name="password"/><br/></h4>
<input id="login1" type="submit" value="login" name="method">

<input id="reset" type="reset" value="重置">
</form>
</div>
</div>
</body>
</html>

登录失败页面

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:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ERROR</title>
</head>
<body>
<a href="login.jsp"><h1>用户名或密码错误,点击重新登录</h1></a>
</body>
</html>

操作页面

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
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 9:25
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;
}
#center{
width: 260px;
position: absolute;
top: 200px;
left: 580px;
border-radius: inherit;
}
.button{
align-self: center;
}

</style>
</head>
<body>
<!--头部的div-->
<div id="head">
<h1 align="center">操作界面</h1>
</div>
<div id="center">
<form method="post" action="login">
<h2 align="center">欢迎您${username}</h2>
<a href="in.jsp"><input type="button" value="存钱"></a><br/>
<a href="get.jsp"><input type="button" value="取钱"></a><br/>
<a href="view.jsp"><input type="button" value="查看余额"><br/></a>
<a href="changepassword.jsp"><input type="button" value="修改密码"><br/></a>
<input type="submit" name="method" value="layout"><br/>
</form>
</div>


</body>
</html>


取钱页面

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/11/30
Time: 9:26
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>
body{
background-color: #F5DEB3;
}
div{
width: 300px;
background-color: #F5DEB3;
position: absolute;
top: 200px;
left: 580px;
}
</style>
</head>
<body>
<div>
<h1>取款</h1>
<form method="post" action="login">
<h2>取钱金额:<input type="text" name="moneyin"></h2>
<input type="reset" value="重置">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="click" type="submit" name="method" value="get">
</form>
</div>
</body>
</html>

取钱失败页面 / 取钱成功页面

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="service.jsp"><h1>取钱失败,余额不足,点击跳转到操作页面</h1></a>
</body>
</html>

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="service.jsp"><h1>取钱成功,点击跳转到操作页面</h1></a>
</body>
</html>

存钱界面

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 10:16
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>
body{
background-color: #F5DEB3;
}
div{
width: 300px;
background-color: #F5DEB3;
position: absolute;
top: 200px;
left: 580px;
}
</style>
</head>
<body>
<div>
<h1>存款</h1>
<form method="post" action="login">
<h2>存储金额:<input type="text" name="moneyin"></h2>
<input type="hidden" name="moneyin">
<input type="reset" value="重置">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="click" type="submit" name="method" value="in">
</form>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 10:16
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>
body{
background-color: #F5DEB3;
}
div{
width: 300px;
background-color: #F5DEB3;
position: absolute;
top: 200px;
left: 580px;
}
</style>
</head>
<body>
<div>
<h1>存款</h1>
<form method="post" action="login">
<h2>存储金额:<input type="text" name="moneyin"></h2>
<input type="hidden" name="moneyin">
<input type="reset" value="重置">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="click" type="submit" name="method" value="in">
</form>
</div>
</body>
</html>

存钱成功界面/存钱失败页面

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: 10:22
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="service.jsp"><h1>存钱成功,点击跳转到操作页面</h1></a>
</body>
</html>

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: 10:23
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="service.jsp"><h1>存钱失败,点击跳转到操作页面</h1></a>
</body>
</html>

修改密码页面

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
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2021/11/30
Time: 10:45
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: #FAEBD7;
}
div{
position: absolute;
top: 200px;
left: 600px;
}
</style>
</head>
<body>
<div>
<h1>修改密码</h1>
<form action="login" method="post">
<h3>请输入旧密码:<input type="text" name="oldpassword"></h3>
<h3>请输入新密码:<input type="text" name="newpassword"></h3>
<input type="submit" 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;<input type="submit" value="change" name="method"/>
</form>
</div>
</body>
</html>

修改密码成功页面 / 修改密码失败页面

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: 10:52
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>

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: 10:51
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="changepassword.jsp"><h1>旧密码错误,点击重新输入</h1></a>
</body>
</html>

查看余额页面

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/11/30
Time: 10:11
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: antiquewhite;

}
div{
position: absolute;
top: 200px;
left: 600px;
}
</style>
</head>
<body>
<div>
<h1>查看存款</h1>
<form action="login" method="post">
<h2>客户${username},您的存款数目为${money}元</h2><br/>
<a href="service.jsp"><h4>点击返回到操作页面</h4></a>
</form>
</div>
</body>
</html>


后端代码

所有的依赖

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>BankSystem</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>BankSystem Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--mysql依赖-->
<!-- https://mvnrepository.com/artifact/org.wisdom-framework/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.3.04</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>


</dependencies>

<build>
<finalName>BankSystem</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.dwx.servlet.customerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>

后端代码

DAO层

basedao

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.dwx.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//这是一个最基础的Dao类 是用来和数据库进行交互的
//通过分析 我们需要用到的jdbc操作 包括 查 改
public class BaseDao {
//获取和数据库的联结
public static String driver;
public static String url;
public static String username;
public static String password;
//通过重定向 到db.propertries中获取到数据
//定义的static参数 通过static代码块 对其进行初始化
static{
InputStream stream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(stream);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
//与数据库进行链接
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}

return connection;
}
//创建用于改的execute
//编写查询公共类
public static ResultSet exectue(Connection connection, PreparedStatement prepareStatement, ResultSet resultSet, String sql, Object[] params) throws Exception {
try {
prepareStatement = connection.prepareStatement(sql);
//不清楚这里的prepareStatement与setStatement有啥区别
//prepareStream是预编译 是解决数据库泄露问题时讲过的
for (int i = 0; i < params.length; i++) {
prepareStatement.setObject(i + 1, params[i]);
}
} catch (Exception throwables) {
throwables.printStackTrace();
}
resultSet = prepareStatement.executeQuery();
return resultSet;
}

//增删改公共方法
public static int exectue(Connection connection,PreparedStatement prepareStatement,String sql,Object[] params) throws Exception {
try {
prepareStatement = connection.prepareStatement(sql);
//不清楚这里的prepareStatement与setStatement有啥区别
for (int i=0;i<params.length;i++){
prepareStatement.setObject(i+1,params[i]);
}
} catch (Exception throwables) {
throwables.printStackTrace();
}
int updataRows = prepareStatement.executeUpdate();
return updataRows;


}
//释放资源
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) throws Exception {
boolean flag=true;
if (resultSet!=null){
resultSet.close();
resultSet = null;
}
if (preparedStatement!=null){
preparedStatement.close();
preparedStatement=null;
}
if (connection!=null){
connection.close();
}
return flag;

}

}

monyDao

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.dwx.dao.money;

import java.sql.SQLException;

public interface moneyDao {
//对于金钱的操作有三种 产看余额 存款 取款
//查看余额 就是一个查询语句 返回值是int类型 money
//通过 ID 和 Password来获得余额
double getMoney(String ID,String password) throws SQLException;
//存取款 ID password moneyIn
boolean changeMoney(String ID,String password,double money,double moneyIn);
}

moneyDaoImp

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
59
60
61
62
63
64
65
package com.dwx.dao.money;

import com.dwx.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class moneyDaoImp implements moneyDao {
@Override
public double getMoney(String ID, String password) throws SQLException {
//先进行数据库链接
Connection connection;
connection = BaseDao.getConnection();
double money=0;
//写预编译sql
PreparedStatement pstm=null;
String sql = "SELECT money FROM bank_system WHERE ID = ? AND Password = ?";
//编写一个参数集 用来传递参数
Object[] param = {ID,password};
ResultSet result = null;
//编写一个resul来获得查询的返回值
try {
result = BaseDao.exectue(connection,pstm,result,sql,param);
} catch (Exception e) {
e.printStackTrace();
}
//得到了result
if (result.next()){
money = result.getDouble("money");
}
return money;
}

@Override
public boolean changeMoney(String ID, String password,double money,double moneyIn) {
if (money+moneyIn<0){
return false;
}
boolean flag = false;
//创建连接
Connection connection;
connection = BaseDao.getConnection();
//编写预编译SQL
int r = 0;
PreparedStatement pstm = null;
String sql = "UPDATE bank_system SET money = ? WHERE ID = ? AND Password = ?";
//编写param表示参数集
//这里 用money + moneyIn表示改变后的值 若moneyIn为正 则就是存储 要是负 则是取钱
Object[] param = {money+moneyIn,ID,password};
//调用Base进行改值
try {
r = BaseDao.exectue(connection,pstm,sql,param);
} catch (Exception e) {
e.printStackTrace();
}
if (r!=0){
flag=true;
}
System.out.println("Dao"+flag);
return flag;
}
}

userDao

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.dwx.dao.user;

import java.sql.SQLException;

//这个抽象类 主要是对user进行操作 如 修改密码 增加用户 删除用户 等等
public interface userDao {
//修改密码 修改密码需要用到 ID 和 old_password 进行比较后才能修改
//修改密码 返回布尔类型 表示是否修改成功
boolean changePassword(String ID,String oldPassword,String newPassword);
//用户登录 就是去数据库查询是否有此人 如有此人 则返回username 需要获得ID,password
String login(String ID,String password) throws SQLException;

}

userDaoImp

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
59
60
61
62
63
64
65
66
package com.dwx.dao.user;

import com.dwx.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class userDaoimp implements userDao{
@Override
public boolean changePassword(String ID, String oldPassword,String newPassword) {
//获得链接
boolean flag = false;
int resultSet = 0;
Connection connection;
connection = BaseDao.getConnection();
//预编译sql
PreparedStatement pstm = null;
String sql = "UPDATE bank_system SET Password=? WHERE ID=? AND Password=?";
//设置sql要插入的参数
Object[] param = {newPassword,ID,oldPassword};
try {
resultSet = BaseDao.exectue(connection,pstm,sql,param);
} catch (Exception e) {
e.printStackTrace();
}
if (resultSet!=0){
flag=true;
}
//关闭开启的资源
try {
BaseDao.closeResource(connection,pstm,null);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}

@Override
public String login(String ID, String password) throws SQLException {
Connection connection;
//注意 不要忘记connection
connection = BaseDao.getConnection();
String username=null;
//预编译一个SQL 只需要一个SELECT
PreparedStatement pstm=null;
//准备一个结果集 这个查询是要获得结果集的
String sql = "SELECT * From bank_system WHERE ID = ? AND Password = ?";
//创建一个param参数
Object[] param = {ID,password};
//用一个result来存储查询到的结果集
ResultSet resultSet = null;
try {
resultSet = BaseDao.exectue(connection,pstm,resultSet,sql,param);
} catch (Exception e) {
e.printStackTrace();
}
assert resultSet != null;
if (resultSet.next()){
username = resultSet.getString("username");
}
return username;
}
}

POJO

映射类

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
package com.dwx.pojo;
//这个类是映射到数据库中bank_system这个表
public class bank_system {
//创造几个对象 对应数据库中的几个参数
//设置的都是私有对象 需要通过set和get获取值
private int ID;
private String Password;
private String username;
private int money;

public int getID() {
return ID;
}

public void setID(int ID) {
this.ID = ID;
}

public String getPassword() {
return Password;
}

public void setPassword(String password) {
Password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public int getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}
}

service层

userservice

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.dwx.service.user;
//对于用户的一些操作

public interface userService {

//修改用户的密码 修改密码需要得到并传递给dao层 ID(int) newPasswor(String) oldPasswor(String)
//返回值类型为 boolen
boolean changePasswordService(String ID,String oldPassword,String newPassword);
//登录 需要给Dao ID Password 需要返回给test username
String login(String ID,String passWord);

}

userserviceImp

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
package com.dwx.service.user;

import com.dwx.dao.user.userDaoimp;

import java.sql.SQLException;

public class userServiceImp implements userService{
@Override
public boolean changePasswordService(String ID, String oldPassword, String newPassword) {
boolean flag;
userDaoimp daoimp = new userDaoimp();
flag = daoimp.changePassword(ID,oldPassword,newPassword);
return flag;
}

@Override
public String login(String ID, String passWord) {
String username = null;
userDaoimp daoimp = new userDaoimp();
try {
username = daoimp.login(ID,passWord);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return username;
}
}

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.dwx.servlet;

import com.dwx.service.money.moneyServiceImp;
import com.dwx.service.user.userServiceImp;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class customerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
String method = req.getParameter("method");
System.out.println(method);
switch (method) {
case "login":
login(req, resp);
break;
case "get":
getmoney(req, resp);
break;
case "in":
insertmoney(req, resp);
break;
case "layout":
logout(req, resp);
break;
case "change":
changepassword(req, resp);
break;
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
doGet(req, resp);
}
public void login(HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
System.out.println(id);
String password = req.getParameter("password");
System.out.println(password);
double money;
//用得到的数据进行联结
userServiceImp userServiceImp = new userServiceImp();
moneyServiceImp moneyServiceImp = new moneyServiceImp();
money = moneyServiceImp.getMoney(id,password);
//登录
String username = userServiceImp.login(id,password);
//如果登录成功就跳转到service.jsp页面
//如果登陆失败 就跳转到error.jsp页面
if (username==null){
try {
resp.sendRedirect("error.jsp");
} catch (IOException ignored) {

}
}else {
//如果联结成功 就把用户名 账号 密码 都放到session里边 以便其他的程序使用
req.getSession().setAttribute("id",id);
req.getSession().setAttribute("password",password);
req.getSession().setAttribute("username",username);
req.getSession().setAttribute("money",money);
try {
resp.sendRedirect("service.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}

}
public void getmoney(HttpServletRequest req,HttpServletResponse resp){
//经过登录操作 已经把id,password,money等数据都存放到session里了 需要的话直接去session里拿
String id = (String) req.getSession().getAttribute("id");
String password = (String) req.getSession().getAttribute("password");
double money = (Double) req.getSession().getAttribute("money");
System.out.println(id);
System.out.println(password);
System.out.println(money);
//从前端获取要取出的数值 再去service层进行操作
double moneyin = Double.parseDouble(req.getParameter("moneyin"));
//设置一个flag来分辨是否取钱成功
boolean flag;
moneyServiceImp moneyServiceImp = new moneyServiceImp();
flag = moneyServiceImp.changeMoney(id,password,money,-moneyin);
//如果取钱成功 跳转到取钱成功页面
//取钱成功 及时更改session里的money的值
req.getSession().setAttribute("money",moneyServiceImp.getMoney(id,password));
if (flag){
try {
resp.sendRedirect("getSuccess.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
//如果取钱失败 跳转到取钱失败页面
else {
try {
resp.sendRedirect("getError.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void insertmoney(HttpServletRequest req,HttpServletResponse resp){
//与取款一样 存款需要从session里边获取数值
String id = (String) req.getSession().getAttribute("id");
String password = (String) req.getSession().getAttribute("password");
double money = (Double) req.getSession().getAttribute("money");
//再拿到前端输入的需要存款的数值
double moneyin = Double.parseDouble(req.getParameter("moneyin"));
//调用service层对数据进行操作哦
moneyServiceImp moneyServiceImp = new moneyServiceImp();
//设置一个布尔值 来判断是否操作成功
boolean flag;
flag = moneyServiceImp.changeMoney(id,password,money,moneyin);
if (flag){
//如果操作成功 跳转到存款成功页面 并且更新session中money的值
req.getSession().setAttribute("money",moneyServiceImp.getMoney(id,password));
try {
resp.sendRedirect("insuccess.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}else {
//如果没有成功 则跳转到存钱失败页面
try {
resp.sendRedirect("inError.jsp");
} catch (IOException e) {
e.printStackTrace();
}

}

}
public void logout(HttpServletRequest req, HttpServletResponse resp){
//退出操作
//退出操作的具体思路就是 将session里的id和password都换成null并且跳转到登录页面
req.getSession().setAttribute("id",null);
req.getSession().setAttribute("password",null);
try {
resp.sendRedirect("login.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
public void changepassword(HttpServletRequest req, HttpServletResponse resp){
//从session里获得数据
String id = (String) req.getSession().getAttribute("id");
String password = (String) req.getSession().getAttribute("password");
//从前端获得数据
String passwordin = req.getParameter("oldpassword");
System.out.println(passwordin);
String newpassword = req.getParameter("newpassword");
System.out.println(newpassword);
//如果前端获取的oldpassword与实际的password不同 跳转到Error页面
//如果通过了 就往下继续执行
userServiceImp userServiceImp = new userServiceImp();
//创造一个布尔值来标记是否修改成功
boolean flag;
flag = userServiceImp.changePasswordService(id,password,newpassword);
if (flag)
{
//如果修改成功 就跳转到成功页面
try {
resp.sendRedirect("changesuccess.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}else {
//如果没成功就送到失败页面
try {
resp.sendRedirect("changeError.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:

请我喝杯咖啡吧~

支付宝
微信