JDBC研究ノート(2) – 単純なプロジェクトを通してトランザクションとMVCモデルを理解する

1 Star2 Stars3 Stars4 Stars5 Stars (まだ評価されていません)
Loading...

まず、トランザクション

1.1取引の4つの特性(ACID)

1.アトミック性:トランザクション内のすべての操作がすべて成功したか、すべて失敗したかのいずれかです。
2.整合性:トランザクションが実行された後、データベースのステータスは他のビジネスルールと一致します。
3.分離:分離とは、並行操作では、それぞれのトランザクションが互いに干渉しないように、異なるトランザクションを分離する必要があることを意味します。
4.永続性:トランザクションが正常にコミットされると、トランザクション内のすべてのデータ操作をデータベースに永続化する必要があります。 トランザクションがコミットされた直後にデータベースがクラッシュした場合でも、データベースを再起動した後は、何らかのメカニズムを使用してデータを復元する必要があります。

1.2 JDBC操作トランザクション

JDBCでのトランザクションの処理はConnectionオブジェクトを通じて行われ、同じトランザクション内のすべての操作で同じConnectionオブジェクトが使用されます。

setAutoCommit(boolean);トランザクションを自動的にコミットするかどうかを設定します。trueの場合は自動送信(デフォルト値はtrue)、つまり実行されたSQL文はそれぞれ別のトランザクション、falseの場合はトランザクションを開くそうです。 con.setAutoCommit(false);ステートメントは、トランザクションが開いていることを示します。

Con.commit();トランザクションを送信して終了します。

Con.rollback();トランザクションをロールバックします。

第二に、MVCモデルを簡単に紹介します
MVCは、モデル、ビュー、およびコントローラの略語です。 つまり、MVCは、モデルレイヤー、ビューレイヤー、コントロールレイヤーに分かれています。

1.モデル層:2つの部分に分かれています:1.データベース内の特定のテーブルのマッピングクラス。 2.データベース・マッピング・クラス操作(追加、削除、変更、検査)メソッドのラッパー・クラス。
2.ビジネスレイヤー:通常はビジネスレイヤーがあります。ビジネスレイヤーは、モデルレイヤーの基本操作メソッド(追加、削除、変更、チェック)を呼び出して、より複雑な操作(ビジネス)を実装します。
3.制御レイヤ:ビジネスレイヤメソッドへの呼び出しは、ビューレイヤとコントロールレイヤの間のブリッジとしての役割を果たします。
4.ビュー層:ユーザーのデータを受け入れるためにユーザーと直接対話し、ユーザーはデータを交換します。

第三に、JDBCとトランザクション(接続プールなし)を理解するための単純なMVCモデルプロジェクトを通じて、

項目説明機能:トランザクションを介してユーザ間の伝達関数を実現する。
特定の操作プロセス:譲渡元、受取人のユーザ名、および特定の譲渡額を別々に入力し、ユーザ間の転送を完了します。

3.1データベース内のフォームとアカウント情報の準備

Account_infoフォーム:

Trans_info形式:

account_infoに挿入された2つのアカウントレコードが転送に使用されます

3.2準備されたクラス

1. DBUtilクラス/ /データベースへの接続を取得します。
2.Account_infoクラス/ /データベース内のユーザ情報のマッピングクラス。
3.Trans_infoクラス/ /データベース内のユーザトランザクションレコードのマッピングクラス。
4.AccountDaoクラス// Account_infoの基本操作メソッド(追加、削除、変更)のカプセル化クラス。
5.TransDaoクラス// Trans_infoの基本的な操作方法(追加、削除、変更)のラッパークラス。
6.TransServiceクラスは/ /伝達関数を実装します。
7.Trans_actionクラス// TransServiceクラスを呼び出して制御します。
8.Viewクラスはユーザーデータを受け入れるために使用されます。

注:2345はモデルレイヤに属し、6はビジネスレイヤに属し、7はコントロールレイヤに属し、8はビューレイヤに属します。

3.3実装コード

DBUtilクラス:

package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

 package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

 package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

 package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

 package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

 package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

 package transaction; import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建一个Account对象用于对查找Account对象 Account_info from=a_dao.Account_query(1);
