Home Java Transaction using java mysql

Transaction using java mysql

2 min read
0
0
582

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();
		}
		
		
	}

}

I have attached the video tutorial below it will help you  to do this  step by step.

 

  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
  • Is Java Hard to Learn?

    Is Java Hard to Learn is it True? Java is one of the most popular programming languages in…
  • How to generate random with weight java

    Generating Random Numbers with Weights in Java Random number generation with weighted prob…
Load More Related Articles
  • Conditional Statements in Python

    Conditional statements in Python allow us to control the flow of execution based on condit…
  • Java Beans

    A Java Bean is a reusable software component that follows a specific set of conventions. J…
  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
Load More By admin
  • Java Beans

    A Java Bean is a reusable software component that follows a specific set of conventions. J…
  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
  • Java Developer Jobs

    Java remains one of the most in-demand programming languages worldwide, powering everythin…
Load More In Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Conditional Statements in Python

Conditional statements in Python allow us to control the flow of execution based on condit…