Created
July 14, 2021 13:38
-
-
Save pablohdzvizcarra/1bc54332822d95ee865001bcb975b18a to your computer and use it in GitHub Desktop.
way to create two transactions in MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pluralsight.jdbc.course.m7c2; | |
import java.sql.*; | |
public class OrderComponent | |
{ | |
public static void main(String[] args) throws SQLException | |
{ | |
int customer = 112; | |
LineItem lineItem = | |
new LineItem("S10_1949", 10, 100.00); | |
int result = OrderComponent.OrderComponentTwoTransactions(customer, lineItem); | |
System.out.println(result); | |
} | |
private static int OrderComponentTwoTransactions(int customer, LineItem lineItem) throws SQLException | |
{ | |
String query = "INSERT INTO orders (orderDate, requiredDate, status, customerNumber) " | |
+ "VALUES (now(), now(), 'In Process', ?)"; | |
try (Connection connection = DriverManager.getConnection( | |
"jdbc:mysql://localhost:3306/classicmodels", | |
"root", | |
"my-secret-pw" | |
); | |
PreparedStatement orderStatement = connection.prepareStatement(query, | |
Statement.RETURN_GENERATED_KEYS)) | |
{ | |
connection.setAutoCommit(false); | |
orderStatement.setInt(1, customer); | |
orderStatement.executeUpdate(); | |
try (ResultSet resultOrder = orderStatement.getGeneratedKeys()) | |
{ | |
if (!resultOrder.next()) | |
{ | |
connection.rollback(); | |
return 0; | |
} | |
int orderNumber = resultOrder.getInt(1); | |
String queryInsert = | |
"INSERT INTO orderdetails " | |
+ "(orderNumber, productCode, quantityOrdered, priceEach, orderLineNumber) " | |
+ "VALUES (?, ?, ?, ?, ?)"; | |
try (PreparedStatement detailsStatement = connection.prepareStatement(queryInsert)) | |
{ | |
detailsStatement.setInt(1, orderNumber); | |
detailsStatement.setString(2, lineItem.getProductCode()); | |
detailsStatement.setInt(3, lineItem.getQuantityOrder()); | |
detailsStatement.setDouble(4, lineItem.getPriceEach()); | |
detailsStatement.setDouble(5, 1); | |
int count = detailsStatement.executeUpdate(); | |
if (count == 1) | |
{ | |
connection.commit(); | |
return orderNumber; | |
} else | |
{ | |
connection.rollback(); | |
return 0; | |
} | |
} | |
} catch (Exception exception) | |
{ | |
connection.rollback(); | |
throw exception; | |
} | |
} | |
} | |
public static int OrderComponentTwoTransactions(int customerOrder, String productCode, | |
int quantityOrder, double priceEach) throws SQLException | |
{ | |
String query = "INSERT INTO orders (orderDate, requiredDate, status, customerNumber) " | |
+ "VALUES (now(), now(), 'In Process', ?)"; | |
try (Connection connection = DriverManager.getConnection( | |
"jdbc:mysql://localhost:3306/classicmodels", | |
"root", | |
"my-secret-pw" | |
); | |
PreparedStatement orderStatement = connection.prepareStatement(query, | |
Statement.RETURN_GENERATED_KEYS)) | |
{ | |
connection.setAutoCommit(false); | |
orderStatement.setInt(1, customerOrder); | |
orderStatement.executeUpdate(); | |
try (ResultSet resultOrder = orderStatement.getGeneratedKeys()) | |
{ | |
if (!resultOrder.next()) | |
{ | |
connection.rollback(); | |
return 0; | |
} | |
int orderNumber = resultOrder.getInt(1); | |
String queryInsert = | |
"INSERT INTO orderdetails " | |
+ "(orderNumber, productCode, quantityOrdered, priceEach, orderLineNumber) " | |
+ "VALUES (?, ?, ?, ?, ?)"; | |
try (PreparedStatement detailsStatement = connection.prepareStatement(queryInsert)) | |
{ | |
detailsStatement.setInt(1, orderNumber); | |
detailsStatement.setString(2, productCode); | |
detailsStatement.setInt(3, quantityOrder); | |
detailsStatement.setDouble(4, priceEach); | |
detailsStatement.setInt(5, 1); | |
int count = detailsStatement.executeUpdate(); | |
if (count == 1) | |
{ | |
connection.commit(); | |
return orderNumber; | |
} else | |
{ | |
connection.rollback(); | |
return 0; | |
} | |
} | |
} catch (Exception exception) | |
{ | |
connection.rollback(); | |
throw exception; | |
} | |
} | |
} | |
private static class LineItem | |
{ | |
private final String productCode; | |
private final int quantityOrder; | |
private final double priceEach; | |
public LineItem(String productCode, int quantityOrder, double priceEach) | |
{ | |
super(); | |
this.productCode = productCode; | |
this.quantityOrder = quantityOrder; | |
this.priceEach = priceEach; | |
} | |
public String getProductCode() | |
{ | |
return productCode; | |
} | |
public int getQuantityOrder() | |
{ | |
return quantityOrder; | |
} | |
public double getPriceEach() | |
{ | |
return priceEach; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment