我是Spring框架的新手,我正在接受培訓。正如我的TL給出的,我試圖通過在控制器中使用以下方法來完成將數據插入到數據庫表中的任務。請求處理失敗;嵌套異常是java.sql.SQLException:不能用executeQuery發出數據操作語句()
Create a controller. (UserController.java)
it should have two methods submit() and showAllUsers()
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String submit(@Valid @ModelAttribute("user")User user, BindingResult result, ModelMap model) {
}
@RequestMapping(value = "/users", method = RequestMethod.GET)
public ModelAndView showAllUsers() {
// ...
}
我完成了代碼,但我收到以下錯誤:
Request processing failed; nested exception is java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
我UserDaoImpl
代碼:
package com.baylogic.peoplecentral.dao.impl;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import com.baylogic.peoplecentral.dao.UserDao;
import com.baylogic.peoplecentral.pojo.User;
public class UserDaoImpl implements UserDao {
DataSource dataSource;
public DataSource getDataSource() {
return this.dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean createUser(String firstname, String lastname, int id, String email) throws SQLException {
String query = "insert into user(firstname,lastname,id,email) values(?,?,?,?)";
PreparedStatement pstmt = dataSource.getConnection().prepareStatement(query);
pstmt.setString(1, firstname);
pstmt.setString(2, lastname);
pstmt.setInt(3, id);
pstmt.setString(4, email);
ResultSet resultSet = pstmt.executeQuery();
if (resultSet.next())
return (resultSet.getInt(1)>0);
else
return false;
}
}
我卡住,無法向前移動這個任務。你能幫我解決這個問題嗎?