日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

对于多表查询和转账的事务提交

發布時間:2024/2/28 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对于多表查询和转账的事务提交 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

項目列表:

?

效果展示:

?

數據庫.sql

/*

Navicat MySQL Data Transfer

?

Source Server ????????: mysql

Source Server Version : 50549

Source Host ??????????: localhost:3306

Source Database ??????: ooxx

?

Target Server Type ???: MYSQL

Target Server Version : 50549

File Encoding ????????: 65001

?

Date: 2019-08-16 09:22:04

*/

?

SET FOREIGN_KEY_CHECKS=0;

?

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

-- Table structure for account

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

DROP TABLE IF EXISTS `account`;

CREATE TABLE `account` (

??`aid` int(11) NOT NULL,

??`abalance` double DEFAULT NULL,

??PRIMARY KEY (`aid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

?

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

-- Records of account

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

INSERT INTO `account` VALUES ('1', '8000');

INSERT INTO `account` VALUES ('2', '9000');

?

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

-- Table structure for user

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

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

??`uid` int(11) NOT NULL AUTO_INCREMENT,

??`uname` varchar(255) DEFAULT NULL,

??`upwd` varchar(255) DEFAULT NULL,

??PRIMARY KEY (`uid`),

??CONSTRAINT `user_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `account` (`aid`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

?

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

-- Records of user

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

INSERT INTO `user` VALUES ('1', '李斌', '123456');

INSERT INTO `user` VALUES ('2', '喬治大哥', '123456');

?

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

????<default-config>
????????<property name="driverClass">com.mysql.jdbc.Driver</property>
????????<property name="jdbcUrl">jdbc:mysql://localhost:3306/ooxx</property>
????????<property name="user">root</property>
????????<property name="password">123456</property>

????????<property name="acquireIncrement">5</property>
????????<property name="initialPoolSize">10</property>
????????<property name="minPoolSize">5</property>
????????<property name="maxPoolSize">20</property>
????</default-config>


</c3p0-config>

?

?

JdbcUtil

package com.henu.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

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

public class JdbcUtil {

????private static ComboPooledDataSource ds = null;


????//在靜態代碼塊中創建數據庫連接池
????static{
????????try{
????????????ds = new ComboPooledDataSource("c3p0-config");//使用C3P0的命名配置來創建數據源

????????}catch (Exception e) {
????????????throw new ExceptionInInitializerError(e);
????????}
????}

????public static ComboPooledDataSource getDataSourse(){
????????return ds;
????}

????public static Connection getConnection() throws SQLException{
????????//從數據源中獲取數據庫連接
????????return ds.getConnection();
????}

????public static void close(Connection conn,Statement st,ResultSet rs){
????????if(rs!=null){
????????????try{
????????????????rs.close();
????????????}catch (Exception e) {
????????????????e.printStackTrace();
????????????}
????????}
????????if(st!=null){
????????????try{
????????????????st.close();
????????????}catch (Exception e) {
????????????????e.printStackTrace();
????????????}
????????}

????????if(conn!=null){
????????????try{
????????????????conn.close();
????????????}catch (Exception e) {
????????????????e.printStackTrace();
????????????}
????????}
????}
}

?

?

Account

package com.henu.bean;

public class Account {
????private int aid;
????private double abalance;

????public Account() {
????}

????public Account(int aid, int abalance) {
????????this.aid = aid;
????????this.abalance = abalance;
????}


????public int getAid() {
????????return aid;
????}

????public void setAid(int aid) {
????????this.aid = aid;
????}

????public double getAbalance() {
????????return abalance;
????}

????public void setAbalance(double abalance) {
????????this.abalance = abalance;
????}

????@Override
????public String toString() {
????????return "Account{" +
????????????????"aid=" + aid +
????????????????", abalance=" + abalance +
????????????????'}';
????}
}

?

User

package com.henu.bean;

public class User {
????private int uid;
????private String uname;
????private String upwd;
????private Account account;//account放進user中以便多表查詢

????public User() {
????}

????public User(String uname, String upwd) {
????????this.uname = uname;
????????this.upwd = upwd;
????}

????public User(String uname, String upwd, Account account) {
????????this.uname = uname;
????????this.upwd = upwd;
????????this.account = account;
????}

????public User(int uid, String uname, String upwd, Account account) {
????????this.uid = uid;
????????this.uname = uname;
????????this.upwd = upwd;
????????this.account = account;
????}

????public int getUid() {
????????return uid;
????}

????public void setUid(int uid) {
????????this.uid = uid;
????}

????public String getUname() {
????????return uname;
????}

????public void setUname(String uname) {
????????this.uname = uname;
????}

????public String getUpwd() {
????????return upwd;
????}

????public void setUpwd(String upwd) {
????????this.upwd = upwd;
????}

????public Account getAccount() {
????????return account;
????}

????public void setAccount(Account account) {
????????this.account = account;
????}

????@Override
????public String toString() {
????????return "User{" +
????????????????"uid=" + uid +
????????????????", uname='" + uname + '\'' +
????????????????", upwd='" + upwd + '\'' +
????????????????", account=" + account +
????????????????'}';
????}
}

?

UserDao

package com.henu.dao;

import com.henu.bean.User;

import java.sql.Connection;


public interface UserDao {
????User findUserByunameAndupwd(String uanme, String upwd);
????int updateUserAbalance(Connection conn, double d, String uname, String upwd);
????User findUserByUid(int uid);
????int updateToUserAbalance(Connection conn,double d,int aid);
}

?

UserDaoImpl

package com.henu.dao.impl;

import com.henu.bean.Account;
import com.henu.bean.User;
import com.henu.dao.UserDao;
import com.henu.util.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Set;

public class UserDaoImpl implements UserDao {

????@Override
????public User findUserByunameAndupwd(String uanme, String upwd) {
????????QueryRunner qr = new QueryRunner(JdbcUtil.getDataSourse());
????????User user = new User();
????????Account account = new Account();
????????Map<String,Object> map = null;
????????String sql = "select * from user,account where uid=aid and uname=? and upwd=? ";
????????try {
????????????map = qr.query(sql,new MapHandler(),uanme,upwd);
????????????Set<Map.Entry<String,Object>> entries = map.entrySet();
????????????for (Map.Entry<String,Object> e : entries
?????????????????) {
????????????????if ("uid".equals(e.getKey())){
????????????????????user.setUid((int)e.getValue());
????????????????}else if ("uname".equals(e.getKey())){
????????????????????user.setUname((String)e.getValue());
????????????????}else if ("upwd".equals(e.getKey())){
????????????????????user.setUpwd((String)e.getValue());
????????????????}else if ("aid".equals(e.getKey())){
????????????????????account.setAid((int)e.getValue());
????????????????}else if ("abalance".equals(e.getKey())){
????????????????????account.setAbalance((double)e.getValue());
????????????????}
????????????}
????????????user.setAccount(account);
????????} catch (SQLException e) {
????????????e.printStackTrace();
????????}
????????return user;
????}

????@Override
????public int updateUserAbalance(Connection conn, double d, String uname, String upwd) {
????????User user = findUserByunameAndupwd(uname,upwd);
????????QueryRunner qr = new QueryRunner(JdbcUtil.getDataSourse());
????????int res = 0;
????????int aid = user.getUid();
????????double dd = user.getAccount().getAbalance() - d;
????????if (dd > 0) {
????????????Account account = new Account();
????????????String sql = "update account set abalance=? where aid = ?";
????????????try {
????????????????res = qr.update(sql, new Object[]{dd, aid});
????????????} catch (SQLException e) {
????????????????e.printStackTrace();
????????????}
????????????user.setAccount(account);
????????????return res;
????????}else{
????????????return 0;
????????}

????}

????@Override
????public User findUserByUid(int uid) {
????????QueryRunner qr = new QueryRunner(JdbcUtil.getDataSourse());
????????User user = new User();
????????Account account = new Account();
????????Map<String,Object> map = null;
????????String sql = "select * from user,account where uid = aid and uid = ?";
????????try {
????????????map = qr.query(sql,new MapHandler(),uid);
????????????Set<Map.Entry<String,Object>> entries = map.entrySet();
????????????for (Map.Entry<String,Object> e: entries
?????????????????) {
????????????????if ("uid".equals(e.getKey())){
????????????????????user.setUid((int)e.getValue());
????????????????}else if("uname".equals(e.getKey())){
????????????????????user.setUname((String)e.getValue());
????????????????}else if("upwd".equals(e.getKey())){
????????????????????user.setUpwd((String)e.getValue());
????????????????}else if("aid".equals(e.getKey())){
????????????????????account.setAid((int)e.getValue());
????????????????}else if ("abalance".equals(e.getKey())){
????????????????????account.setAbalance((double)e.getValue());
????????????????}
????????????????user.setAccount(account);
????????????}

????????} catch (SQLException e) {
????????????e.printStackTrace();
????????}
????????return user;
????}

????@Override
????public int updateToUserAbalance(Connection conn,double d, int aid) {
????????User user2 = findUserByUid(aid);
????????QueryRunner qr = new QueryRunner(JdbcUtil.getDataSourse());
????????int res = 0;
????????double dd = user2.getAccount().getAbalance()+d;
????????Account account = new Account();
????????String sql = "update account set abalance=? where aid=?";
????????try {
????????????res = qr.update(sql,new Object[]{dd,aid});
????????????user2.setAccount(account);
????????} catch (SQLException e) {
????????????e.printStackTrace();
????????}
????????return res;
????}


}

?

?

UserService

package com.henu.service;

import com.henu.bean.User;

import java.sql.Connection;

public interface UserService {
????User serviceLogin(String uname, String upwd);
????double QueryBalance(User user);
????int withdrawal(Connection conn,double d, String uname, String upwd);
????User serviceLoginByUid(int uid);
????int receiveTransfer(Connection conn,double dd,int uid);
}

?

?

UserServiceImpl

package com.henu.service.impl;

import com.henu.bean.User;
import com.henu.dao.UserDao;
import com.henu.dao.impl.UserDaoImpl;
import com.henu.service.UserService;

import java.sql.Connection;

public class UserServiceImpl implements UserService {
????UserDao userDao = new UserDaoImpl();
????@Override
????public User serviceLogin(String uname, String upwd) {
????????return userDao.findUserByunameAndupwd(uname,upwd);
????}

????@Override
????public double QueryBalance(User user) {

????????double balance = user.getAccount().getAbalance();
????????return balance;
????}

????@Override
????public int withdrawal(Connection conn, double d, String uname, String upwd) {
????????return userDao.updateUserAbalance(conn,d,uname,upwd);
????}

????@Override
????public User serviceLoginByUid(int uid) {
????????return userDao.findUserByUid(uid);
????}

????@Override
????public int receiveTransfer(Connection conn,double dd, int uid) {
????????return userDao.updateToUserAbalance(conn,dd,uid);
????}

}

?

MainClass

package com.henu;

import com.henu.bean.User;
import com.henu.service.UserService;
import com.henu.service.impl.UserServiceImpl;
import com.henu.util.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

public class MainClass {
????public static void main(String[] args) {

????????Scanner sc = new Scanner(System.in);
????????System.out.println("Welcome to mengdong bank!");
????????System.out.println("Please enter your name:");
????????String uname = sc.nextLine();
????????System.out.println("Please enter your pwd:");
????????String upwd = sc.nextLine();

????????UserService uService = new UserServiceImpl();
????????User user = new User();
????????try {
????????????user = uService.serviceLogin(uname, upwd);
????????}catch (NullPointerException e){
????????????throw new RuntimeException("No such user!Please restart the program!");
????????}

????????boolean flag = true;
????????while(flag){
????????????System.out.println("--------------------------------");
????????????menu();
????????????System.out.println("--------------------------------");
????????????System.out.println("Please enter your choose:");
????????????String choose = sc.nextLine();
????????????switch (choose){
????????????????case "1" :
????????????????????User newUser = uService.serviceLogin(uname,upwd);
????????????????????double d1 = uService.QueryBalance(newUser);
????????????????????System.out.println("********************************");
????????????????????System.out.println("Your Balance is:" + d1);
????????????????????System.out.println("********************************");
????????????????????flag = true;
????????????????????break;
????????????????case "2" :
????????????????????System.out.println("Please enter your withdrawal amount:");
????????????????????String str = sc.nextLine();
????????????????????double d2 = Double.parseDouble(str);
????????????????????Connection conn1 = null;
????????????????????try {
????????????????????????conn1 = JdbcUtil.getConnection();
????????????????????????conn1.setAutoCommit(false);
????????????????????????uService.withdrawal(conn1,d2,uname,upwd);
????????????????????????conn1.commit();
????????????????????} catch (SQLException e) {
????????????????????????e.printStackTrace();
????????????????????????try {
????????????????????????????conn1.rollback();
????????????????????????} catch (SQLException e1) {
????????????????????????????e1.printStackTrace();
????????????????????????}
????????????????????}
????????????????????System.out.println("********************************");
????????????????????System.out.println("You withdraw "+ d2 + " yuan in cash.");
????????????????????System.out.println("********************************");
????????????????????flag = true;
????????????????????break;
????????????????case "3" :
????????????????????System.out.println("Please enter the user id you want to transfer:");
????????????????????String str2 = sc.nextLine();
????????????????????System.out.println("Please enter your transfer amount:");
????????????????????String str3 = sc.nextLine();
????????????????????int to = Integer.parseInt(str2);
????????????????????double d3 = Double.parseDouble(str3);
????????????????????if (to == user.getUid()){
????????????????????????System.out.println("********************************");
????????????????????????System.err.println("Here is your account number!");
????????????????????????System.out.println("********************************");
????????????????????}else {
????????????????????????User user2 = null;
????????????????????????try {
????????????????????????????user2 = uService.serviceLoginByUid(to);
????????????????????????} catch (NullPointerException r) {
????????????????????????????System.err.println("********************************");
????????????????????????????System.err.println("There is no such user id!");
????????????????????????????System.err.println("********************************");
????????????????????????}
????????????????????????if (user2 != null) {
????????????????????????????Connection conn = null;
????????????????????????????try {
????????????????????????????????conn = JdbcUtil.getConnection();
????????????????????????????????conn.setAutoCommit(false);
????????????????????????????????uService.withdrawal(conn, d3, uname, upwd);
????????????????????????????????uService.receiveTransfer(conn, d3, user2.getUid());
????????????????????????????????conn.commit();
????????????????????????????} catch (Exception e) {
????????????????????????????????System.err.println("System error!");
????????????????????????????????try {
????????????????????????????????????conn.rollback();
????????????????????????????????} catch (SQLException e1) {
????????????????????????????????????e1.printStackTrace();
????????????????????????????????}
????????????????????????????}
????????????????????????????System.out.println("********************************");
????????????????????????????System.out.println("Transfer Success!");
????????????????????????????System.out.println("********************************");
????????????????????????} else {
????????????????????????????System.err.println("********************************");
????????????????????????????System.err.println("There is no such user id!");
????????????????????????????System.err.println("********************************");
????????????????????????}
????????????????????}
????????????????????flag=true;
????????????????????break;
????????????????case "4" :
????????????????????flag = false;
????????????????????System.exit(0);
????????????????????break;
????????????????default:
????????????????????System.err.println("Your input error!!!");
????????????????????flag = true;
????????????????????break;
????????????}
????????}

????}
????public static void menu(){
????????System.out.println("Welcome to the homepage!!!");
????????System.out.println("We provide the following functionality!");
????????System.out.println("1.Query balance");
????????System.out.println("2.withdrawal");
????????System.out.println("3.transfer accounts");
????????System.out.println("4.exit");
????}

}

?

?

?

總結

以上是生活随笔為你收集整理的对于多表查询和转账的事务提交的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。