小言_互联网的博客

JDBC学习笔记

223人阅读  评论(0)

JDBC 分层开发(day1 银行系统的搭建)



JDBC分层开发

1.创建数据库信息


-- ----------------------------
-- 银行管理员表
-- ----------------------------
DROP TABLE IF EXISTS `bankmanager`;
CREATE TABLE `bankmanager` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,
  `mname` varchar(20) NOT NULL,
  `mpass` varchar(20) NOT NULL,
  PRIMARY KEY (`mid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- 添加数据
-- ----------------------------
INSERT INTO `bankmanager` VALUES ('1', 'admin', 'admin');

-- ----------------------------
-- 银行卡信息表
-- ----------------------------
DROP TABLE IF EXISTS `bankcard`;
CREATE TABLE `bankcard` (
  `cid` bigint(11) NOT NULL AUTO_INCREMENT,
  `cpassword` int(6) NOT NULL,
  `cmoney` double(9,0) NOT NULL,
  `ctype` varchar(10) NOT NULL,
  `uid` int(11) NOT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=1000006 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- 添加数据
-- ----------------------------
INSERT INTO `bankcard` VALUES ('1000001', '123456', '56000', '储蓄卡', '1');
INSERT INTO `bankcard` VALUES ('1000002', '654321', '8520', '储蓄卡', '2');
INSERT INTO `bankcard` VALUES ('1000003', '135790', '456452', '储蓄卡', '3');
INSERT INTO `bankcard` VALUES ('1000004', '147258', '1200', '信用卡', '2');
INSERT INTO `bankcard` VALUES ('1000005', '258369', '56123', '储蓄卡', '4');
-- ----------------------------
-- 用户信息表
-- ----------------------------
DROP TABLE IF EXISTS `bankuser`;
CREATE TABLE `bankuser` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `uname` varchar(20) NOT NULL,
  `uphone` int(11) NOT NULL,
  `company` varchar(30) DEFAULT NULL,
  `hometown` varchar(30) NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- 添加数据
-- ----------------------------
INSERT INTO `bankuser` VALUES ('1', 'xx', '1854012935', '毛氏集团', '四川南充新生村');
INSERT INTO `bankuser` VALUES ('2', 'xxx', '1852453012', '毛氏集团保安部', '辽宁锦州小柳村');
INSERT INTO `bankuser` VALUES ('3', 'xx', '1362493209', '甲骨文', '内蒙赤峰冯庄');
INSERT INTO `bankuser` VALUES ('4', 'xx', '1365689213', '字节跳动', '浙江台州下盘村');

2.创建项目并且分层

​ 项目结构

​ src

​ com.lxk.bean 实体类

​ com.lxk.controller 控制层:接收请求并且处理

​ com.lxk.service 业务层的接口

​ com.lxk.service.impl 业务层: 处理请求的业务以及事务的处理

​ com.lxk.dao 持久化层的接口

​ com.lxk.dao.impl 持久化层:操作数据库

调用的顺序:controller层接收请求,然后调用service层完成业务的处理,service层调用Dao来完成数据库操作。

3.编写代码

​ 项目结构


给项目导入jar包
Mysql8.0及以上的JDBC jar包下载
1.在项目文件新建名为lib的文件夹


2.把下载好的jar包复制并粘贴在lib文件夹中


3.右键jar包配置路径

主要代码:

实体类

BankCard.java

package com.yy.bean;

/**   
 * @ClassName:  BankCard   
 * @Description:银行卡实体类 
 * @author: 
 * @date:   2021年5月10日 下午1:17:09      
 * @Copyright:  
 */
public class BankCard {
   
	private long cid;
	private int cpssword;
	private double cmoney;
	private String ctype;
	private BankUser bankuser;
	
	@Override
	public String toString() {
   
		return this.cid+"\t\t"+this.cpssword+"\t\t"+this.cmoney+"\t\t"+this.ctype+"\t\t"
				 +this.bankuser.getUid()+"\t\t"+this.bankuser.getUname()+"\t\t"+this.bankuser.getUphone()+"\t\t"+this.bankuser.getCompany()+"\t\t"+this.bankuser.getHometown();
	}
	public BankUser getBankuser() {
   
		return bankuser;
	}
	public void setBankuser(BankUser bankuser) {
   
		this.bankuser = bankuser;
	}
	public long getCid() {
   
		return cid;
	}
	public void setCid(long cid) {
   
		this.cid = cid;
	}
	public int getCpssword() {
   
		return cpssword;
	}
	public void setCpssword(int cpssword) {
   
		this.cpssword = cpssword;
	}
	public double getCmoney() {
   
		return cmoney;
	}
	public void setCmoney(double cmoney) {
   
		this.cmoney = cmoney;
	}
	public String getCtype() {
   
		return ctype;
	}
	public void setCtype(String ctype) {
   
		this.ctype = ctype;
	}
	
	
	
	
}

BankManager.java

package com.yy.bean;

/**   
 * @ClassName:  BankManager   
 * @Description:银行账户管理员实体类 
 * @author: 
 * @date:   2021年5月10日 下午12:11:41      
 * @Copyright:  
 */
public class BankManager {
   
	private int mid;
	private String mname;
	private String mpass;
	
	public int getMid() {
   
		return mid;
	}
	public void setMid(int mid) {
   
		this.mid = mid;
	}
	public String getMname() {
   
		return mname;
	}
	public void setMname(String mname) {
   
		this.mname = mname;
	}
	public String getMpass() {
   
		return mpass;
	}
	public void setMpass(String mpass) {
   
		this.mpass = mpass;
	}
	
	
}

BankUser.java

package com.yy.bean;

/**   
 * @ClassName:  BankUser   
 * @Description:用户实体类
 * @author: 
 * @date:   2021年5月10日 下午4:18:14      
 * @Copyright:  
 */
public class BankUser {
   
	private int uid;
	private String uname;
	private int uphone;
	private String company;
	private String hometown;
	public String getHometown() {
   
		return hometown;
	}
	public void setHometown(String hometown) {
   
		this.hometown = hometown;
	}
	public int getUid() {
   
		return uid;
	}
	public void setUid(int uid) {
   
		this.uid = uid;
	}
	public String getUname() {
   
		return uname;
	}
	public void setUname(String uname) {
   
		this.uname = uname;
	}
	public int getUphone() {
   
		return uphone;
	}
	public void setUphone(int uphone) {
   
		this.uphone = uphone;
	}
	public String getCompany() {
   
		return company;
	}
	public void setCompany(String company) {
   
		this.company = company;
	}
	@Override
	public String toString() {
   
		return  uid+"\t\t"+uname+"\t\t"+uphone+"\t\t"+company+"\t\t"+hometown;
	}
	
	
}

控制层

BankController.java

package com.yy.controller;

import java.util.List;

import com.yy.bean.BankCard;
import com.yy.bean.BankManager;
import com.yy.bean.BankUser;
import com.yy.service.BankService;
import com.yy.service.impl.BankServiceImpl;

/**   
 * @ClassName:  BankController   
 * @Description:控制层
 * @author:
 * @date:   2021年5月10日 下午12:17:37      
 * @Copyright:  
 */
public class BankController {
   
	BankService bankservice=new BankServiceImpl();
	//管理员登录功能
	public String ManagerLogin(BankManager bm) {
   
		return bankservice.ManagerLogin(bm);//得到管理员账户名
	}
	
	public boolean CardLogin(BankCard bc) {
   
		return bankservice.CardLogin(bc);//获得是否存在的标志
	}
	
	public List<BankCard> showAllBankCardInfo(){
   //查看所有的银行卡信息
		return bankservice.showAllBankCardInfo();
	}
	public List<BankCard> showBankCardInfobyphone(int userphone){
   //通过手机号查询银行卡信息
		return bankservice.showBankCardInfobyphone(userphone);
	}
	public List<BankUser> showAllBankUserInfo(){
   //查看所有用户信息
		return bankservice.showAllBankUserInfo();
		
	}
 	
	
	public boolean Finduserbyphone(BankUser bu) {
   //在添加用户信息之前先查询表中是否有该用户
		return bankservice.Finduserbyphone(bu);
	}
	
	public boolean adduserInfo(BankUser bu) {
   //添加用户信息
		return bankservice.adduserInfo(bu);
	}
	
	public boolean addcardInfo(BankCard bc){
   //添加银行卡信息
		return bankservice.addcardInfo(bc);
		
	}
	
	public boolean FindBankcardbycid(Long cid) {
   //查询是否有这个卡号的银行卡
		return bankservice.FindBankcardbycid(cid);
	}
	public boolean updatecardpwd(Long cid,int npwd) {
   //修改银行卡密码
		return bankservice.updatecardpwd(cid, npwd);
	}
	public boolean deletecardbycid(Long cid) {
   
		return bankservice.deletecardbycid(cid);
		
	}
}

业务层接口

BankService.java

package com.yy.service;

import java.util.List;

import com.yy.bean.BankCard;
import com.yy.bean.BankManager;
import com.yy.bean.BankUser;

/**   
 * @ClassName:  BankService   
 * @Description:业务层的接口 
 * @author: 
 * @date:   2021年5月10日 下午3:58:49      
 * @Copyright:  
 */
public interface BankService {
   
	public String ManagerLogin(BankManager bm);
	public boolean CardLogin(BankCard bc);
	public List<BankCard> showAllBankCardInfo();
	public List<BankCard> showBankCardInfobyphone(int userphone);
	public boolean Finduserbyphone(BankUser bu);
	public boolean adduserInfo(BankUser bu);
	public List<BankUser> showAllBankUserInfo();
	public boolean addcardInfo(BankCard bc);
	public boolean FindBankcardbycid(Long cid);
	public boolean updatecardpwd(Long cid,int npwd);
	public boolean deletecardbycid(Long cid);
}

业务层

BankServiceImpl.java

package com.yy.service.impl;

import java.util.List;

import javax.print.attribute.standard.MediaSize.NA;

import com.yy.bean.BankCard;
import com.yy.bean.BankManager;
import com.yy.bean.BankUser;
import com.yy.dao.BankDao;
import com.yy.dao.impl.BankDaoImpl;
import com.yy.service.BankService;

/**   
 * @ClassName:  BankServiceImpl   
 * @Description:处理请求的业务以及事务的处理   
 * @author: 
 * @date:   2021年5月10日 下午3:59:18      
 * @Copyright:  
 */
public class BankServiceImpl implements BankService{
   
	BankDao bankdao=new BankDaoImpl();
	@Override
	public String ManagerLogin(BankManager bm) {
   
		return bankdao.ManagerLogin(bm);
	}
	//如果查询返回值是1则说明查询到此人,返回是0则说明未查询到此人
	@Override
	public boolean CardLogin(BankCard bc) {
   
		int flag=bankdao.CardLogin(bc);
		if(flag>0)
			return true;
		return false;
	}
	@Override
	public List<BankCard> showAllBankCardInfo() {
   
		
		return bankdao.showAllBankCardInfo();
	}
	@Override
	public List<BankCard> showBankCardInfobyphone(int userphone) {
   
		
		return bankdao.showBankCardInfobyphone(userphone);
	}
	@Override
	public boolean Finduserbyphone(BankUser bu) {
   
		if(bankdao.Finduserbyphone(bu)>0)
			return true;
		return false;
		
	}
	@Override
	public boolean adduserInfo(BankUser bu) {
   
		if(bankdao.adduserInfo(bu)>0) 
			return true;
		return false;
	}
	@Override
	public List<BankUser> showAllBankUserInfo() {
   
		return bankdao.showAllBankUserInfo();
	}
	@Override
	public boolean addcardInfo(BankCard bc) {
   
		if(bankdao.addcardInfo(bc)>0) 
			return true;
		return false;
	}
	@Override
	public boolean FindBankcardbycid(Long cid) {
   
		if(bankdao.FindBankcardbycid(cid)>0)
			return true;
		return false;
	}
	@Override
	public boolean updatecardpwd(Long cid,int npwd) {
   
		if(bankdao.updatecardpwd(cid, npwd)>0) 
			return true;
		return false;
	}
	@Override
	public boolean deletecardbycid(Long cid) {
   
		if(bankdao.deletecardbycid(cid)>0) 
			return true;
		return false;
	}
	
}

持久化层接口

BankDao.java

package com.yy.dao;

import java.util.List;

import com.yy.bean.BankCard;
import com.yy.bean.BankManager;
import com.yy.bean.BankUser;

/**   
 * @ClassName:  BankDao   
 * @Description:持久化层接口
 * @author: 
 * @date:   2021年5月10日 下午3:57:03      
 * @Copyright:  
 */
public interface BankDao {
   
	public String ManagerLogin(BankManager bm);
	public int CardLogin(BankCard bc);
	public List<BankCard> showAllBankCardInfo();
	public List<BankCard> showBankCardInfobyphone(int userphone);
	public int Finduserbyphone(BankUser bu);
	public int adduserInfo(BankUser bu);
	public List<BankUser> showAllBankUserInfo();
	public int addcardInfo(BankCard bc);
	public int FindBankcardbycid(Long cid);
	public int updatecardpwd(Long cid,int npwd);
	public int deletecardbycid(Long cid);
}

持久化层

BankDaoImpl.java

package com.yy.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;

import com.yy.bean.BankCard;
import com.yy.bean.BankManager;
import com.yy.bean.BankUser;
import com.yy.dao.BankDao;

/**   
 * @ClassName:  BankDaoImpl   
 * @Description:数据库操作   
 * @author: 
 * @date:   2021年5月10日 下午3:58:04      
 * @Copyright:  
 */
public class BankDaoImpl implements BankDao{
   
	//查询管理员的账户名
	@Override
	public String ManagerLogin(BankManager bm) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs=null;
		String mname="";
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select mname from bankmanager where mname=? and mpass=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, bm.getMname());
			ps.setString(2, bm.getMpass());
			rs=ps.executeQuery();
			while(rs.next()) {
   
				mname=rs.getString("mname");
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return mname;
	}
	//查询是否有这个银行账户,用于登录
	@Override
	public int CardLogin(BankCard bc) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs=null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select count(*) from bankcard where cid=? and cpassword=?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, bc.getCid());
			ps.setInt(2,bc.getCpssword());
			rs=ps.executeQuery();
			while(rs.next()) {
   
				flag=rs.getInt(1);
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return flag;
	}
	@Override
	//查询银行卡所有信息
	public List<BankCard> showAllBankCardInfo() {
   
		Connection conn=null;
		Statement stat=null;
		ResultSet rs=null;
		List<BankCard> list=new ArrayList<BankCard>();
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select bc.*,bu.uname,bu.uphone,bu.company,bu.hometown from bankcard bc,bankuser bu where bc.uid=bu.uid";
			stat= conn.createStatement();
			rs=stat.executeQuery(sql);
			while(rs.next()) {
   
				BankCard bc=new BankCard();
				BankUser bu=new BankUser();
				bc.setBankuser(bu);
				bc.setCid(rs.getLong("cid"));
				bc.setCpssword(rs.getInt("cpassword"));
				bc.setCmoney(rs.getDouble("cmoney"));
				bc.setCtype(rs.getString("ctype"));
				bc.getBankuser().setUid(rs.getInt("uid"));
				bc.getBankuser().setUname(rs.getString("uname"));
				bc.getBankuser().setUphone(rs.getInt("uphone"));
				bc.getBankuser().setCompany(rs.getString("company"));
				bc.getBankuser().setHometown(rs.getString("hometown"));
				list.add(bc);
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(stat!=null) {
   
				try {
   
					stat.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	@Override
	//通过手机号查询银行卡信息
	public List<BankCard> showBankCardInfobyphone(int userphone) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs=null;
		List<BankCard> list=new ArrayList<BankCard>();
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select bc.*,bu.uname,bu.uphone,bu.company,bu.hometown from bankcard bc,bankuser bu where bc.uid=bu.uid and bu.uphone=?";
			ps= conn.prepareStatement(sql);
			ps.setInt(1, userphone);
			rs=ps.executeQuery();
			while(rs.next()) {
   
				BankCard bc=new BankCard();
				BankUser bu=new BankUser();
				bc.setBankuser(bu);
				bc.setCid(rs.getLong("cid"));
				bc.setCpssword(rs.getInt("cpassword"));
				bc.setCmoney(rs.getDouble("cmoney"));
				bc.setCtype(rs.getString("ctype"));
				bc.getBankuser().setUid(rs.getInt("uid"));
				bc.getBankuser().setUname(rs.getString("uname"));
				bc.getBankuser().setUphone(rs.getInt("uphone"));
				bc.getBankuser().setCompany(rs.getString("company"));
				bc.getBankuser().setHometown(rs.getString("hometown"));
				list.add(bc);
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	@Override
	//通过手机查询是否有这个用户
	public int Finduserbyphone(BankUser bu) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs=null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select count(*) from bankuser where uphone=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1,bu.getUphone());
			rs=ps.executeQuery();
			while(rs.next()) {
   
				flag=rs.getInt(1);
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return flag;
	}
	@Override
	//向用户表添加信息
	public int adduserInfo(BankUser bu) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="insert into bankuser(uname,uphone,company,hometown) values (?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, bu.getUname());
			ps.setInt(2, bu.getUphone());
			ps.setString(3, bu.getCompany());
			ps.setString(4, bu.getHometown());
			flag=ps.executeUpdate();
			
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		return flag;
	}
	@Override
	//查询所有用户信息
	public List<BankUser> showAllBankUserInfo() {
   
		Connection conn=null;
		Statement stat=null;
		ResultSet rs=null;
		List<BankUser> list=new ArrayList<BankUser>();
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select * from bankuser";
			stat= conn.createStatement();
			rs=stat.executeQuery(sql);
			while(rs.next()) {
   
				BankUser bu=new BankUser();
				bu.setUid(rs.getInt(1));
				bu.setUname(rs.getString(2));
				bu.setUphone(rs.getInt(3));
				bu.setCompany(rs.getString(4));
				bu.setHometown(rs.getString(5));
				list.add(bu);
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(stat!=null) {
   
				try {
   
					stat.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	@Override
	public int addcardInfo(BankCard bc) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="insert into bankcard(cpassword,cmoney,ctype,uid) values (?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, bc.getCpssword());
			ps.setDouble(2, bc.getCmoney());
			ps.setString(3, bc.getCtype());
			ps.setInt(4, bc.getBankuser().getUid());
			flag=ps.executeUpdate();
			
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		return flag;
	}
	@Override
	//通过银行卡号查询
	public int FindBankcardbycid(Long cid) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs=null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="select count(*) from bankcard where cid=?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1,cid);
			rs=ps.executeQuery();
			while(rs.next()) {
   
				flag=rs.getInt(1);
			}
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(rs!=null) {
   
				try {
   
					rs.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return flag;
	}
	//通过卡号修改密码
	@Override
	public int updatecardpwd(Long cid,int npwd) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="update bankcard set cpassword=? where cid=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, npwd);
			ps.setLong(2, cid);
			flag=ps.executeUpdate();
			
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		return flag;
	}
	@Override
	public int deletecardbycid(Long cid) {
   
		Connection conn=null;
		PreparedStatement ps =null;
		int flag=0;
		try {
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
					+ "class04?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "root");
			String sql="delete from bankcard  where cid=?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, cid);
			flag=ps.executeUpdate();
			
		} catch (SQLException e) {
   
			e.printStackTrace();
		}finally {
   
			if(conn!=null) {
   
				try {
   
					conn.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
   
				try {
   
					ps.close();
				} catch (SQLException e) {
   
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		return flag;
	}
	
}

主函数(视图层)

BankMain.java

package com.yy.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.yy.bean.BankCard;
import com.yy.bean.BankManager;
import com.yy.bean.BankUser;
import com.yy.controller.BankController;

//两个界面
//1.管理员界面  
//   1.1 新增用户信息
//   1.2 新增银行卡信息
//   1.3 修改银行卡信息
//   1.4 删除银行卡
//   1.5 查看所有银行卡信息
//   1.6 根据条件查看银行卡信息
//2.用户界面
//   2.1 取钱功能
//   2.2 存钱功能
//   2.3 转账功能
//   2.4 查看自己银行卡信息
//   2.5 修改密码功能
//3.登录功能
//   3.1 管理员登录
//   3.2 用户登录
/**   
 * @ClassName:  BankMain   
 * @Description:相当于视图层   
 * @author: 
 * @date:   2021年5月10日 下午4:00:38      
 * @Copyright:  
 */
public class BankMain {
   

	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		BankController bankcontroller = new BankController();
		//一级界面完成管理员和普通用户功能的选择调用		
		System.out.println("*************欢迎使用太平洋银行管理系统***********");
		System.out.println("1.管理员登录");
		System.out.println("2.普通用户登录");
		System.out.println("********************************************");
		System.out.println("请输入您的选择:");
		int key1=sc.nextInt();
		switch (key1) {
   
		case 1://管理员登录
			System.out.println("请输入管理员账户名:");
			String ManagerName=sc.next();
			System.out.println("请输入管理员密码:");
			String ManagerPwd=sc.next();
			BankManager bm = new BankManager();
			bm.setMname(ManagerName);
			bm.setMpass(ManagerPwd);
			String mname=bankcontroller.ManagerLogin(bm);
			if(mname!=null) {
   
				System.out.println("欢迎管理员:"+mname+"使用本系统!");
			}
			boolean Flag1=true;
			while(Flag1) {
   
				System.out.println("===================");
				System.out.println("1.新增用户信息");
				System.out.println("2.新增银行卡信息");
				System.out.println("3.修改银行卡密码");
				System.out.println("4.删除银行卡");
				System.out.println("5.查看所有银行卡信息");
				System.out.println("6.根据手机号查看银行卡信息");
				System.out.println("7.查看所有用户信息");
				System.out.println("===================");
				System.out.println("请输入您的选择:");
				int key2=sc.nextInt();
				switch (key2) {
   
				case 1://新增用户信息
					BankUser bu = new BankUser();
					System.out.println("请输入用户名:");
					bu.setUname(sc.next());
					System.out.println("请输入手机号:");
					bu.setUphone(sc.nextInt());
					System.out.println("请输入公司名称:");
					bu.setCompany(sc.next());
					System.out.println("请输入籍贯:");
					bu.setHometown(sc.next());
					boolean f=bankcontroller.Finduserbyphone(bu);
					if(!f) {
   
						if(bankcontroller.adduserInfo(bu)) {
   
							System.out.println("添加成功!");
						}
					}else {
   
						System.out.println("该手机号已存在!无法进行添加。");
					}
					break;
				case 2://新增银行卡信息
					BankCard bc=new BankCard();
					BankUser bu2=new BankUser();
					bc.setBankuser(bu2);
					List<BankCard> list=new ArrayList<BankCard>();
					System.out.println("请输入新建银行卡的密码:");
					bc.setCpssword(sc.nextInt());
					System.out.println("请输入新建银行卡的预存金额:");
					bc.setCmoney(sc.nextDouble());
					boolean f2=true;
					while(f2) {
   //如果不按照正确的提示输入则一直循环,提醒重新输入为预期值为止
						System.out.println("请输入新建银行卡的类型(储蓄卡/信用卡)");
						String ctype=sc.next();
						if(ctype.equals("储蓄卡")||ctype.equals("信用卡")) {
   
							bc.setCtype(ctype);
							f2=false;
						}else{
   
							System.out.println("请按提示重新输入!");
						}
					}
					List<BankUser> list4=bankcontroller.showAllBankUserInfo();
					for (BankUser bu4 : list4) {
   
						System.out.println(bu4.getUid()+"——"+bu4.getUname()+"——"+bu4.getUphone());
					}
					System.out.println("请输入新建银行卡的新ID:");
					bc.getBankuser().setUid(sc.nextInt());
					if(bankcontroller.addcardInfo(bc)) {
   
						System.out.println("添加成功!");
					}
					break;
				case 3://修改银行卡信息
					System.out.println("请输入要修改密码的银行卡卡号:");
					Long cid=sc.nextLong();
					boolean f3=bankcontroller.FindBankcardbycid(cid);
					if(f3) {
   
						System.out.println("请输入要修改的密码:");
						int pwd=sc.nextInt();
						boolean f4=true;
						while(f4) {
   
							System.out.println("请再次输入新密码:");
							int newpwd=sc.nextInt();
							if(pwd==newpwd) {
   
								if(bankcontroller.updatecardpwd(cid, newpwd)) {
   
									System.out.println("修改成功!");
									f4=false;
								}
							}else {
   
								System.out.println("两次密码输入不一致!");
							}
						}
					}else {
   
						System.out.println("该银行卡不存在!无法进行密码修改。");
					}
					break;
				case 4://删除银行卡
					System.out.println("请输入要修改密码的银行卡卡号:");
					Long ncid=sc.nextLong();
					boolean f4=bankcontroller.FindBankcardbycid(ncid);
					if(f4) {
   
						if(bankcontroller.deletecardbycid(ncid)) {
   
							System.out.println("删除成功!");
						}
					}else {
   
						System.out.println("该银行卡不存在!无需操作");
					}
					break;
				case 5://查看所有银行卡信息
					List<BankCard> list1=bankcontroller.showAllBankCardInfo();
					System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
					System.out.println("银行卡号"+"\t\t"+"密码"+"\t\t"+"余额"+"\t\t"+"银行卡类型"+"\t\t"
							 +"用户号"+"\t\t"+"用户名"+"\t\t"+"电话"+"\t\t\t"+"公司"+"\t\t"+"籍贯");
					for (BankCard bc2 : list1) {
   
						System.out.println(bc2);
					}
					System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
					break;
				case 6://根据条件(手机号)查看银行卡信息
					System.out.println("请输入所绑定的手机号:");
					int userphone=sc.nextInt();
					List<BankCard> list2=bankcontroller.showBankCardInfobyphone(userphone);
					if(list2.isEmpty()) {
   
						System.out.println("对不起,您输入的手机号未绑定过任何银行卡!");
					}else {
   
						System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
						System.out.println("银行卡号"+"\t\t"+"密码"+"\t\t"+"余额"+"\t\t"+"银行卡类型"+"\t\t"
								 +"用户号"+"\t\t"+"用户名"+"\t\t"+"电话"+"\t\t\t"+"公司"+"\t\t"+"籍贯");
						for (BankCard bc3 : list2) {
   
							System.out.println(bc3);
						}
						System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
					}
					
					break;
				case 7://查看所有用户信息
					List<BankUser> list3=bankcontroller.showAllBankUserInfo();
					System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
					System.out.println("用户号"+"\t\t"+"姓名"+"\t\t"+"电话"+"\t\t\t"+"公司名"+"\t\t"+"籍贯");
					for (BankUser bu1 : list3) {
   
						System.out.println(bu1);
					}
					System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
					break;
				default:
					System.out.println("您的输入有误!");
					break;
				}
				System.out.println("是否继续? Y/N");
				if(sc.next().equals("N")) {
   
					Flag1=false;
					System.out.println("欢迎下次使用!");
				}
			}
			
			break;
		case 2://普通用户登录
			System.out.println("请输入您的银行卡号:");
			Long cardid=sc.nextLong();
			System.out.println("请输入您的密码:");
			int cardpwd=sc.nextInt();
			BankCard bc=new BankCard();
			bc.setCid(cardid);
			bc.setCpssword(cardpwd);
			boolean flag=bankcontroller.CardLogin(bc);
			if(flag) {
   
				System.out.println("登陆成功!");
			}else {
   
				System.out.println("登陆失败!");
			}
			boolean Flag2=true;
			while(Flag2) {
   
				System.out.println("===================");
				System.out.println("1.取钱功能");
				System.out.println("2.存钱功能");
				System.out.println("3.转账功能");
				System.out.println("4.查看自己银行卡信息");
				System.out.println("5.修改密码功能");
				System.out.println("===================");
				System.out.println("请输入您的选择:");
				int key3=sc.nextInt();
				switch (key3) {
   
				case 1:
					
					break;
				case 2:
					
					break;
				case 3:
					
					break;
				case 4:
					
					break;
				case 5:
	
					break;
				default:
					System.out.println("您的输入有误!");
					break;
				}
				System.out.println("是否继续? Y/N");
				if(sc.next().equals("N")) {
   
					Flag1=false;
					System.out.println("欢迎下次使用!");
				}
			}
			break;
		default:
			System.out.println("您的输入有误!程序退出");
			System.exit(0);
			break;
		}

	}

}


转载:https://blog.csdn.net/weixin_44524687/article/details/116611087
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场