Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void update(int userId, String date, String column, String value){
- try {
- // convert date from String to Date
- DateTime dt = DateTime.parse(date);
- java.sql.Date sqlDate = new java.sql.Date(dt.getMillis());
- // create prepared statement
- conn = DriverManager.getConnection("jdbc:mysql://localhost/db","root", "");
- String query = "UPDATE expenses_income SET ? = ? WHERE userId = ? AND date = CAST(? AS DATETIME);";
- PreparedStatement preparedStmt = conn.prepareStatement(query);
- preparedStmt.setString(1, column);
- preparedStmt.setString(2, value);
- preparedStmt.setInt(3, userId);
- preparedStmt.setDate(4, sqlDate);
- preparedStmt.executeUpdate();
- conn.close();
- } catch (Exception e) {
- System.err.println("MySQL exception: " + e);
- }
- }
- 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
- String query = "UPDATE expenses_income SET ? = ? WHERE userId = ? AND date = ?;";
Add Comment
Please, Sign In to add comment