无法将数据插入数据库

时间:2016-05-19 06:52:10

标签: mysql sql jdbc

这是事物

ps = conn.prepareStatement("INSERT INTO goods_country(goodsId,goodsCountry) VALUES (?,?)");
ps.setInt(1, this.returnId());
ps.setString(2, goodsCountry);
int exe = ps.executeUpdate();

我确定this.returnId()goodsCountry有价值, 运行此代码时出现此错误

  

java.sql.SQLException:参数索引超出范围(1>参数个数,为0)。       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)       在com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3321)       在com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3306)       在com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3345)       在com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3296)       在com.backend.model.MoviesBeanBO.inserCountry(MoviesBeanBO.java:67)       在com.backend.controller.MovieOnBoard.doPost(MovieOnBoard.java:71)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:646)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       at filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:25)

无法弄清楚发生了什么, 如果有人能提供帮助,我将非常感激。 THX!

1 个答案:

答案 0 :(得分:0)

尝试删除值和问号之间的空格

    ps = conn.prepareStatement("INSERT INTO goods_country(goodsId,goodsCountry) VALUES(?,?)");
    ps.setInt(1, this.returnId());
    ps.setString(2, goodsCountry);
    int exe = ps.executeUpdate();

或者需要检查驱动程序jar文件。