Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JDBC prepared statement maximum parameter markers

Im building a large database call using PreparedStatement that has 2000+ parameter markers.

Im getting this error

Caused by: java.sql.SQLException: Prepared or callable statement has more than 2000 parameter markers.
    at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:1139)
    at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:156)
    at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.<init>(JtdsPreparedStatement.java:107)
Caused by: java.sql.SQLException: Prepared or callable statement has more than 2000 parameter markers. 

I tried searching the API Docs and google but couldnt find how to configure this.

Does anyone know if it is possible to up this limit? I am aware it is going to be a slow database call but that is fine for now.

Also will this cause me any issues in the long run, would I be better off running it in batches?

like image 330
cowls Avatar asked Jan 31 '13 17:01

cowls


People also ask

Is PreparedStatement is less efficient than statement?

Statement is used for static queries like DDLs i.e. create,drop,alter and prepareStatement is used for dynamic queries i.e. DML query. In Statement, the query is not precompiled while in prepareStatement query is precompiled, because of this prepareStatement is time efficient.

Which of the following is true about PreparedStatement in JDBC?

Q 5 - Which of the following is correct about PreparedStatement? A - PreparedStatement allows mapping different requests with same prepared statement but different arguments to execute the same execution plan.

Which statement of JDBC should be used to set the values for the parameters for SQL dynamically?

Set parameters dynamically to prepared Statement in JDBC.


1 Answers

Seams like you're stuck at 2000. Here is a cut out from the driver source.

if (params != null && params.size() > 255
     && connection.getPrepareSql() != TdsCore.UNPREPARED
     && procName != null) {
  int limit = 255; // SQL 6.5 and Sybase < 12.50
  if (connection.getServerType() == Driver.SYBASE) {
    if (connection.getDatabaseMajorVersion() > 12 ||
        connection.getDatabaseMajorVersion() == 12 &&
        connection.getDatabaseMinorVersion() >= 50) {
      limit = 2000; // Actually 2048 but allow some head room
    }
  } else {
    if (connection.getDatabaseMajorVersion() == 7) {
      limit = 1000; // Actually 1024
    } else if (connection.getDatabaseMajorVersion() > 7) {
      limit = 2000; // Actually 2100
    }
  }
  if (params.size() > limit) {
   throw new SQLException(
       Messages.get("error.parsesql.toomanyparams",
       Integer.toString(limit)),
       "22025");
  }
}

Here are a blog with examples on how to solve it.

like image 108
Stefan Rasmusson Avatar answered Oct 15 '22 23:10

Stefan Rasmusson