Transaction Resultset in Java With Example. This Java Transaction example describes the basic operations performed on the Transaction database connectivity code.In this example you will learn about how to write java code for Transaction with oracle database.
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TransactionDemo {
public static void main(String args[]){
Connection con=null;
Statement st = null;
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:ORCL”,”scott”,”tiger”);
con.setAutoCommit(false);
st = con.createStatement();
st.executeUpdate(“delete from eshusoft where username=’myuser'”);
System.out.print(“Do U want to delete(yes/no):”);
String ch = new BufferedReader(new InputStreamReader(System.in)).readLine();
if(ch.equalsIgnoreCase(“yes”)){
con.commit();
System.out.print(“rows is deleted”);
} else if(ch.equalsIgnoreCase(“no”)){
con.rollback();
System.out.print(“rows is not deleted”);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result:
Do you want to delete(yes/no) : yes
row is deleted