JDBC - 如何更新日期-1 处的记录

JDBC - How to update record where date -1

我有两个 table,其中有两列用于在 SQL 数据库中存储应用程序设置。我的问题是如何通过将 dCreateTime 值更改为负 1 天来更新记录加上有两个 table 调用 where userID = atest211。我的代码似乎不起作用。

我的代码:

public void getEmployeesFromDataBase() {
        try {
            String query = "update a set a.dCreateTime=a.dCreateTime-1 from tbet a, tuser b where a.iUserKey= b.iUserKey and b.sUserid = 'atest211' and a.dCreateTime> GETDATE()-5";
            statement = connection.createStatement();
            rs = statement.executeQuery(query);

            while (rs.next()) {
                int EmpId = rs.getInt("iUserKey");
                String EmpName = rs.getString("sUserid");
                System.out.println(EmpId + "\t" + EmpName + "\t");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

我更新的代码,它仍然不能正常工作

public void updateTableData() {
        PreparedStatement pstmt = null;
        try {
            pstmt = connection.prepareStatement("update a set a.dCreateTime=a.dCreateTime-1 from tbet a, tuser b where a.iUserKey= b.iUserKey and b.sUserid = 'atest211' and a.dCreateTime> GETDATE()-5");
            pstmt.setString(1, "dCreateTime");
            pstmt.setString(2, "atest211");
            // To execute update query.
            pstmt.executeUpdate();

            // Printing all records of user table after updating record.
            String query = "select * from tbet";
            // Get the contents of user table from DB
            ResultSet res = statement.executeQuery(query);
            // Print the result untill all the records are printed
            // res.next() returns true if there is any next record else returns
            // false
            while (res.next()) {
                System.out.println(String.format("%s - %s - %s - %s ", res.getString(1), res.getString(2),
                        res.getString(3), res.getString(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

您需要更新...加入..设置..其中语法:

       String query = "update a join b on a.iUserKey= b.iUserKey set a.dCreateTime=a.dCreateTime-1  where b.sUserid = 'atest211' and a.dCreateTime> GETDATE()-5";

接下来您必须使用 statement.execute(query); 而不是 executeQuery(query)。 该方法不 return 结果集....

您还应该阅读有关 SQL 注入和使用准备好的语句的信息。

您正在使用带有更新的 result set,这是不可能的。 此外,在您的 while 循环中,您基本上是从数据库中获取值,而不是更新它们。

如果你想更新值,我建议你使用PreparedStatement

如果您想让代码正常工作,请尝试在查询中使用 SELECT 而不是 UPDATE

 String query = "SELECT a.dCreateTime FROM tbet a, tuser b WHERE a.iUserKey= b.iUserKey and b.sUserid = 'atest211' and a.dCreateTime> GETDATE()-5";

编辑: 如果你想使用 PreparedStatement,试试这样的东西

// create the preparedstatement
PreparedStatement ps = conn.prepareStatement(
  "UPDATE yourTable SET title = ?, author = ? WHERE id = ?");

ps.setString(1,title);
ps.setString(2,author);
ps.setInt(3,id);

//execute the upate
ps.executeUpdate();
ps.close();

归功于@OldMcDonald,因为我修改了代码并且它正在运行,不幸的是在 select 语句期间出现一些错误,同时试图检索记录以检查记录是否已更新,遇到错误: java.lang.NullPointerException。它通过添加 statement = connection.createStatement();

解决了错误

代码如下:

public void updateTableData() {
            PreparedStatement pstmt = null;
            try { 
                //create calendar date -1 day
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1); 

                // preparing query to update record In db. 
                pstmt = connection.prepareStatement("update a set a.dCreateTime= ? from tbet a, tuser b where a.iUserKey= b.iUserKey and b.sUserid = ? and a.dCreateTime> GETDATE()-5");
                // Set name value which you wants to update. 
                pstmt.setDate(1, new java.sql.Date(cal.getTime().getTime()));
                // Set id of record which you wants to update.
                pstmt.setString(2, "atest211");
                // To execute update query.
                pstmt.executeUpdate();

                // Printing all records of user table after updating record.
                String query = "select * from tbet where iUserKey=53298 ORDER BY tbet.dCreateTime desc";
                statement = connection.createStatement();
                // Get the contents of user table from DB
                ResultSet res = statement.executeQuery(query);
                // Print the result untill all the records are printed
                // res.next() returns true if there is any next record else returns
                // false
                while (res.next()) {
                    System.out.println(String.format("%s - %s - %s - %s ", res.getString(1), res.getString(2),
                            res.getString(3), res.getString(4)));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }