MySQL的/ Java错误(MySQL/Java error)

2019-09-28 03:59发布

新手程序员在这里。 一旦这样的tomcat MVN:运行出现以下错误:

SEVERE: Servlet.service() for servlet appServlet threw exception
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL  syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values (?, ?)' at line 1

有问题的代码如下:

public void create(User user) {
    this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password values (?, ?)");


            user.getUserName(); user.getId();

}

public void delete(User user) {
    this.jdbcTemplate.update("DELETE FROM xyz.user WHERE id = ?");


}

public void update(User user) {
    this.jdbcTemplate.update(
            "UPDATE xyz.user SET UserName = ? password = ? WHERE id = ?");

Google搜索 - 无法找到一个解决方案场景(?,?)。 PLS。 帮助 - THX提前:)

下面是完整的代码(几乎) - 我做错了什么,但无法弄清楚什么。

public User find(String login) {
  System.out.println("Trying to find the user...." + login);    
  User user = this.jdbcTemplate.queryForObject(
        "select * from xyz where user_name = ?",
        new Object[]{login},
        new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(Long.valueOf(rs.getInt(1)));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                return user;
            }
        });
  System.out.println("Found user..." + user);
   return user;
}

public void create(User user) {

    this.jdbcTemplate.update("INSERT INTO ibstechc_dev.user(user_name, user_password) VALUES (?,?)");

            user.getUserName(); user.getId() ;
}


public void delete(User user) {
    this.jdbcTemplate.update("DELETE FROM xyz WHERE id = ?");
    // TODO Auto-generated method stub

}

public void update(User user) {
    this.jdbcTemplate.update(
            "UPDATE xyz SET user_name = ?, user_password = ? WHERE id = ?");
    // TODO Auto-generated method stub

}

}

我坚持同样的错误 - tomcat的:运行引发以下 -

SEVERE: Servlet.service() for servlet appServlet threw exception

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法错误; 检查对应于你的MySQL服务器版本使用附近的正确语法手册“?,?)”在1号线

Answer 1:

使用此代码:

    this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password) values (?, ?)");

有一个与你的SQL语句的问题。 为了给你试图做透视:

INSERT INTO xyz.user(user_name, user_password values ('testuser','testpass'))

代替

INSERT INTO xyz.user(user_name, user_password) values ('testuser','testpass'))

希望这是有道理的?



Answer 2:

我觉得这里是SQL语法问题:

INSERT INTO xyz.user(user_name, user_password values (?, ?)

通过替换此

INSERT INTO xyz.user(user_name, user_password) values (?, ?);


Answer 3:

有一个在更新SQL语法错误

public void update(User user) {
    this.jdbcTemplate.update(
            "UPDATE xyz.user SET UserName = ? password = ? WHERE id = ?");

这是不是这样的更新,给一个逗号的方式(,)

public void update(User user) {
    this.jdbcTemplate.update(
            "UPDATE xyz.user SET UserName = ? ,password = ? WHERE id = ?");


Answer 4:

你的错误是这样的语句

this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password values (?, ?)");

它应该是

this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password) values (?, ?)");

希望能帮助到你

干杯



Answer 5:

尝试这个:-

this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password) values (?, ?)");

代替

this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password values (?, ?)");


文章来源: MySQL/Java error
标签: java mysql jdbc