比如我现在向Jtable添加一行,然后编辑,我想将Jtable的数据保存到数据库,该怎么做?

解决方案 »

  1.   

    把那一行的数据保存下来, 假如你用的是sql的话, 生成insert into 语句, 然后执行就可以了
      

  2.   

    建議 連接JDBC的次數盡可能的少   比如全編輯完畢 把table中的數據一起插入數據庫 不要每編輯一條插一次~~
      

  3.   

      Connection conn = Connect.Connect();
            String dishName = dishNameTF.getText();
            String sqlStr = "select *  from menus where name = '"+ dishName + "' order by menuid asc";
            ResultSet rs = new ForeMarg(conn).select(sqlStr);
            try{
                if(rs.next()) {
                    JOptionPane.showMessageDialog(this,"该物品已经存在!","添加失败",JOptionPane.INFORMATION_MESSAGE);
                }else if(dishName.equals("") & downPercentTF.getText().equals("") & priceTF.getText().equals("")  & purchaseTF.getText().equals("")){
                    JOptionPane.showMessageDialog(this,"请输入物品号!","添加失败",JOptionPane.INFORMATION_MESSAGE);
                } else{
                    Object[] data= {
                        dishNameTF.getText(),
                        codeTF.getText(),
                        kindComBox.getSelectedItem(),
                        unitComBox.getSelectedItem(),
                        priceTF.getText(),
                        purchaseTF.getText(),
                        downPercentTF.getText()
                    };
                    modelc.insertRow(modelc.getRowCount(),data);
                    String desksStr = "insert into menus (name,code,kindid,unitid,price,purchase,downPercent) values('" + data[0] +"','" + data[1]+ "','"+ data[2]+ "','"+ data[3]+"','" +data[4]+ "','"+data[5]+"','"+data[6]+ "')";
                    new ForeMarg(conn).updateOrInsert(desksStr);
                    dishNameTF.setText("");
                    codeTF.setText("");
                    priceTF.setText("");
                    purchaseTF.setText("");
                    downPercentTF.setText("");
                }
            }catch(Exception ee){ee.printStackTrace();}
        }//GEN-LAST:event_addDishActionPerformed
      

  4.   

    读取jtable里面一行的数据。jtable.setValueAt(row,col)方法然后再录入到数据库中去。