gpt4 book ai didi

java - MySQL/Java 错误

转载 作者:行者123 更新时间:2023-11-29 04:46:24 24 4
gpt4 key购买 nike

这里是新手程序员。执行 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 = ?");

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

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

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 行的 '?,?)' 附近使用的正确语法

最佳答案

使用这段代码:

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

希望这是有道理的?

关于java - MySQL/Java 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18307358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com