简单的事务

看看就好程序有很多问题
没有优化,看着有点乱,希望能帮到你,有很多地方都注释,看看吧
关于事务的就三句
connection.setAutoCommit(false); //取消事务的自动提交
connection.commit(); //手动提交事务
try {
connection.rollback(); //事务回滚
} catch (SQLException e1) {
e1.printStackTrace();
}

第一张表

第二张表

package com.rmq.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.rmq.bean.Commodity;

public class Dao {

private static String url = "jdbc:mysql://localhost:3306/lulujdbc1?characterEncoding=UTF8";
private static String name = "root";
private static String passWord = "root";

static {
	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}


public static void main(String[] args) {

// )查询出库存GoodsNum为零的商品列表
List commoditys2 = queryGoodsName2(0);
for(Commodity c: commoditys2) {
System.out.println©;
}

// 完成售货的操作:要求用户输入 购买商品id=1 在商品表中将该商品的GoodsNum减1,
// 在SaleInfo中新增一条该商品的销售记录,要求在同一个事务中完成更新操作。
upDate(5); //括号里面是id

}

// 完成售货的操作:要求用户输入 购买商品id=1 在商品表中将该商品的GoodsNum减1,
// 在SaleInfo中新增一条该商品的销售记录,要求在同一个事务中完成更新操作。
private static void upDate(int goodsId) {
//2.创建Connection对象 ,连接数据库
Connection connection = null;
//3.获取PreparedStatement 对象操作数据库 ,增删改查
PreparedStatement statement = null;
PreparedStatement statement2 = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection(url, name, passWord);
connection.setAutoCommit(false); //取消事务的自动提交

		//4.创建sql
		String sql = "UPDATE l_goodsinfo SET goodsNum = goodsNum-1 WHERE goodsId = ?";
		statement = connection.prepareStatement(sql);
		statement.setInt(1, goodsId);
		int number = statement.executeUpdate();
		System.out.println("成功");

// int a = 10/0; //测试

		String sql2 = "INSERT INTO l_saleinfo(goodsID,price) SELECT goodsID,price FROM l_goodsinfo WHERE goodsID= ?;";
		statement2 = connection.prepareStatement(sql2);
		statement2.setInt(1, goodsId);
		int number2 = statement2.executeUpdate();
		System.out.println("成功");

		connection.commit();  //手动提交事务
		
	} catch (Exception e) {
		e.printStackTrace();
		try {
			connection.rollback();   //事务回滚
		} catch (SQLException e1) {
			e1.printStackTrace();
		}  
	}finally {
		//6.关闭连接,释放资源
		if(connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}

//1)查询出库存GoodsNum为零的商品列表
private static List queryGoodsName2(int goodsNum) {
List commoditys = new ArrayList();
//2.创建Connection对象 ,连接数据库
Connection connection = null;
//3.获取PreparedStatement 对象操作数据库 ,增删改查
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection(url, name, passWord);
//4.创建sql
String sql = “SELECT GoodsName FROM l_GoodsInfo WHERE goodsNum= ?”;
statement = connection.prepareStatement(sql);
statement.setInt(1, goodsNum);
//5.执行,得到返回结果resultSet 对象,对结果进行遍历
resultSet = statement.executeQuery();
while(resultSet.next()) {
Commodity commodity = new Commodity();
// commodity.setGoodsID(resultSet.getInt(“goodsID”));
commodity.setGoodsName(resultSet.getString(“goodsName”));
// commodity.setGoodsNum(resultSet.getInt(goodsNum));
// commodity.setPrice(resultSet.getDouble(“price”));
commoditys.add(commodity);

		}
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		//6.关闭连接,释放资源
		if(connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	return commoditys;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值