Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepared statement Parameter index out of range (0 < 1 )

Am not expert on web application, recently am writing a small webapp for my mobile app using servlets. My agenda is to receive data from client, and to put it into db. everything is running fine except, my database insert statement,which am trying to achieve using 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();

but while executing this task, am receiving following exception

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)

I seriously tried all sorts of thing which am capable of, but this exception is not going away, any idea on same?

Thanks, Techfist

like image 866
Techfist Avatar asked Dec 21 '14 18:12

Techfist


People also ask

What is parameter index out of range?

The error message means that the SQL query you specified doesn't accept any parameters. In order to specify parameters in your SQL query, you need to use question marks.

What is the index of the first parameter when using a prepared statement?

The first number (1) is the index of the parameter to insert the value for. The second number (123) is the value to insert into the SQL statement.


2 Answers

PreparedStatement parameter indices start at 1

preparedStmt.setString(1, data.getDeviceNumber());
...
like image 122
Reimeus Avatar answered Nov 15 '22 22:11

Reimeus


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 parameters are not zero-indexed. It starts with 1 , as all the indexes in database related statements start with 1.

like image 23
Panther Avatar answered Nov 15 '22 22:11

Panther