JDBC Batch Bug ?

249 views
Skip to first unread message

tqb

unread,
Sep 28, 2008, 4:30:13 AM9/28/08
to H2 Database
Hi,All

public void initialization() throws Exception {
String strSeq = "create sequence testSeq;";
String strSql = "create table t_test (id INTEGER not null,name
VARCHAR(10) not null);";
Connection con = newConnection(); //"jdbc:h2:mem:test", "sa", ""
if (null == con) {
throw new Exception("Connection Error");
}
PreparedStatement ps = con.prepareStatement(strSeq);
ps.addBatch(strSql);
ps.executeBatch();
closeStatement(ps);
closeConnection(con);
}

After running out as follows abnormal:

Exception in thread "main" org.h2.jdbc.JdbcSQLException: This method
is not allowed for a prepared statement; use a regular statement
instead. [90130-63]
at org.h2.message.Message.getSQLException(Message.java:89)
at org.h2.message.Message.getSQLException(Message.java:93)
at org.h2.message.Message.getSQLException(Message.java:71)
at org.h2.message.Message.getSQLException(Message.java:114)
at
org.h2.jdbc.JdbcPreparedStatement.addBatch(JdbcPreparedStatement.java:
214)
at cn.tqb.db.h2.CreateScheam.initialization(CreateScheam.java:18)
at cn.tqb.db.h2.CreateScheam.main(CreateScheam.java:25)

H2 Version: 1.0.79 (2008-09-26)

the strSeq and strSql merge together, there is no problem
String strCommand="create sequence testSeq;create table t_test (id
INTEGER not null,name VARCHAR(10) not null);";

Why?
Is H2 does not support it?



Thomas Mueller

unread,
Sep 28, 2008, 5:53:15 AM9/28/08
to h2-da...@googlegroups.com
Hi,

> PreparedStatement ps = con.prepareStatement(strSeq);
> ps.addBatch(strSql);
> ps.executeBatch();

As the exception says, PreparedStatement.addBatch(String sql) is not
allowed. What is allowed is: Statement.addBatch(String sql). This is
defined by the JDBC specification. It is not a problem of H2. See
below how to fix your application.

> Exception in thread "main" org.h2.jdbc.JdbcSQLException: This method
> is not allowed for a prepared statement; use a regular statement
> instead. [90130-63]

You are using version 1.0.63, not 1.0.79 as you wrote. The error code
includes the version (xx-63 means version 1.0.63). Probably an old
h2.jar is in the classpath.

Regards,
Thomas

public static void main(String[] a) throws Exception {


String strSeq = "create sequence testSeq;";
String strSql = "create table t_test (id INTEGER not null,name
VARCHAR(10) not null);";

org.h2.Driver.load();
Connection conn =
DriverManager.getConnection("jdbc:h2:mem:test", "sa", "");
Statement stat = conn.createStatement();
stat.addBatch(strSeq);
stat.addBatch(strSql);
stat.executeBatch();
stat.close();
conn.close();
}

Reply all
Reply to author
Forward
0 new messages