質問

i have made a jtable where i am giving the values manually,and saving the data in the sql by clicking the save(jButton2) button.now the problem is that when i am keeping the jtable blank and saving it..its showing data saved,but nothing is saving in the database.

And when i am putting the data in the jtable and trying to save its giving exception.

column(i,5) is getting value by multiplying column(i,3) and column(i,4)

jButton2ActionPerformed(save button) method is as follows:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
     try {
String sql="insert into `sampleDB`.`Item_detail` (P_name,Manuf_name,Model_no,Purchase_price,Available,total Price,Sell_price,Quality,Color,Description,profit,Cost_price,idDealerItem) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";

         pst=con.prepareStatement(sql);

            int last=jTable1.getRowCount();
            for( int i=0;i<last;i++){
                if(((String) jTable1.getValueAt(i,0))!=null){
            String column1= (String) jTable1.getValueAt(i,0);
            String column2= (String) jTable1.getValueAt(i,1);
            String column3= (String) jTable1.getValueAt(i,2);
            String column4= (String) jTable1.getValueAt(i,3);
            String column5= (String) jTable1.getValueAt(i,4);                
            String column6= (String) jTable1.getValueAt(i,5);
            String column7= (String) jTable1.getValueAt(i,6);   

            pst.setString(1,column1);
            pst.setString(2,column2);
            pst.setString(3,column3);
            pst.setString(4,column4);
            pst.setString(5,column5);
            pst.setString(6,column6);
            pst.setString(7,null);
        pst.setString(8,column7);
        pst.setString(9,null);
        pst.setString(10,null);
        pst.setString(11,null);
        pst.setString(12,null);
        pst.setString(13,null);



            pst.execute();
            } 
       }

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

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

Method where columns are getting multiplied is as follow.:

private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {                                   
// TODO add your handling code here:
    if(evt.getKeyCode()==KeyEvent.VK_TAB){
       int r = jTable1.getRowCount();
    for (int i = 0; i < r; i++){
     if(jTable1.getValueAt(i,3)!=null && jTable1.getValueAt(i,4)!=null ){
        int a=Integer.parseInt(jTable1.getValueAt(i,3).toString());
                    int b=Integer.parseInt(jTable1.getValueAt(i,4).toString());
                     int d= a*b;
                     jTable1.setValueAt(d,i,5); 
     }
    } 
    }
}  

new error is:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Price,Sell_price,Quality,Color,Description,profit,Cost_price,idDealerItem) value' at line 1
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:1054)
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:331)
at Extra.access$300(Extra.java:14)
at Extra$4.actionPerformed(Extra.java:121)
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)

Please help.. thanks in advance..

役に立ちましたか?

解決

You have ClassCastException because of your JTable store Integer values which can't be cast to String like next String column1= (String) jTable1.getValueAt(i,0);. Change that to:

String column1= jTable1.getValueAt(i,0) == null ? "" : jTable1.getValueAt(i,0).toString();

他のヒント

You can't cast Integer to String, but you can write

Integer integer = new Integer(4);
String string = integer.toString(); // "4"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top