Guest User

Untitled

a guest
Jan 14th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. public void update(int userId, String date, String column, String value){
  2. try {
  3. // convert date from String to Date
  4. DateTime dt = DateTime.parse(date);
  5. java.sql.Date sqlDate = new java.sql.Date(dt.getMillis());
  6. // create prepared statement
  7. conn = DriverManager.getConnection("jdbc:mysql://localhost/db","root", "");
  8. String query = "UPDATE expenses_income SET ? = ? WHERE userId = ? AND date = CAST(? AS DATETIME);";
  9. PreparedStatement preparedStmt = conn.prepareStatement(query);
  10. preparedStmt.setString(1, column);
  11. preparedStmt.setString(2, value);
  12. preparedStmt.setInt(3, userId);
  13. preparedStmt.setDate(4, sqlDate);
  14. preparedStmt.executeUpdate();
  15.  
  16. conn.close();
  17. } catch (Exception e) {
  18. System.err.println("MySQL exception: " + e);
  19. }
  20. }
  21.  
  22. MySQL exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''comment' = '123' WHERE userId = 1 AND date = CAST('2018-01-06' AS DATETIME)' at line 1
  23.  
  24. String query = "UPDATE expenses_income SET ? = ? WHERE userId = ? AND date = ?;";
Add Comment
Please, Sign In to add comment