有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用NetBeans删除Oracle 11g数据库中的行

我试图删除数据库中的一行,该行与JTextField中的某个内容匹配。但是出现了一个异常,下面的代码没有告诉我原因。我能做什么

      try{Class.forName("oracle.jdbc.OracleDriver");

PreparedStatement pstmnt;
try (
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","123")) {

    pstmnt = conn.prepareStatement("delete from TEMP where matricRoll=?");

    pstmnt.setString(1, matricRoll.getText());
    pstmnt.executeUpdate();

            conn.close();
            pstmnt.close();
  GI.setVisible(true);
ED.setVisible(false);
addingToFrame();
settingBounds();  
} catch (SQLException ex) {JOptionPane.showMessageDialog(null,"error");}

        } catch (ClassNotFoundException ex) {
}

共 (2) 个答案

  1. # 1 楼答案

    试试这个:

    String deleteSQL = "DELETE DBUSER WHERE USER_ID = ?";
    
        try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(deleteSQL);
            preparedStatement.setInt(1, 1001);
    
            // execute delete SQL stetement
            preparedStatement.executeUpdate();
    
            System.out.println("Record is deleted!");
    
        } catch (SQLException e) {
    
            System.out.println(e.getMessage());
    
        } finally {
    
            if (preparedStatement != null) {
                preparedStatement.close();
            }
    
            if (dbConnection != null) {
                dbConnection.close();
            }
    
        }
    
  2. # 2 楼答案

    谢谢大家的关注。。我发现了错误。。我正在从一个无效的列中删除。。。 以下是解决方案

           try{
               Class.forName("oracle.jdbc.OracleDriver");
               Connection conn;
               PreparedStatement pstmnt;
           try (conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","123"))
     {
    
            pstmnt = conn.prepareStatement("delete from TEMP where MATRICRN=?");
            String roll=matricRoll.getText();
            int rollm=Integer.valueOf(roll);
    
            pstmnt.setInt(1, rollm);
            pstmnt.executeUpdate();
            JOptionPane.showMessageDialog(null,"Deleted");
                conn.close();
                pstmnt.close();
                GI.setVisible(true);
                ED.setVisible(false);
                addingToFrame();
                settingBounds();  
                    } 
        catch (SQLException ex) 
                {JOptionPane.showMessageDialog(null,"error"+ex);}
    
           } 
        catch (ClassNotFoundException exe) {
                     JOptionPane.showMessageDialog(null,exe);    }