使用JTable更新数据库时的NumberFormatException

Jar*_*vis 1 java swing jtable

我似乎无法用我的JTable中给出的值更新我的数据库.

我的代码:

public void Update() throws SQLException 
{
    for (int a = 0; a < jTable1.getRowCount(); a++) {
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//127.0.0.1:1521/xe","IND","pass");
        Statement stmt = con.createStatement();
        String query4 = "insert into MONTH_inv VALUES (?,?,?,?,?)";
        String Category = jTable1.getValueAt(a, 0).toString();  
        String Item = jTable1.getValueAt(a, 1).toString();
        int Quantity = Integer.parseInt((String) jTable1.getValueAt(a, 2));
        int avg = Integer.parseInt((String) jTable1.getValueAt(a, 3));
        int ground = Integer.parseInt((String) jTable1.getValueAt(a, 4));

        PreparedStatement pstmt = con.prepareStatement(query4);
        pstmt.setString(1, Category);
        pstmt.setString(2, Item);
        pstmt.setInt(3, Quantity);
        pstmt.setInt(4, avg);
        pstmt.setInt(5, ground);
        pstmt.executeUpdate();
    }    
} 
Run Code Online (Sandbox Code Playgroud)

一切都运行正常,但当我点击我的GUI上的UPDATE按钮时,我收到错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 5

显然,您尝试解析为int的其中一个字符串为null.

您应该检查jTable1.getValueAt(a,2), jTable1.getValueAt(a,3)jTable1.getValueAt(a,4)确保它们不为空.