(Netbeans , Swing , SQL 2005)将表格里的所有数据增加到数据库,请问这段代码应该怎么修改?点击按钮没有反应    int rows = jTable1.getRowCount();
    String c1 = null;
    for (int i = 0; i < rows; i++) {
        c1 = (String) jTable1.getValueAt(i, 1);
    }
    String sql = "SELECT * FROM info";
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=Database", "sa", "sa");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            sql = "insert into notpay values(" + c1 + ")";
            int n = stmt.executeUpdate(sql);
            if (n > 0) {
                JOptionPane.showMessageDialog(null, "增加成功");
            } else {
                JOptionPane.showMessageDialog(null, "增加失败");
            }
            rs.close();
            stmt.close();
            con.close();
        }
    } catch (ClassNotFoundException ex) {
    } catch (SQLException ex) {
    }

解决方案 »

  1.   

    1.你直接for (int i = 0; i < rows; i++) {
       c1 = (String) jTable1.getValueAt(i, 1);
       }
     这个不就只存最后一行的table值给c1了?这不是你想要的存所有吧。这是一个问题。2.while (rs.next()) {
       sql = "insert into notpay values(" + c1 + ")";
       int n = stmt.executeUpdate(sql);
       if (n > 0) {
       JOptionPane.showMessageDialog(null, "增加成功");
       } else {
       JOptionPane.showMessageDialog(null, "增加失败");
       }
       rs.close();
       stmt.close();
       con.close();
       }while循环一次,就关闭了?以为你关闭资源的code都在while内部。要不给完整code,否则不清楚你说点按钮没反应的具体原因。
     
      

  2.   

    用Swing做的,自动生成代码的,界面都是拉控件,不用监听的