Account_info to=a_dao.Account_query(2);

//调用业务层的transcation(from,to,money)方法实现业务 TransService ts=new TransService();
String re=ts.transaction(from, to, money); return re;
} }
--> View类: package test; import java.sql.SQLException;
import java.util.Scanner; import action.Trans_action; public class view { public static void main(String[] args) throws SQLException { Scanner scan=new Scanner(System.in);
System.out.println("请输入转账人的id:");
int f_id=scan.nextInt();
System.out.println("请输入收款方的id:");
int t_id=scan.nextInt();
System.out.println("亲输入转账金额:");
double money= scan.nextDouble(); Trans_action action=new Trans_action();
String rs=action.trans_action(f_id, t_id, money);
System.out.println("您的转账结果:"+rs);
} }
--> 3.4 效果展示 eclipse 控制台中输入数据: MySQL中用户账号金额的变化: MySQL中的转账信息(看最新一条): 标签 JDBC发表评论取消回复 电子邮件地址不会被公开。 必填项已用*标注 文章导航上一篇文章 上一篇 JDBC学习笔记(三)——数据库连接池下一篇文章 下一篇 忘记密码之手把手教你重置MySQL登陆密码(mac版本) tag 自豪地采用WordPress

package transaction; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
static Connection con=null;
static {
String url="jdbc:mysql://localhost:3306/demo1?useSSL=false&useUnicode=ture&characterEncoding=utf8";//连接数据库的url
String user="root";//用户名
String password="123";//密码
try {
con=DriverManager.getConnection(url,user,password);//获取与数据库连接的对象
}catch(SQLException e) {
e.printStackTrace();
}
} //获取连接对象的方法
public static Connection getConnection() {
return con;
}
}
--> Account_info类: package transaction; import java.util.Date; public class Account_info {
//数据库account_info表中字段的定义 private Integer id;
private String name;
private double balance;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
--> Trans_info类: package transaction; public class Trans_info {
//对数据库中trans_info表字段的定义 private Integer t_id;
private Integer source_id;
private String source_name;
private Integer destination_id;
private String destination_name;
private double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public Integer getSource_id() {
return source_id;
}
public void setSource_id(Integer source_id) {
this.source_id = source_id;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
public Integer getDestination_id() {
return destination_id;
}
public void setDestination_id(Integer destination_id) {
this.destination_id = destination_id;
}
public String getDestination_name() {
return destination_name;
}
public void setDestination_name(String destination_name) {
this.destination_name = destination_name;
}
public String toString() {
String st;
st=t_id.toString()+" "+source_id.toString()+" "+source_name.toString()+" "+destination_id.toString()
+" "+destination_name.toString();
return st;
}
}
--> AccountDao类: --> package transaction; import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class AccountDao { //在数据库account_info表中添加账号的方法
public void Account_add(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();//获取连接对象
String sql="insert into account_info value(?,?,?,?;)";//编写预处理sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//创造PreparedStatement对象
//定义sql语句中不完整的部分
pstmt.setInt(1, account.getId());
pstmt.setNString(2, account.getName());
pstmt.setDate(3, new Date(account.getDate().getTime()));
pstmt.setDouble(4, account.getBalance());
int isexe=pstmt.executeUpdate();//执行更改操作 }
//在数据库account_info表中删除账号的方法
public void Account_delete(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from account_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, account.getId());
int isexe=pstmt.executeUpdate();
}
//更新指定id用户账号的账户金额的方法
public void Account_update(Account_info account) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update account_info set balance=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, (int) account.getBalance());
pstmt.setInt(2, account.getId());
int isexe=pstmt.executeUpdate();
}
//查询所有账号信息
public List<Account_info> Account_query() throws SQLException {
List<Account_info> list=new ArrayList<Account_info>();//创建用于接收Account_info对象的列表对象
Connection con=DBUtil.getConnection();
String sql="select * from account_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);//执行查询操作获得结果集对象
//遍历结果集,将结果集赋值给对应Account_info实例
while(rs.next()) {
Account_info ac=new Account_info();//创建用于接收指定结果集的Account_info对象
//获取结果集中的数据给Account_info对象赋值
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance"));
list.add(ac);
}
return list;
}
//根据id查询指定用户账号方法
public Account_info Account_query(int id) throws SQLException{
Account_info ac=new Account_info();//创建用于接收查询结果的Account_info对象 Connection con=DBUtil.getConnection();
String sql="select * from account_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next();//这里要注意,如果要对结果集进行操作就一定要实行这一步
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("a_name"));
ac.setDate(rs.getDate("a_date"));
ac.setBalance(rs.getInt("balance")); return ac;}}--> TransDao类: package transaction; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class TransDao { public void Trans_add(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="insert into trans_info(source_id,source_name,destination_id,destination_name,money) value(?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
int isexe=pstmt.executeUpdate(); }
public void Trans_delete(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="delete from trans_info where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public void Trans_update(Trans_info trans) throws SQLException {
Connection con=DBUtil.getConnection();
String sql="update trans_info set source_id=?,source_name=?,destination_id=?,destination_name=?,money=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, trans.getSource_id());
pstmt.setString(2, trans.getSource_name());
pstmt.setInt(3, trans.getDestination_id());
pstmt.setString(4, trans.getDestination_name());
pstmt.setDouble(5, trans.getMoney());
pstmt.setInt(6, trans.getT_id());
int isexe=pstmt.executeUpdate(); }
public List<Trans_info> Trans_query() throws SQLException {
List<Trans_info> list=new ArrayList<Trans_info>();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
Trans_info trans=new Trans_info();
trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
list.add(trans);
}
return list;
}
public Trans_info Trans_query(int id) throws SQLException {
Trans_info trans=new Trans_info();
Connection con=DBUtil.getConnection();
String sql="select * from trans_info where id=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs=stmt.executeQuery();
rs.next(); trans.setT_id(rs.getInt("t_id"));
trans.setSource_id(rs.getInt("source_id"));
trans.setSource_name(rs.getString("source_na,e"));
trans.setDestination_id(rs.getInt("destination_id"));
trans.setDestination_name(rs.getString("destination_name"));
trans.setMoney(rs.getDouble("money")); return trans;
}
}
--> TransService类: package service; import java.sql.Connection;
import java.sql.SQLException; import transaction.AccountDao;
import transaction.Account_info;
import transaction.DBUtil;
import transaction.TransDao;
import transaction.Trans_info; public class TransService {
//业务层方法输入参数为:转账人(from)、收款方(to)、转账金额(money)
public String transaction(Account_info from ,Account_info to, double money) throws SQLException {
Connection con=DBUtil.getConnection();
con.setAutoCommit(false);//关闭每次操作自动提交,即开始事务操作
try {
AccountDao a_dao=new AccountDao();//创建AccountDao对象用于对指定账户对象进行基本操作
TransDao t_dao=new TransDao();//创建TransDao对象用于对指定转账信息对象进行基本操作 //对转账人账户金额进行修改
from.setBalance(from.getBalance()-money);
a_dao.Account_update(from); //对收款方账户金额进行修改
to.setBalance(to.getBalance()+money);
a_dao.Account_update(to); Trans_info trans=new Trans_info();//创建用于接收转账信息的Trans_info对象 //对trans对象进行赋值 trans.setSource_id(from.getId());
trans.setSource_name(from.getName());
trans.setDestination_id(to.getId());
trans.setDestination_name(to.getName());
trans.setMoney(money);
t_dao.Trans_add(trans);//在数据库中添加转账信息 con.commit();//提交事务
return "成功";//如果事务进行成功返回一个"成功"字符串
}catch(Exception e) {
con.rollback();//如果事务中存在异常,回滚这次事务执行的操作,即取消这次事务中的任何操作,并返回一个"失败"字符串
e.printStackTrace();
return "失败";
}
finally{
con.close();
} }
}
--> Trans_action类 : package action; import java.sql.SQLException; import service.TransService;
import transaction.AccountDao;
import transaction.Account_info; public class Trans_action {
//控制层方法输入参数为:转账人的id(f_id)、收款人的id(t_id)、转账金额(money)
public String trans_action(int f_id,int t_id ,double money) throws SQLException { AccountDao a_dao=new AccountDao();//创建