i've built h2 in memory database short term storage. connect database short java stub , test make sure have connected successfully. since it's quite short, i've included code in full. i've used both .isclosed , isvalid(0). closed always comes in negative (meaning not closed) , isvalid(0) answers in positive (meaning not closed). both behave way whether use valid credentials or bogus
public class h2seewhaticabbreak { public static connection getconnection() { try { class.forname("org.h2.driver"); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); system.exit(-1); } connection conn = null; properties connectionprops = new properties(); connectionprops.put("user", "<username>"); connectionprops.put("password", "<password>"); try { conn = drivermanager.getconnection("jdbc:h2:~/tickets"); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); system.exit(-1); } return conn; } public static void main(string[] args) throws sqlexception { connection dbctx = getconnection(); if (dbctx == null ) { system.err.println("got null, bye!!"); system.exit(-1); } if (dbctx.isclosed()) { system.err.println("there's rotten in river city"); } else { system.err.println("got connection"); } system.err.println("looks did it??"); } }
any ideas @ all?
you create , set connectionprops
, don't use it. code equivalent following. please note default username , password h2 empty string.
conn = drivermanager.getconnection("jdbc:h2:~/tickets");
or
conn = drivermanager.getconnection("jdbc:h2:~/tickets", "", "");
or
conn = drivermanager.getconnection("jdbc:h2:~/tickets", null);
or
conn = drivermanager.getconnection("jdbc:h2:~/tickets", new properties());
what want either:
conn = drivermanager.getconnection("jdbc:h2:~/tickets", "<username>", "<password>");
or (more complicated):
properties connectionprops = new properties(); connectionprops.put("user", "<username>"); connectionprops.put("password", "<password>"); conn = drivermanager.getconnection("jdbc:h2:~/tickets", connectionprops);