You can do it using
JDBC. When you create a Connection object it is set to autocommit by default. First of all after creating the Connection object set the autocommit to false.
Connection con = DriverManager.getConnection(params);
con.setAutoCommmit(false);
Now you insert the records in 1st table and then second table in try catch block.
Try{
Connection con = DriverManager.getConnection(params);
con.setAutoCommmit(false);
//insert records in table1
//insert records in table2
con.commit(); //records entered successfully
}catch(Exception ex){
con.rollback(); //It means there is some problem while entering records
}