Hi!

I use h2database engine from maven

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.174</version>
</dependency>

, I use java
 
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

>From Test I see output

commited value = 1
id=1
commited value = 2
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Параметр "#1" не 
установлен
Parameter "#1" is not set; SQL statement:
  ? = call IDENTITY()  [90012-174]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:332)
at org.h2.message.DbException.get(DbException.java:172)
at org.h2.message.DbException.get(DbException.java:149)
at org.h2.expression.Parameter.checkSet(Parameter.java:80)
at org.h2.command.Prepared.checkParameters(Prepared.java:163)
at org.h2.command.CommandContainer.query(CommandContainer.java:90)
at org.h2.command.Command.executeQuery(Command.java:196)
at org.h2.jdbc.JdbcPreparedStatement.execute(JdbcPreparedStatement.java:189)
at Test.main(Test.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

but from Test2 I see "normal" output

commited value = 1
id=1
commited value = 2
id=2


It's a little less than optimal code, but it is - the correct code.

Perhaps some error in h2?

WBR,
Kirill Temnenkov

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.
import org.h2.jdbcx.JdbcConnectionPool;

import java.sql.*;

import static jabot.Helper.checkNotNull;

public class Test {
    public static void main(String[] args) throws SQLException {
        JdbcConnectionPool cp = JdbcConnectionPool.create(
                "jdbc:h2:mem:test", "sa", "sa");
        try (Connection conn = cp.getConnection()) {

            try (Statement statement = checkNotNull(conn).createStatement()) {
                statement.execute(checkNotNull("CREATE TABLE LOGDATA\n" +
                        "(\n" +
                        "    ID IDENTITY PRIMARY KEY NOT NULL,\n" +
                        "    NICK VARCHAR(200) NOT NULL\n" +
                        ");\n"));
            }

        }

        for (int i = 1; i < 3; ++i) {
            try (Connection conn = cp.getConnection()){
                conn.setAutoCommit(false);


                String value = "" + i;

                try(PreparedStatement ps = conn.prepareStatement("INSERT INTO LOGDATA ( NICK )\n" +
                        "VALUES (?)")){
                    ps.setString(1, value);
                    ps.execute();
                    conn.commit();
                    System.out.println("commited value = " + value);
                }

                try(CallableStatement cs = conn.prepareCall("{ ? = call IDENTITY()}")){
                    cs.registerOutParameter(1, Types.BIGINT);
                    cs.execute();
                    long id = cs.getLong(1);
                    System.out.println("id=" + id);

                }

            }
        }



    }
}
import org.h2.jdbcx.JdbcConnectionPool;

import java.sql.*;

import static jabot.Helper.checkNotNull;

public class Test2 {
    public static void main(String[] args) throws SQLException {
        JdbcConnectionPool cp = JdbcConnectionPool.create(
                "jdbc:h2:mem:test", "sa", "sa");
        try (Connection conn = cp.getConnection()) {

            try (Statement statement = checkNotNull(conn).createStatement()) {
                statement.execute(checkNotNull("CREATE TABLE LOGDATA\n" +
                        "(\n" +
                        "    ID IDENTITY PRIMARY KEY NOT NULL,\n" +
                        "    NICK VARCHAR(200) NOT NULL\n" +
                        ");\n"));
            }

        }

        for (int i = 1; i < 3; ++i) {
            try (Connection conn = cp.getConnection()) {
                conn.setAutoCommit(false);


                String value = "" + i;

                try (PreparedStatement ps = conn.prepareStatement("INSERT INTO LOGDATA ( NICK )\n" +
                        "VALUES (?)")) {
                    ps.setString(1, value);
                    ps.execute();
                    conn.commit();
                    System.out.println("commited value = " + value);
                }

                CallableStatement cs = conn.prepareCall("{ ? = call IDENTITY()}");
                cs.registerOutParameter(1, Types.BIGINT);
                cs.execute();
                long id = cs.getLong(1);
                System.out.println("id=" + id);

            }
        }


    }
}

Reply via email to