银行账户管理系统(oracle数据库连接池,数据库的链接,)

/*

* 银行账户管理系统:
* 属性:账户id,姓名,金额salary,利息类型;

*管理员模块实现的功能:

* 1.给用户开户
* 2.查询所有账户信息


* 用户模块实现的功能:

* 1.显示用户信息
* 2.存款
* 3.取款
* 4.查询本账户利息
*
* */

其中要导开源包三个:

oracle.jdbc.jar

commons-dbcp-1.4jar

commons-pool-1.5.5.jar

/*
* oracle数据库连接池:可实现数据库与eclipse跨平台连接
* 数据库连接池:里面放了好多连接数据库的链接,负责分配,释放,管理数据库连接,可以重复使用,而不新建
* ,使用完时候释放到数据库缓冲池中其他系统使用时,还可以在连接使用,而不需要再次新建一个连接池
* */

连接数据库连接池的文件:

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl1
username=oraclejava
password=lxn123


initialSize=10
maxActive=50
minIdle=5
maxWait=5000

-------------------------------------------------------------------------------------------------------------------------

实现对数据库数据进行增删改查操作的类;

public class JDBCOracle {
//连接到数据库连接池,实现数据的查询功能
public void select(String sql) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
// String sql="select * from bank";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
System.out.println("用户的id\t\t"+"姓名\t\t"+"金额");
System.out.println("----------------------------------------------");
while(resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
double salary=resultSet.getDouble(3);
System.out.println(id+"\t\t"+name+"\t\t"+salary);
}

} catch (Exception e) {
e.printStackTrace();
}
finally {
close(connection, preparedStatement, resultSet);
}
}
//查询判断输入的id号是否相同
public boolean selectT(String sql) throws Exception{
boolean b=false;
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
// String sql="select * from bank";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
int id=resultSet.getInt(1);
b=true;
}

} catch (Exception e) {
System.out.println("出错了!!!");
}
finally {
close(connection, preparedStatement, resultSet);
}
return b;
}
//查看用户的金额,时候超额
public double selectT1(String sql) throws Exception{
double salary=0;
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
// String sql="select * from bank";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
salary=resultSet.getDouble(1);

}

} catch (Exception e) {
System.out.println("出错了!!!");
}
finally {
close(connection, preparedStatement, resultSet);
}
return salary;
}
//查询一个用户的信息
public void selectT2(String sql) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
// String sql="select * from bank";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
System.out.println("用户的id\t\t"+"姓名\t\t"+"金额"+"\t\t"+"利率");
System.out.println("----------------------------------------------");
while(resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
double salary=resultSet.getDouble(3);
double commission_pct=resultSet.getDouble(4);
System.out.println(id+"\t\t"+name+"\t\t"+salary+"\t\t"+commission_pct);
}

} catch (Exception e) {
e.printStackTrace();
}
finally {
close(connection, preparedStatement, resultSet);
}
}

//连接到数据库连接池,实现增删改功能
public void update(String sql) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
//String sql="insert into bank values(001,'jiajia',1000,0.12)";
try {
connection=getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.executeUpdate();

} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, preparedStatement, null);
}
}

//关闭数据库资源的方法
public void close(Connection connection,
PreparedStatement preparedStatement,ResultSet resultSet) throws Exception{
if (resultSet!=null) {
resultSet.close();
}
if (preparedStatement!=null) {
preparedStatement.close();
}
if (connection!=null) {
connection.close();
}
}

/*
* oracle数据库连接池:可实现数据库与eclipse跨平台连接
* 数据库连接池:里面放了好多连接数据库的链接,负责分配,释放,管理数据库连接,可以重复使用,而不新建
* ,使用完时候释放到数据库缓冲池中其他系统使用时,还可以在连接使用,而不需要再次新建一个连接池
* */
public Connection getConnection() throws Exception{

Properties properties=new Properties();
InputStream in=
JDBCOracle.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in);

DataSource dataSource=
(DataSource) BasicDataSourceFactory.createDataSource(properties);
Connection connection=dataSource.getConnection();
// BasicDataSource basicDataSource=(BasicDataSource) dataSource;
// System.out.println(basicDataSource.getMaxWait());
return connection;
}
@Test
public void testGetConnection() throws Exception{
System.out.println(getConnection());
}
}

------------------------------------------------------------------------------------------------------

实现功能的主类:

