1.11M Members

Inserting into two different tables using java

 
0
 

Please how can i insert into two different tables in mysql using java. i tried this and its only for one table. so how can i insert into branches and items table simultaneously in the same code?

  try{
            String sql = "Insert into asset_update (Date_Acquired,Item_Code,Serial_Number,Cost_Of_Acquisition,Estimated_Useful_Life,Depreciation_Start_Date,User,Status,Disposal_Date)values (?,?,?,?,?,?,?,?,?)"; 
        pst=conn.prepareStatement(sql);
        pst.setString(1, ((JTextField)date_aquired.getDateEditor().getUiComponent()).getText());
        String ItemCode = ComboBox_itemCode.getSelectedItem().toString();
        pst.setString(2, ItemCode );
        pst.setString(3, txt_sn.getText());
        pst.setString(4, txt_coa.getText());
        pst.setString(5, eul.getText());
        pst.setString(6, ((JTextField)date_dsd.getDateEditor().getUiComponent()).getText());
        String users = jComboBox_Users.getSelectedItem().toString();
        pst.setString(7, users);
        String status = ComboBox_status.getSelectedItem().toString();
        pst.setString(8, status);
        pst.setString(9, ((JTextField)date_dd.getDateEditor().getUiComponent()).getText());
        pst.execute();
        JOptionPane.showMessageDialog(null, "Saved"); 
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
    }            
 
0
 

As far as I know you can't insert into two tables with one SQL insert statement, so you need to prepare two statements, set all their strings, and execute them both. You may need to wrap them in a transaction if you are worried about database integrity after a partial failure.
Is there any reason why this would be a problem in your application?

 
0
 

this is the full code but it gives me errors.

try{
            String sql = "Insert into asset_update (Date_Acquired,Item_Code,Serial_Number,Cost_Of_Acquisition,Estimated_Useful_Life,Depreciation_Start_Date,User,Status,Disposal_Date)values (?,?,?,?,?,?,?,?,?)"; 
        pst=conn.prepareStatement(sql);
        pst.setString(1, ((JTextField)date_aquired.getDateEditor().getUiComponent()).getText());
        String ItemCode = ComboBox_itemCode.getSelectedItem().toString();
        pst.setString(2, ItemCode );
        pst.setString(3, txt_sn.getText());
        pst.setString(4, txt_coa.getText());
        pst.setString(5, eul.getText());
        pst.setString(6, ((JTextField)date_dsd.getDateEditor().getUiComponent()).getText());
        String users = jComboBox_Users.getSelectedItem().toString();
        pst.setString(7, users);
        String status = ComboBox_status.getSelectedItem().toString();
        pst.setString(8, status);
        pst.setString(9, ((JTextField)date_dd.getDateEditor().getUiComponent()).getText());

        String branches ="insert into branches(Branch_Area)Values(?)";
        pst=conn.prepareStatement(branches);
        String barea = combo_Area.getSelectedItem().toString();
        pst.setString(1, barea);

        String items ="insert into items(Description,Description_Code,Depreciation_Method)Values(?,?,?)";
        pst=conn.prepareStatement(items);
        String decode = ComboBox_decCode.getSelectedItem().toString();
        pst.setString(1, decode);
        String depm = ComboBox_dm.getSelectedItem().toString();
        pst.setString(2, depm);
        pst.setString(3, txt_desc.getText());  
        pst.execute();
        JOptionPane.showMessageDialog(null, "Saved"); 
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
    }                 
 
0
 

"It gives me errors" tells us nothing. If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be.

 
0
 

without you telling us what the errors are, nobody's going to be able to help you.

 
0
 

So that people do not have to repeat themselves this is also posted here.

 
0
 

i have been able to solve the problem. thanx y'all

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: