准备语句参数索引超出范围(0 <1)

时间:2014-12-21 18:36:22

标签: java jdbc prepared-statement

我不是网络应用专家,最近我正在使用servlet为我的移动应用编写一个小型webapp。我的议程是从客户端接收数据,并将其放入数据库。一切都运行正常,除了我的数据库插入语句,它正在尝试使用PreparedStatement实现。

// This is how my insert statement looks like
private String INSERT_INTO_DEVICES = "INSERT INTO travlemate.devices (owner,"
        + "deviceid,gcmid) VALUES (?,?,?)"; 

// This is how am trying to insert
PreparedStatement preparedStmt = mConn.prepareStatement(INSERT_INTO_DEVICES);
preparedStmt.setString(0, data.getDeviceNumber());
preparedStmt.setString(1, data.getDeviceID());
preparedStmt.setString(2,data.getGcmId());
preparedStmt.execute();
mConn.close();

但在执行此任务时,我收到以下异常

java.sql.SQLException: Parameter index out of range (0 < 1 ).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2791)
    at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3627)
    at com.mkgcorp.travelweb.data.DataManager.registerGCMData(DataManager.java:35)
    at com.mkgcorp.travelweb.controller.Loader.doPost(Loader.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我认真尝试了各种各样的能力,但是这个例外不会消失,有什么想法吗?

谢谢, Techfist

2 个答案:

答案 0 :(得分:30)

PreparedStatement参数索引从1

开始
preparedStmt.setString(1, data.getDeviceNumber());
...

答案 1 :(得分:5)

PreparedStatement preparedStmt = mConn.prepareStatement(INSERT_INTO_DEVICES);
preparedStmt.setString(1, data.getDeviceNumber());
preparedStmt.setString(2, data.getDeviceID());
preparedStmt.setString(3,data.getGcmId());
preparedStmt.execute();
mConn.close();

Jdbc参数不是零索引。它从1开始,因为数据库相关语句中的所有索引都以1开头。