/*
* 银行账户管理系统:
* 管理员模块:
* 属性:账户id,姓名,金额salary,利息类型;
* 1.给用户开户
* 2.查询所有账户信息
* 用户模块:
* 1.显示用户信息
* 2.存款
* 3.取款
* 4.查询本账户利息
*
* */
public class Bank {
static Scanner input=new Scanner(System.in);
static JDBCOracle jo=new JDBCOracle();


//1.管理员模块
public static void manager() throws Exception{
boolean b=false;
while(true){
System.out.println("管理员模块菜单!!!");
System.out.println("1.开户"+"\n"+"2.查询所有账户"+"\n"+"0.退出");
System.out.println("请输入菜单号:");
int n=input.nextInt();
switch(n){
case 1:setUser();break;
case 2:lookAllInfo();break;
case 0:System.out.println("已经退出了");b=true;break;
default :System.out.println("输入错误,请重新输入!!!");
}
if (b!=false) {
break;
}
}

}

//开户
public static void setUser() throws Exception{
System.out.println("请输入用户id:");
int id=input.nextInt();
System.out.println("请输入开户户名:");
String name=input.next();
System.out.println("请输入金额:");
double salary=input.nextDouble();
System.out.println("请选择利息类型(1.固定利率---2.浮动利率):");
int t=input.nextInt();
double commission_pct = 0;
switch(t){
case 1:commission_pct=0.03;break;
case 2:commission_pct=com(salary);break;
}
String sql="insert into bank values"
+ "("+id+",'"+name+"',"+salary+","+commission_pct+")";
jo.update(sql);
System.out.println("开户成功!!!");

}

//查询所有的用户信息
public static void lookAllInfo() throws Exception{
System.out.println("----------------------------------------------");
String sql="select id,name,salary from bank order by id asc";
jo.select(sql);
System.out.println("----------------------------------------------");
}

//判断得到浮动利率的方法
public static double com(double salary){
double commission_pct;
if(salary<=5000){
commission_pct=0.01;
}
else if(salary<=100000){
commission_pct=0.02;
}
else{
commission_pct=0.05;
}
return commission_pct;
}

//用户模块
public static void user() throws Exception{
boolean b=false;
System.out.println("用户模块菜单");
while(true){
System.out.println("--------------------------------------------");
System.out.println("1.存款---2.取款---3.查看本人账户信息---0.退出系统");
System.out.println("请首先输入一个账户的id:");
int id=input.nextInt();
String sql="select id from bank where id='"+id+"'";
String sql1="select id,name,salary from bank where id="+id+"";
if(jo.selectT(sql)){
jo.select(sql1);
System.out.println("----------------------------------------");
System.out.println("请输入菜单号:");
int n=input.nextInt();
switch(n){
case 1:inT(id);b=true;break;
case 2:outT(id);b=true;break;
case 3:lookInfo(id);b=true;break;
case 0:System.out.println("系统已经退出了!!!");System.exit(0);
default :System.out.println("输入信息错误,请重新输入!!!");
}
}
if(b!=false){
break;
}
else{
System.out.println("该账户不存在!!!");
}
}
}
//存款:
public static void inT(int id) throws Exception{
System.out.println("请输入要存款的数目:");
double salary=input.nextDouble();
String sql="update bank set salary=salary+"+salary+" where id="+id+"";
jo.update(sql);
System.out.println("存款成功!!!");
}
//取款
public static void outT(int id) throws Exception{
System.out.println("请输入要取款的数目:");
double salary=input.nextDouble();
String sql1="select salary from bank where id="+id+"";
String sql2="update bank set salary=salary-"+salary+" where id="+id+"";
if(salary>jo.selectT1(sql1)){
System.out.println("余额不足!!!");
}
else{
jo.update(sql2);
System.out.println("取款成功!!!");
}


}

//查看本人账户
public static void lookInfo(int id) throws Exception{
String sql="select * from bank where id="+id+"";
jo.selectT2(sql);
System.out.println("-----------------------------------------------------");

}
public static void main(String[] args) throws Exception {
while(true){
System.out.println("请选择 1.管理员---2.用户---0.退出");
int n=input.nextInt();
switch(n){
case 1:manager();break;
case 2:user();break;
case 0:System.out.println("系统已经退出!!!");System.exit(0);
default :System.out.println("输入错误,请重新输入!!!");
}
}
}
}

posted @ 2016-08-22 18:07  G-&-D  阅读(966)  评论(0编辑  收藏  举报