0

I have made a project in netbeans where i have to add the data from the jtable into the database..my main class extends javax.swing.JFrame and implements TableModelListener..

my codes are.:

public class Extra extends javax.swing.JFrame implements TableModelListener{
     Connection con=null;
    ResultSet rs=null;
    PreparedStatement pst=null;
    DefaultTableModel model;



    public Extra() {
        initComponents();
        con=DbConnect.Connection();
        jTable1.setShowGrid(true);
      jTable1.getModel().addTableModelListener(this);





    } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
     try {
            String sql="insert into item (id,item,brand,model,price,quan,total,qual) values(?,?,?,?,?,?,?,?)";
            pst=con.prepareStatement(sql);



            int last=jTable1.getRowCount();
            String column1= (String) jTable1.getValueAt(last-1,0);
            String column2= (String) jTable1.getValueAt(last-1,1);
            String column3= (String) jTable1.getValueAt(last-1,2);
            String column4= (String) jTable1.getValueAt(last-1,3);
            String column5= (String) jTable1.getValueAt(last-1,4);
            String column6= (String) jTable1.getValueAt(last-1,5);
            String column7= (String) jTable1.getValueAt(last-1,6);
            pst.setString(1,null);
            pst.setString(2,column1);
            pst.setString(3,column2);
            pst.setString(4,column3);
            pst.setString(5,column4);
            pst.setString(6,column5);
            pst.setString(7,column6);
            pst.setString(8,column7);

            pst.execute();
            JOptionPane.showMessageDialog(null, "data saved");

        }
        catch(Exception e) {
            e.printStackTrace();
        }
}


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Extra().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration

    @Override
    public void tableChanged(TableModelEvent tme) {
        System.out.println("Table Changed!");
    }





}

my codes to insert the data of the jtable is written in jButton2ActionPerformed() method.

now the problem is when i am inputting the data in the jtable and trying to save it..it is giving me the following error..:

Table Changed!
java.sql.SQLException: No value specified for parameter 8
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2560)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2536)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2462)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1300)
    at Extra.jButton2ActionPerformed(Extra.java:264)
    at Extra.access$100(Extra.java:12)
    at Extra$2.actionPerformed(Extra.java:102)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 22 seconds)

please help..

error ater editing..:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1364)
    at Extra.jButton2ActionPerformed(Extra.java:265)
    at Extra.access$100(Extra.java:12)
    at Extra$2.actionPerformed(Extra.java:102)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 55 seconds)
8
  • 2
    The error message could hardly be clearer. It says that you haven't provided a value for the 8th parameter. And indeed, you've provided a value for the first 7 parameters, but not for the 8th. Have you read the message? Commented Feb 15, 2014 at 7:44
  • yes i have changed the codes..but still the problem persist..please check i have edited the codes Commented Feb 15, 2014 at 7:51
  • What is the new stack trace you get? The key point is to read the error messages. Commented Feb 15, 2014 at 7:52
  • 1
    is id you're primary key? If it is, don't set it to null. If it's auto-adjusting, don't even specify a ? for it. Just use 7 instead of 8, if you're not going to put a value. Commented Feb 15, 2014 at 7:57
  • 2
    @AM26: the error message, once again, could hardly be clearer: "Column 'id' cannot be null". Yet, you're setting it to null. Why don't you read error messages? Commented Feb 15, 2014 at 8:02

1 Answer 1

2

You're not filling your prepared statement completely. Specifically, you're not giving a value for the qual column. Hope that helps!

Sign up to request clarification or add additional context in comments.

5 Comments

yes i have changed the codes..but still the problem persist..please check i have edited the codes
It doesn't show as edited, @AM26, edit your question
its working but its not saving the value of qual of the last row..this problem is happening if i am keeping the last qual selected. If i click on any other column after entering the last qual then its adding everything in the data..y its happening..help
its also saving the empty rows in the database..what should i do that it only takes the filled rows..
null-checking on the argument?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.