package org.jdbc.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
//操作数据库的工具类
public class DBUtil {
// 私有化构造方法
private DBUtil() {
}
private static String url = null;
private static String user = null;
private static String password = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
Properties pro = new Properties();
pro.load(new FileInputStream("src/jdbc.properties"));
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
}
}
// 读取外部属性文件获取连接对象,需手动输入url
public static Connection getConnection1(String url) {
Properties pro = new Properties();
Connection conn = null;
InputStream input = null;
try {
input = new FileInputStream("E:" + File.separator
+ "JDBC.properties");
pro.load(input);
conn = DriverManager.getConnection(url, pro);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 读取工程下的配置文件,获取连接对象参数,不需手动输入url
public static Connection getConnection2() throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
// 释放全部资源
public static void closeAll(Connection conn, Statement stat, ResultSet rs)
throws SQLException {
if (conn != null) {
conn.close();
}
if (stat != null) {
stat.close();
}
if (rs != null) {
rs.close();
}
}
// 释放所用资源
public static void closePart(Connection conn, Statement stat)
throws SQLException {
if (conn != null) {
conn.close();
}
if (stat != null) {
stat.close();
}
}
}
(4)编写代码:
package org.jdbc.transcation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import org.jdbc.util.DBUtil;
public class AccountDemo {
// 主方法控制运行方向
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner scan = new Scanner(System.in);
System.out.println("欢迎来到模拟银行界面");
while (true) {
System.out.print("请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):");
int chioce;
try {
chioce = scan.nextInt();
if (chioce == 1 | chioce == 2 | chioce == 3 | chioce==4 | chioce==5 |chioce == 8) {
switch (chioce) {
case 1: // 转账
System.out.print("请输入转出账户姓名:");
String fromName = br.readLine();
System.out.print("请输入转入账户姓名:");
String toName = br.readLine();
System.out.print("请输入转账金额:");
int money = scan.nextInt();
boolean flag = transFerMoney(fromName, toName, money);
if (flag) {
System.out.println("转账成功");
showBalance(toName);
showBalance(fromName);
} else {
System.out.println("请检查你输入的信息是否正确,请输入正确的信息,重新办理此业务,谢谢!");
}
break;
case 2: // 查询
System.out.print("请选择 (1:查询全部账户信息\t2:个人账户信息):");
int chioce1 = scan.nextInt();
if (chioce1 == 1 | chioce1 == 2) {
switch (chioce1) {
case 1: // 查询全部账户
selectAll();
break;
case 2: // 查询个人账户
System.out.print("请输入查询账户姓名:");
String balName = br.readLine();
selectPart(balName);
break;
}
} else {
System.out.println("你的选项错误,请重新输入");
}
break;
case 3: // 存款
System.out.print("请输入存款账户名:");
String savName = br.readLine();
System.out.print("请输入存款金额:");
int savMoney = scan.nextInt();
boolean isSave = saveMoney(savName, savMoney);
if (isSave) {
System.out.println("存款成功");
showBalance(savName);
} else {
System.out.println("你输入的账户信息有误,存款失败..");
}
break;
case 4: // 取款
System.out.print("请输入取款账户名:");
String draName = br.readLine();
System.out.print("请输入取款金额:");
int draMoney = scan.nextInt();
boolean isDraw = drawMoney(draName, draMoney);
if (isDraw) {
System.out.println("取款成功");
showBalance(draName);
} else {
System.out.println("你输入的账户信息有误,取款失败..");
}
break;
case 5: // 取款
System.out.print("请输入开户账户名:");
String opName = br.readLine();
System.out.print("请输入开户存款金额:");
int opBalance = scan.nextInt();
boolean isOpen = openAccount(opName, opBalance);
if (isOpen) {
System.out.println("恭喜你,开户成功");
System.out.println("当前账户信息:");
selectPart(opName);
} else {
System.out.println("Sorry,发生异常,你开户失败..");
}
break;
case 8: // 退出本系统
System.out.println("谢谢你的光临,再见!!");
System.exit(0);
}
} else {
System.out.println("你的选项错误,请重新输入");
}
} catch (IOException e) {
System.out.println("你有非法操作,产生异常现象:"+e.getMessage());
}
}
}
// 转钱方法
@SuppressWarnings("resource")
public static boolean transFerMoney(String fromName, String toName,
int money) {
boolean flag = false;
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs=null;
try {
conn = DBUtil.getConnection2();
String sql="select * from account where name=?";
ps=conn.prepareStatement(sql);
ps.setString(1, fromName);
rs=ps.executeQuery();
boolean flag1=false;
flag1= rs.next();
ps.close();
ps=conn.prepareStatement(sql);
ps.setString(1, fromName);
rs=ps.executeQuery();
boolean flag2=false;
flag2= rs.next();
ps.close();
if((flag1 && flag2)!=false){
conn.setAutoCommit(false);// 取消自动提交
String sql1 = "update account set balance=balance-? where name=?";
String sql2 = "update account set balance=balance+? where name=?";
ps = conn.prepareStatement(sql1);
ps.setInt(1, money);
ps.setString(2, fromName);// 转出
ps.executeUpdate();
ps.close();
ps = conn.prepareStatement(sql2);
ps.setInt(1, money);
ps.setString(2, toName);// 转入
ps.executeUpdate();
conn.commit();// 提交事务
flag = true;
}else{
System.out.println("Sorry,你输入的账户有误,不能进行转账处理");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closeAll(conn, ps,rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
// 全部账户查询方法信息
public static void selectAll() {
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection2();// 建立连接
String sql = "select * from account";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
System.out.println("全部账户信息查询结果为:");
System.out.println("编号\t姓名\t账户余额");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int balance = rs.getInt("balance");
System.out.println(id + "\t" + name + "\t" + balance);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closeAll(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 查询单人账户信息
@SuppressWarnings("resource")
public static void selectPart(String balName) {
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection2();// 建立连接
String sql3 = "select * from account where name=?";
ps = conn.prepareStatement(sql3);
ps.setString(1, balName);
rs = ps.executeQuery();
boolean flag = rs.next();
if (flag == true) {
System.out.println(balName + "账户信息查询结果为:");
System.out.println("编号\t姓名\t账户余额");
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int balance = rs.getInt("balance");
System.out.println(id + "\t" + name + "\t" + balance);
}
} else {
System.out.println("你输入的 账户信息不存在,请重新输入..");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closeAll(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 存钱方法
public static boolean saveMoney(String saveName, int savMoney) {
boolean flag = false;
Connection conn = null;
PreparedStatement ps = null;
String sql6 = "update account set balance=balance+? where name=?";
int count = 0;
try {
conn = DBUtil.getConnection2();
ps = conn.prepareStatement(sql6);
ps.setInt(1, savMoney);
ps.setString(2, saveName);
count = ps.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closePart(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
// 取钱方法
public static boolean drawMoney(String drawName, int draMoney) {
boolean flag = false;
Connection conn = null;
PreparedStatement ps = null;
String sql7 = "update account set balance=balance-? where name=?";
int count = 0;
try {
conn = DBUtil.getConnection2();
ps = conn.prepareStatement(sql7);
ps.setInt(1, draMoney);
ps.setString(2, drawName);
count = ps.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closePart(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
// 建立新的账户
public static boolean openAccount(String opName, int opBalance) {
boolean flag = false;
Connection conn = null;
PreparedStatement ps = null;
String sql9 = "insert into account(name,balance) values(?,?)";
int count = 0;
try {
conn = DBUtil.getConnection2();
ps = conn.prepareStatement(sql9);
ps.setString(1, opName);
ps.setInt(2, opBalance);
count = ps.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closePart(conn, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
//显示余额方法
@SuppressWarnings("resource")
public static void showBalance(String showName) {
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection2();// 建立连接
String sql10 = "select balance from account where name=?";
ps = conn.prepareStatement(sql10);
ps.setString(1, showName);
rs = ps.executeQuery();
rs = ps.executeQuery();
while (rs.next()) {
int shBalance = rs.getInt("balance");
System.out.println(showName + "账户当前 余额为:"+shBalance);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.closeAll(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
欢迎来到模拟银行界面
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):2
请选择 (1:查询全部账户信息 2:个人账户信息):1
全部账户信息查询结果为:
编号 姓名 账户余额
1 项羽 35013020
2 刘邦 20002980
3 李世民 47618000
4 赵匡胤 23990000
5 朱元璋 12356000
6 嬴政 32200000
7 曹操 200000000
8 孙权 14500000
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):1
请输入转出账户姓名:李世民
请输入转入账户姓名:曹操
请输入转账金额:3000
转账成功
曹操账户当前 余额为:200003000
李世民账户当前 余额为:47615000
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):1
请输入转出账户姓名:呵呵
请输入转入账户姓名:哈哈
请输入转账金额:20000
Sorry,你输入的账户有误,不能进行转账处理
请检查你输入的信息是否正确,请输入正确的信息,重新办理此业务,谢谢!
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):2
请选择 (1:查询全部账户信息 2:个人账户信息):2
请输入查询账户姓名:刘邦
刘邦账户信息查询结果为:
编号 姓名 账户余额
2 刘邦 20002980
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):2
请选择 (1:查询全部账户信息 2:个人账户信息):3
你的选项错误,请重新输入
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):2
请选择 (1:查询全部账户信息 2:个人账户信息):2
请输入查询账户姓名:哈哈
你输入的 账户信息不存在,请重新输入..
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):3
请输入存款账户名:朱元璋
请输入存款金额:300000
存款成功
朱元璋账户当前 余额为:12656000
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):3
请输入存款账户名:呵呵
请输入存款金额:3300
你输入的账户信息有误,存款失败..
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):4
请输入取款账户名:曹操
请输入取款金额:250
取款成功
曹操账户当前 余额为:200002750
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):
4
请输入取款账户名:哈哈
请输入取款金额:200
你输入的账户信息有误,取款失败..
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):5
请输入开户账户名:李渊
请输入开户存款金额:300000000
恭喜你,开户成功
当前账户信息:
李渊账户信息查询结果为:
编号 姓名 账户余额
9 李渊 300000000
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):2
请选择 (1:查询全部账户信息 2:个人账户信息):1
全部账户信息查询结果为:
编号 姓名 账户余额
1 项羽 35013020
2 刘邦 20002980
3 李世民 47615000
4 赵匡胤 23990000
5 朱元璋 12656000
6 嬴政 32200000
7 曹操 200002750
8 孙权 14500000
9 李渊 300000000
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):6
你的选项错误,请重新输入
请输入你的选择(1 转账 2 查询 3存款 4取款 5开户 8退出):8
谢谢你的光临,再见!!
Java总结(随笔)——代码总结JDBC以及事务,以银行转账,查账等为例
原文地址:http://blog.51cto.com/13501268/2128948