Trax
Trax

Reputation: 17

how to insert value from jtable to mysql?

Im trying to insert data from jtable to database!! the first three columns(stafftimetableid,staffname,staffid) are inserted from the jtexfield(no errors found,successfully added) but when im trying to insert from jtable it promts a java.null pointerExcetion error !!

I have no errors in database connection !!

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (jComboBox1.getSelectedItem().equals("Staff Time Table"))
        {
            try 
            {
                PreparedStatement pst =null;
                Connection con = clerkpanell.DBConnection.connectDB();
                String data=jTable2.getValueAt(0,1).toString();

                String sql = "insert into stafftimetable      (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.getText()+"','"+staffname.getText()+"','"+staffid.getText()+"','"+data+"');";


                 pst=con.prepareStatement(sql);
                 pst.executeUpdate();
             //    JOptionPane.showMessageDialog(null,"Added");
            }
            catch (Exception e)

            {
                JOptionPane.showMessageDialog(null,e);

            }    
        }   

Upvotes: 0

Views: 784

Answers (1)

Code Geek
Code Geek

Reputation: 485

In this statement String sql = "insert into stafftimetable (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.getText()+"','"+staffname.getText()+"','"+staffid.getText()+"','"+data+"');";

Please store the ttid.getText(), staffname.getText(),staffid.getText()into separate variables. Something like this,

String ttid=ttid.getText();
String staffname = staffname.getText();
String staffid = staffid.getText();

and then the insert statement should be something like this

String sql = "insert into stafftimetable      (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.+"','"+staffname+"','"+staffid+"','"+data+"');";

Upvotes: 1

Related Questions