The transaction is a set of Sql statement that can be executed as a single unit.the transaction is complete only when all the sql statements in a transaction excute successfully.if any one of the sql statement in the transaction fails the entire transaction is rolled back.
paste the code inside the main method
import java.sql.*;
public class Transaction
{
public static void main(String args[])
{
Connection con;
PreparedStatement ps1;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/vmproducts","root","");
con.setAutoCommit(false);
ps1 = con.prepareStatement("insert into product(pname,price,qty)values(?,?,?)");
ps1.setString(1,"Mouse");
ps1.setInt(2,15000);
ps1.setInt(3,100);
int first = ps1.executeUpdate();
System.out.println("First Row Inserted but not commited");
ps1 = con.prepareStatement("insert into product(pname,price,qty)values(?,?,?)");
ps1.setString(1,"Keyborad");
ps1.setInt(2,5000);
ps1.setInt(3,500);
int second = ps1.executeUpdate();
System.out.println("Second Row Inserted but not commited");
ps1 = con.prepareStatement("insert into product(pname,price,qty)values(?,?,?)");
ps1.setString(1,"Printer");
ps1.setInt(2,10000);
ps1.setInt(3,100);
int third = ps1.executeUpdate();
System.out.println("Third Row Inserted but not commited");
/*Commit a trasaction */
con.commit();
System.out.println("Transaction Commitedddddd");
} catch (ClassNotFoundException | SQLException e)
{
e.printStackTrace();
}
}
}
What Is the Tesla Pi Phone? Imagine if Tesla, the company that makes famous…
Inventory Management POS systems are now an essential part of modern businesses such as bookshops,…
If you're just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for…
GitHub is a powerful tool used by teams and developers around the globe. This guide is…
It's like having a super-smart buddy that is always there to help you write stories,…
The UK is known for its rich history, diverse culture, and most of all its…