Fork me on GitHub

Runtime类

Runtime类用于表示java虚拟机运行时的状态,它用于封装java虚拟机进程。

通过getRuntime( )方法获取与之相关的Runtime对象

avaiProcessors( )方法和freeMemory( )方法和maxMemory()方法

代码实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package page03;
import java.io.IOException;
public class Strinh {
public static void main(String[] args) throws IOException, InterruptedException {
/*表示java虚拟机运行时的状态*/
/*Runtime类中提供】一个exec()方法,这个方法执行DOS命令*/
Runtime rt = Runtime.getRuntime();
/*availablePracessors()方法用来获得处理器的个数*/
/*freeMemory()方法用来获得空闲内存大小*/
/*maxMemory用来获得最大可用内存大小*/
System.out.println("处理器的个数:"+rt.availableProcessors()+"个");
System.out.println("空闲内存大小:"+rt.freeMemory()/1024/1024+"M");
System.out.println("最大可用内存大小"+rt.maxMemory()/1021/1024+"M");
Process process = rt.exec("notepad.exe");
Thread.sleep(3000);
process.destroy();

}
}

语法格式:

Runtime run = Runtime.getRuntime();

exec( )方法

Runtime类中提供了一个exec( )方法,该方法用于执行一个DOS命令,从而实现和命令框中输入DOS命令相同的效果

destroy( )方法

Runtime类中提供的destroy( )方法,是用来关闭打开的文件

System类

System类的常用方法

方法说明 功能描述
static void exit(int status) 该方法用来终止当前正在运行的java虚拟机,其中参数static表示状态码,若状态码非0,则表示异常终止
static void gc() 运行垃圾回收器,并对垃圾进行回收(只是提醒虚拟机进行回收,实际不知道是否真的回收)
static native long currentTimeMillis() 返回以毫秒为单位的当前时间
static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) 从src引用的指定源数组拷贝到dest引用的数组,拷贝从指定的位置开始,到目标数组的指定位置结束
static Properties getProperties() 取得当前的系统属性
static String getProperty(String key) 获取指定键描述的系统属性

getProprety()方法

getProprety方法用于获取当前系统的全部属性,该方法会返回一个Propreties对象,其中封装了系统的所有属性,这些属性是以键值对的形式存在。

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package page03;

import java.util.Properties;
import java.util.Set;

public class System01 {

public static void main(String[] args) {
Properties pr = System.getProperties();
System.out.println(pr);
Set<String>propertyNames = pr.stringPropertyNames();
for(String key:propertyNames){
String value = System.getProperty(key);
System.out.println(key+"---->"+value);
}

}
}

currentTimeMills()

currentTimeMills()方法返回一个long类型的值,表示当时时间与1970年1月1日0点0分0秒之间的差,单位是毫秒,通常也将该值称作时间戳。

两个时间戳之差能够求出程序的运行时间

代码实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class currentTimeMills01 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int sum=0;
for(int i=0;i<1000000000;i++){
sum=sum+i;
}
long endTime = System.currentTimeMillis();
System.out.println("程序的运行时间为:"+(endTime-startTime)+"毫秒");

}
}

arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

  • src:表示源数组
  • dest:表示目标数组
  • srcPos:表示原数组中拷贝元素的起始位置
  • destPos:表示拷贝到目标元素的起始位置
  • length:表示拷贝元素的个数

java构造器详解

定义

类的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。

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
package way;
class Person{
//一个类及时什么也不写,他也会存在一个方法
String name;
//构造器第一个功能:实例化一些初始值
//使用new关键字,必须使用构造器
//无参构造
//Alt + insert可以直接生成构造器


public Person() {
}



//有参构造,一旦使用有参构造,就必须显示定义无参构造
public Person(String name){
this.name = name;
}

}

public class way03 {
public static void main(String[] args) {
//用new关键词声明了一个person对象
Person person = new Person("XXX");
System.out.println(person.name);
}

}

构造器的特点

  • 构造器的名字必须和类的名字相同
  • 构造器必须没有返回类型,也不能写void
  • 使用new关键字,必须使用构造器
  • 一个类及时什么也不写,他也会存在一个方法

注释:可以通过java.class文件可以发现其中有一个方法

构造器的作用

  • 构造器第一个功能:实例化一些初始值
  • 使用new关键字,必须使用构造器

构造器的方便构造

Alt + insert可以直接生成构造器

计算器原始代码(面向过程)

计算器原始代码(面向过程)

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

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class testCale {
public static void main(String[] args) {
new Cale();
}
}
class Cale extends Frame{
public Cale(){
//在窗口上 我们需要三个文本框 一个标签 一个按钮
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(11);

Label label = new Label("+");

Button button = new Button("=");
button.addActionListener(new MyCale(num1,num2,num3)); //将三个文本框作为参数传递给下边的方法
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});

}

}
class MyCale implements ActionListener{

//获取三个变量
//将第三个变量的值设为前两个变量的和
//将前两个变量清空
private TextField num1,num2,num3; //设置三个本地的参数来承接传递过来的参数
public MyCale(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;

}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());//将字符串类型强制转换成int类型的方法
int n2 = Integer.parseInt(num2.getText());

num3.setText(""+(n1+n2));

num1.setText("");
num2.setText("");

}
}

Random类

在math中有random()方法,但是,java还提供类一个Random类,这些类能够提供更加丰富多样的随机数

Random的构造方法

方法说明 功能描述
Random( ) 用于创建一个随机数生成器,每次实例化Random对象会产生不同的随机数
Random(long seed) 使用一个long类型的seed种子,当seed相同时,每次Random实例会生成相同的随机数

与C语言不同的是,Random( )方法中种子是随机给出的

Random类的常用方法

方法声明 功能描述
boolean nextBoolean() 随机生成boolean类型随机数
double nextDouble() 随机生成double类型的随机数
float nextFloat() 随机生成float类型的随机数
int nextInt() 随机生成int类型的随机数
int nextInt(n) 随机生成从[0,n)之间int类型的随机数
long nextLong() 随机生成long类型的随机数

代码实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package page03;

import java.util.Random;

public class Random01 {
public static void main(String[] args) {
Random r = new Random();
System.out.println("这是一个随机的boolean类型的随机数"+r.nextBoolean());
System.out.println("这是一个int类型的随机数"+r.nextInt());
System.out.println("这是一个double类型的随机数"+r.nextDouble());
System.out.println("这一个float类型的随机数"+r.nextFloat());
System.out.println("这是一个产生从0~10的int类型的随机数"+r.nextInt(10));
System.out.println("这是一个产生long类型的随机数"+r.nextLong());

}
}

JdbcUtils

JdbcUtils

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

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

public class JdbcUtils {
//提升作用域
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
// 这是一个静态代码块 因此 再倒入这个类时 只会加载一次
static {
try{
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); //这里是去获取配置文件
Properties properties = new Properties(); //这是一个流
properties.load(in); //加载配置文件
driver = properties.getProperty("driver");//这就是获取配置文件中数值的方法
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
Class.forName(driver);//加载mysql
}catch (Exception e){
System.out.println("出错了!!!");
}finally {

}
}
//获取链接
//方法会返回一个Connection类型的数据
public static Connection getConnection() throws Exception {
return DriverManager.getConnection(url, username, password);
}
//释放资源
public static void release(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
}
}

注意:这个JdbcUtils工具类需要放在调用它的类的同一个包下

db.properties

1
2
3
4
5
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEnconding=utf8&useSSL=true
username=dwx
password=123456

注意:这个配置需要放在src目录下

操作实例

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

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

public class JDBCSecond {
public static void main(String[] args) throws Exception {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();//获取数据库链接
statement = connection.createStatement();//获得sql的执行对象
String sql = "INSERT INTO dwx(id,`name`,`sex`)" +
"VALUES(6,'kti','m')"; //有了sql命令
int i = statement.executeUpdate(sql);//将sql丢到sql执行对象里边就行了
if(i>0){
System.out.println("插入成功!!!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.release(connection,statement,resultSet); //调用工具类来释放资源
}

}
}

servlet优化

servlet web.tml配置 以及 maven servlet jsp依赖

servlet web.tml配置

1
2
3
4
5
6
7
8
<?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">
</web-app>

maven servlet jsp依赖

1
2
3
4
5
6
7
8
9
 
<!--servlet依赖-->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>

1
2
3
4
5
6
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>

实现SMBMS

实现SMBMS

实现这个SMBMS用到了包括servlet jsp jstl standard mysql等的依赖

由于Tomcat版本过高原因,javax已经不再适用 而替换成了jakarta

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
<!--jsp的依赖-->
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>

<!--jar包的依赖-->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--这个项目里我自己倒的包-->
<!--servlet 5.0-->
<!--servlet依赖-->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!--jsp2.2-->
<!--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>
<!--standard-->
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>

为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();
}
}
}

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();
}
}

}
}

  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信