0

i need to insert data in two diferents tables, so i don't understand how interact the two objects for insert the data. i have two classes class a and class b both have two methods addA and addb the class a is composed by a classB object so in my method addA i create a new object of classB and call the addB method, IN both methods i use a connection and a preparedStatement with commit, but i need to do a rollback if the method addB doesen't work, someone could give me an example of this?, thanks!

EXAMPLE OF CLASS A
class a{

int id;
String name;
int age;

a(String name, int age){
 this.name= name;
 this.age=age;

}

public boolean addA( ) throws Exception {

    conecctions.Conn.getConnection();
    Statement stmt = null;
    Connection conection = conecctions.Conn.connection;
    Boolean result = false;


    try {
        conection.setAutoCommit(false);

        stmt = conection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        String addnew= "insert into table_a values (?,?)"

         PreparedStatement prepared1= conection.prepareStatement(addnew);
         prepared1.setString(1, "name");
         prepared2.setInt(2, 25);

        prepared1.executeUpdate();

        ResultSet keys = prepared1.getGeneratedKeys();
        int lastKey = 1;
        while (keys.next()) {
          lastKey = keys.getInt(1);
        }
        this.id=lastKey;

        result = true;
        conection.commit();


        b newB= new b(this.id, this.age);
        newb.addB();

catch (Exception e2) {

        if (conection != null) {

            try {


                System.out.println("rollback");

            } catch (Exception e1) {
                e1.printStackTrace();

            }

        }
        e2.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
    }// nothing we can do
        try {
            if (conection != null)
                conection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }


    return result;


}

EXAMPLE OF CLASS B

   class b{

int id;
int idA;
int ageA;

b(String idA, int ageA){
 this.idA= idA;
 this.ageA=ageA;

}

public boolean addB( ) throws Exception {

    conecctions.Conn.getConnection();
    Statement stmt = null;
    Connection conection = conecctions.Conn.connection;
    Boolean result = false;


    try {
        conection.setAutoCommit(false);

        stmt = conection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        String addnew= "insert into table_b values (?,?)"

         PreparedStatement prepared2= conection.prepareStatement(addnew);
         prepared2.setInt(1, a_id);
         prepared2.setInt(2, a_age);

        prepared2.executeUpdate();



        result = true;
        conection.commit();


catch (Exception e2) {

        if (conection != null) {

            try {


                System.out.println("rollback");

            } catch (Exception e1) {
                e1.printStackTrace();

            }

        }
        e2.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
    }// nothing we can do
        try {
            if (conection != null)
                conection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }


    return result;


}

its just a example

juneau
  • 11
  • 2
  • im new in java so i dontu uderstand preatty well, i was reading about JTA or commit 2 phase but i dont know if this are necessary to achieve that, thanks! – juneau Feb 06 '18 at 02:10
  • 2
    What have you tried to achieve your wanted results? What has your research concerning your problem shown? Can you provide code of your tries? [How do I ask a good question](//stackoverflow.com/help/how-to-ask), [How much research effort is expected](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), and [How to create a Minimal, Complete, and Verifiable example](//stackoverflow.com/help/mcve) might be helpful to improve your question. – peacetype Feb 06 '18 at 02:31

2 Answers2

0

Simplest approach is to make 2 DAOs classes: 1 that will insert data into first table, 2nd DAO is to insert data to another table. Your DAO classes should extend a DAO abstract class where you initiated the connection and has the disconnect() method to disconnect the connection, resultset, preparestatement.

The data that you are going to insert, where is it coming from?

kokko moh
  • 16
  • 2
0

If you want to rollback if any of these methods (addA or addB) fail, then they belong to the same transaction, so make your insert/updates inside object A and object B and leave the transaction management (try - catch, conn.setAutocommit(false), conn.commit(), conn.rollback()) to another object outside. See also this question here and a basic documentation for jdbc transactions here.