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

Recommended Answers

All 6 Replies

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?

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

"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.

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.