1,105,263 Community Members

Inserting into two different tables using java

Member Avatar
Benjamin_4
Junior Poster in Training
54 posts since Sep 2013
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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);
        }
    }            
Member Avatar
JamesCherrill
... trying to help
10,363 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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?

Member Avatar
Benjamin_4
Junior Poster in Training
54 posts since Sep 2013
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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);
        }
    }                 
Member Avatar
JamesCherrill
... trying to help
10,363 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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.

Member Avatar
jwenting
duckman
8,333 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

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

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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

Member Avatar
Benjamin_4
Junior Poster in Training
54 posts since Sep 2013
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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