1

新手程序员在这里。在执行 mvn tomcat:run 时,我收到以下错误:

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 = ?");

谷歌搜索 - 找不到(?,?)场景的解决方案。请。帮助 - 提前谢谢 :)

这是完整的代码(几乎) - 我做错了什么,但不知道是什么。

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:run 抛出以下内容 -

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

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '?,?)' 附近使用正确的语法

4

5 回答 5

2

使用此代码:

    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'))

希望这有意义吗?

于 2013-08-19T06:10:58.577 回答
1

我认为这是sql语法问题:

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

将其替换为

INSERT INTO xyz.user(user_name, user_password) values (?, ?);
于 2013-08-19T06:11:29.760 回答
1

更新中有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 = ?");
于 2013-08-19T06:13:03.663 回答
0

你的错误是这个陈述

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

它应该是

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

希望能帮助到你

干杯

于 2013-08-19T06:11:46.420 回答
0

尝试这个:-

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

代替

this.jdbcTemplate.update("INSERT INTO xyz.user(user_name, user_password values (?, ?)");
于 2013-08-19T06:12:13.640 回答