使用Java在SQL中删除语句

时间:2016-03-08 09:08:51

标签: sql oracle

这是我第一次使用java实现Web App和DB连接。 我尝试编写从用户获取值的方法,并根据该值删除记录。

**public void DeleteTask(String tid) {

    try {
        String deletesql = ("DELETE FROM Tasks" + " WHERE Task_Id = " + tid + ";");

        stmt = con.prepareStatement(deletesql);
        stmt.setString(1,"tid");
        int rowsDeleted = stmt.executeUpdate();
        if (rowsDeleted > 0) {
            System.out.println("Record is deleted successfully!");
        }
    } catch (SQLException e) {
        System.out.print("SQL Exception " + e);
        e.printStackTrace();
        System.exit(1);
    }
}**

输出为: 连接的。 SQL异常java.sql.SQLException:ORA-01722:无效的数字 java.sql.SQLException:ORA-01722:无效的数字

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1010)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1062)
at gov.adjd.dao.TasksDao.DeleteTask(TasksDao.java:112)
at gov.adjd.dao.TasksDao.main(TasksDao.java:132)

提前谢谢你,

1 个答案:

答案 0 :(得分:0)

public void DeleteTask(String tid) {

    try {
        String deletesql = "DELETE FROM Tasks WHERE Task_Id = ? ";

        stmt = con.prepareStatement(deletesql);
        stmt.setString(1, tid);
        stmt.executeUpdate();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

确保您在编程前学习,请参阅: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html