>Hi
>Which version are you using? This is working correctly for me.
>Regards, Noel
Hi Noel,
I was on 168, but testing with 173 gives the same error. Below is a test
class and the output I get when I run it.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class H2CacheExample {
public static void main(String[] args) {
try {
Class.forName("org.h2.Driver");
}
catch ( Exception e) {
e.printStackTrace();
return;
}
doItWithUrl("jdbc:h2:~/h2cacheexample;QUERY_CACHE_SIZE=0");
doItWithUrl("jdbc:h2:~/h2cacheexample");
}
/**
* Connect using the supplied URL, then run the test
* @param url the url
*/
private static void doItWithUrl(String url) {
System.out.println("Connecting to database with url: " + url);
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
runTest(conn);
System.out.println("Test complete OK");
}
catch ( Exception e) {
System.out.println("Test threw exception");
e.printStackTrace();
}
finally {
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
/**
* Run the test with the supplied connection
*
* @param conn the connection to use
*/
private static void runTest(Connection conn) throws SQLException {
/*
* This throws SQL exception as table doesn't exist
*/
System.out.println("Before table created...");
prepareStatement(conn, "SELECT * FROM TEST", true);
/*
* Create the table
*/
System.out.println("Creating table...");
executeUpdate(conn, "CREATE TABLE TEST(col1 bigint, col2 varchar(255))");
/*
* Now prepare statement works correctly as the table exists
*/
System.out.println("After table created...");
prepareStatement(conn, "SELECT * FROM TEST", false);
/*
* Delete the table
*/
System.out.println("Deleting table...");
executeUpdate(conn, "DROP TABLE TEST");
/*
* Prepare statement on now deleted table. Should give an exception as the
table is gone
*/
System.out.println("After table deleted...");
prepareStatement(conn, "SELECT * FROM TEST", true);
}
private static void prepareStatement(Connection conn, String sql, boolean
exceptionExpected) throws SQLException {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
if ( exceptionExpected ) {
throw new RuntimeException("Failed to catch expected exception: sql=" +
sql);
}
} catch (SQLException e) {
if ( exceptionExpected )
System.out.println("Caught expected exception: " + e.getMessage());
else
throw e;
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException se2) {
System.out.println("Caught exception closing ps");
se2.printStackTrace();
}
}
}
private static void executeUpdate(Connection conn, String sql) throws
SQLException {
Statement stmnt = null;
try {
stmnt = conn.createStatement();
stmnt.executeUpdate(sql);
} finally {
try {
if (stmnt != null)
stmnt.close();
} catch (SQLException se2) {
System.out.println("Caught exception closing stmnt");
se2.printStackTrace();
}
}
}
}
Here's the output showing an expected exception after dropping the table
with QUERY_CACHE_SIZE=0, but no exception if QUERY_CACHE_SIZE is defaulted
(I think to 8?).
C:\Users\...\bin>java -classpath .;.\h2-1.3.173\h2-1.3.173.jar
H2CacheExample
Connecting to database with url: jdbc:h2:~/h2cacheexample;QUERY_CACHE_SIZE=0
Before table created...
Caught expected exception: Table "TEST" not found; SQL statement:
SELECT * FROM TEST [42102-173]
Creating table...
After table created...
Deleting table...
After table deleted...
Caught expected exception: Table "TEST" not found; SQL statement:
SELECT * FROM TEST [42102-173]
Test complete OK
Connecting to database with url: jdbc:h2:~/h2cacheexample
Before table created...
Caught expected exception: Table "TEST" not found; SQL statement:
SELECT * FROM TEST [42102-173]
Creating table...
After table created...
Deleting table...
After table deleted...
Test threw exception
java.lang.RuntimeException: Failed to catch expected exception: sql=SELECT
* FROM TEST
at H2CacheExample.prepareStatement(H2CacheExample.java:92)
at H2CacheExample.runTest(H2CacheExample.java:82)
at H2CacheExample.doItWithUrl(H2CacheExample.java:29)
at H2CacheExample.main(H2CacheExample.java:15)
Cheers,
Ian.
--
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.