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

时间:2017-07-18 12:43:04

标签: java oracle netbeans oracle11g

我正在尝试删除数据库中行与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 个答案:

答案 0 :(得分:1)

感谢所有您的注意..我发现了错误..我正在从无效栏中删除... 这是解决方案......

       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);    }

答案 1 :(得分:0)

试试这个:

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();
        }

    }