从数据库把数据写入NetBeans表格中

显示到表中

 ArrayList resultlist = Dao.Showuesr();
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        for (Iterator i = resultlist.iterator(); i.hasNext();) {
            Vector map = (Vector) i.next();
            dtm.addRow(map);

修改数据

显示 private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased
        // TODO add your handling code here:
        int row = jTable1.getSelectedRow();
        if (row > -1) {
            String name = jTextField1.getText();
            String password = jTextField2.getText();
            String banji = jTextField3.getText();
            //修改表里的数据
            jTable1.setValueAt(name, row, 0);
            jTable1.setValueAt(password, row, 1);
            jTable1.setValueAt(banji, row, 2);


            //数据库中修改
            //JiushuiMenuInf si = new JiushuiMenuInf(namestr,pricestr);
            Dao.Updateuser(name, password, banji);
        }
  //增加按钮
    private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseReleased
        // TODO add your handling code here:
        String name = jTextField1.getText();
        String password = jTextField2.getText();
        String banji = jTextField3.getText();
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        Vector al = new Vector();
        al.add(name);
        al.add(password);
        al.add(banji);
        dtm.addRow(al);
       //数据库中增加


        Dao.Adduser(name, password, banji);
    }//GEN-LAST:event_jButton3MouseReleased
    //删除按钮
    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseReleased
        // TODO add your handling code here:
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        int row = jTable1.getSelectedRow();
        System.out.println(row);
        if (row == -1) {
            System.out.println("请先选择需要删除的信息");
            return;
        }
        //界面表中删除数据
        dtm.removeRow(row);
        jTextField1.setText("");


        //数据库中删除该记录
        String name = jTextField1.getText();
        Dao.Deleteuser(name);
    }//GEN-LAST:event_jButton2MouseReleased


你可能感兴趣的:(从数据库把数据写入NetBeans表格中)