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

时间:2016-07-11 09:19:26

标签: java sql jdbc sql-update prepared-statement

我有两个表,有两列用于在SQL数据库中存储应用程序设置。我的问题是如何更新记录,将dCreateTime值更改为减1天加上有两个表调用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();
        }

3 个答案:

答案 0 :(得分:2)

您正在使用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();

答案 1 :(得分:1)

你需要更新... join .. set .. where语法:

       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)。 该方法不返回结果集....

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

答案 2 :(得分:1)

归功于@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();
            }