This is my test case but something is wrong and no records are returned from my PreparedStatement. What's wrong?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.util.JdbcUtils;
public class TestSelect {
public static void main(String[] args) throws Exception {
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "sa");
stmt = conn.createStatement();
stmt.execute("create temp table mytest(pk identity, col varchar(20))");
stmt.execute("insert into mytest (col) values('1')");
stmt.execute("insert into mytest (col) values('2')");
stmt.execute("insert into mytest (col) values('3')");
//this doesn't work, no records returned
pstmt = conn.prepareStatement("select pk from mytest where col in (?)");
pstmt.setObject(1, new Object[] { "1", "2" });
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("pk " + rs.getLong(1));
}
JdbcUtils.closeSilently(rs);
JdbcUtils.closeSilently(pstmt);
//this works
pstmt = conn.prepareStatement("select pk from mytest where col in ('1','2')");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("pk " + rs.getLong(1));
}
} finally {
JdbcUtils.closeSilently(rs);
JdbcUtils.closeSilently(pstmt);
JdbcUtils.closeSilently(conn);
}
}
}