【发布时间】:2022-01-19 09:19:22
【问题描述】:
所以我一直在尝试在我的 spring mvc 项目中实现一个 sql 查询来将数据插入到数据库中。该查询在 mysql 工作台中运行。但是当我在 Spring 项目的 DAO 层中编写相同的查询时,它给了我一个错误。 我得到的错误如下
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar
[insert into question (question_id,questions,question_text,user_id) SELECT(?,?,?,?) from user 'where user_id ='2]; nested exception is java.sql.SQLSyntaxErrorException: 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 ''where user_id ='2' at line 1
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:960)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1015)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1025)
at qnaapp.dao.UserDaoImpl.question(UserDaoImpl.java:44)
at qnaapp.service.UserServiceImpl.question(UserServiceImpl.java:27)
at qnaapp.UserServiceTest1.testQuestion(UserServiceTest1.java:45)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
这里是建表查询文本文件的链接 https://drive.google.com/file/d/1C966vW9GNs9MFtEheCr2dhs8rztAROaw/view?usp=sharing
这是DAO层的部分代码
public int question(Question question) {
String sql = "insert into question (question_id,questions,question_text,user_id)
SELECT(?,?,?,?) from user "
+ "'where user_id ='" +question.getUser_id();
return jdbcTemplate.update(sql, new Object[] { question.getQuestion_id(),question.getQuestions(), question.getQuestion_text(), question.getUser_id()});
}
编辑: 这里是我用来测试该查询的junit测试类 打包qnaapp;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import qnaapp.model.Login;
import qnaapp.model.User;
import qnaapp.model.Question;
import qnaapp.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:qna/config/user-beans.xml" })
public class UserServiceTest1 {
@Autowired
private UserService userService;
@Test
public void testValidateUser() {
Login login = new Login();
login.setUsername("krishnadubey");
login.setPassword("123456789");
User user = userService.validateUser(login);
Assert.assertEquals("Krishna", user.getFirstname());
}
@Test
public void testQuestion() {
//User user = new User();
Question question = new Question();
question.setQuestion_id(4);
question.setQuestions("Who is founder of c?");
question.setQuestion_text("Please Explain");
question.setUser_id(2);
int result = userService.question(question);
Assert.assertEquals(1, result);
}
}
请为这些问题提出一些解决方案。
【问题讨论】:
-
SELECT(?,?,?,?) from user 'where user_id ='2似乎不是有效的 SQL 合成器 -
嘿@fantaghirocco 所以问号是我要插入需要插入的值的位置。我的sql查询是这样的-插入问题(question_id,questions,question_text,user_id)选择1作为question_id,“什么是java”作为问题,“请详细解释”作为question_text,user_id FROM user where user_id = 3;你可以在驱动文件中看到它。
-
穆雷尼克的回答解释了它的问题