提交 11eed7a3 authored 作者: Thomas Mueller's avatar Thomas Mueller

Avoid using DriverManager

上级 4cbda601
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
package org.h2.test.db; package org.h2.test.db;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -58,23 +57,23 @@ public class TestCluster extends TestBase { ...@@ -58,23 +57,23 @@ public class TestCluster extends TestBase {
Connection conn; Connection conn;
Statement stat; Statement stat;
String url1 = "jdbc:h2:tcp://localhost:" + port1 + "/test"; String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", false);
Server n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start(); Server n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
conn = DriverManager.getConnection(url1, user, password); conn = getConnection(url1, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table t1(id int, name clob)"); stat.execute("create table t1(id int, name clob)");
stat.execute("insert into t1 values(1, repeat('Hello', 50))"); stat.execute("insert into t1 values(1, repeat('Hello', 50))");
conn.close(); conn.close();
String url2 = "jdbc:h2:tcp://localhost:" + port2 + "/test"; String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", false);
Server n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2 , "-baseDir", getBaseDir() + "/node2").start(); Server n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2 , "-baseDir", getBaseDir() + "/node2").start();
String urlCluster = "jdbc:h2:tcp://" + serverList + "/test"; String urlCluster = "jdbc:h2:tcp://" + serverList + "/test";
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList",
serverList); serverList);
conn = DriverManager.getConnection(urlCluster, user, password); conn = getConnection(urlCluster, user, password);
conn.close(); conn.close();
n1.stop(); n1.stop();
...@@ -108,7 +107,7 @@ public class TestCluster extends TestBase { ...@@ -108,7 +107,7 @@ public class TestCluster extends TestBase {
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList",
serverList); serverList);
conn = DriverManager.getConnection(urlCluster, user, password); conn = getConnection(urlCluster, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table t1(id int, name varchar(30))"); stat.execute("create table t1(id int, name varchar(30))");
stat.execute("insert into t1 values(1, 'a'), (2, 'b'), (3, 'c')"); stat.execute("insert into t1 values(1, 'a'), (2, 'b'), (3, 'c')");
...@@ -130,7 +129,7 @@ public class TestCluster extends TestBase { ...@@ -130,7 +129,7 @@ public class TestCluster extends TestBase {
conn.close(); conn.close();
conn = DriverManager.getConnection(urlCluster, user, password); conn = getConnection(urlCluster, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
rs = stat.executeQuery("select count(*) from t1"); rs = stat.executeQuery("select count(*) from t1");
rs.next(); rs.next();
...@@ -166,7 +165,7 @@ public class TestCluster extends TestBase { ...@@ -166,7 +165,7 @@ public class TestCluster extends TestBase {
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList",
serverList); serverList);
conn = DriverManager.getConnection(urlCluster, user, password); conn = getConnection(urlCluster, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
assertTrue(conn.getAutoCommit()); assertTrue(conn.getAutoCommit());
stat.execute("create table test(id int, name varchar)"); stat.execute("create table test(id int, name varchar)");
...@@ -211,7 +210,7 @@ public class TestCluster extends TestBase { ...@@ -211,7 +210,7 @@ public class TestCluster extends TestBase {
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList",
serverList); serverList);
conn = DriverManager.getConnection(urlCluster, user, password); conn = getConnection(urlCluster, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
assertTrue(conn.getAutoCommit()); assertTrue(conn.getAutoCommit());
stat.execute("create table test(name int)"); stat.execute("create table test(name int)");
...@@ -230,7 +229,7 @@ public class TestCluster extends TestBase { ...@@ -230,7 +229,7 @@ public class TestCluster extends TestBase {
// stop server 2, and test if only one server is available // stop server 2, and test if only one server is available
n2.stop(); n2.stop();
conn = DriverManager.getConnection(urlCluster, user, password); conn = getConnection(urlCluster, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
rs = stat.executeQuery("select * from test"); rs = stat.executeQuery("select * from test");
assertTrue(rs.next()); assertTrue(rs.next());
...@@ -252,14 +251,14 @@ public class TestCluster extends TestBase { ...@@ -252,14 +251,14 @@ public class TestCluster extends TestBase {
String user = getUser(), password = getPassword(); String user = getUser(), password = getPassword();
Connection conn; Connection conn;
Statement stat; Statement stat;
String url1 = "jdbc:h2:tcp://localhost:" + port1 + "/test"; String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", false);
String url2 = "jdbc:h2:tcp://localhost:" + port2 + "/test"; String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", false);
String urlCluster = "jdbc:h2:tcp://" + serverList + "/test"; String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", false);
int len = 10; int len = 10;
// initialize the database // initialize the database
Server n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start(); Server n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
conn = DriverManager.getConnection(url1, user, password); conn = getConnection(url1, user, password);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar) as " + stat.execute("create table test(id int primary key, name varchar) as " +
"select x, 'Data' || x from system_range(0, " + (len - 1) + ")"); "select x, 'Data' || x from system_range(0, " + (len - 1) + ")");
...@@ -279,7 +278,7 @@ public class TestCluster extends TestBase { ...@@ -279,7 +278,7 @@ public class TestCluster extends TestBase {
JdbcUtils.closeSilently(conn); JdbcUtils.closeSilently(conn);
// test the cluster connection // test the cluster connection
Connection connApp = DriverManager.getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user, password); Connection connApp = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user, password);
check(connApp, len, "'" + serverList + "'"); check(connApp, len, "'" + serverList + "'");
// delete the rows, but don't commit // delete the rows, but don't commit
...@@ -306,18 +305,18 @@ public class TestCluster extends TestBase { ...@@ -306,18 +305,18 @@ public class TestCluster extends TestBase {
// test a non-admin user // test a non-admin user
String user2 = "test", password2 = getPassword("test"); String user2 = "test", password2 = getPassword("test");
connApp = DriverManager.getConnection(urlCluster, user2, password2); connApp = getConnection(urlCluster, user2, password2);
check(connApp, len, "'" + serverList + "'"); check(connApp, len, "'" + serverList + "'");
connApp.close(); connApp.close();
n1.stop(); n1.stop();
// test non-admin cluster connection if only one server runs // test non-admin cluster connection if only one server runs
Connection connApp2 = DriverManager.getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2); Connection connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
check(connApp2, len, "''"); check(connApp2, len, "''");
connApp2.close(); connApp2.close();
// test non-admin cluster connection if only one server runs // test non-admin cluster connection if only one server runs
connApp2 = DriverManager.getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2); connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
check(connApp2, len, "''"); check(connApp2, len, "''");
connApp2.close(); connApp2.close();
...@@ -340,7 +339,7 @@ public class TestCluster extends TestBase { ...@@ -340,7 +339,7 @@ public class TestCluster extends TestBase {
String urlNode1 = getURL("node1/test", true); String urlNode1 = getURL("node1/test", true);
String urlNode2 = getURL("node2/test", true); String urlNode2 = getURL("node2/test", true);
String user = getUser(), password = getPassword(); String user = getUser(), password = getPassword();
conn = DriverManager.getConnection(urlNode1, user, password); conn = getConnection(urlNode1, user, password);
Statement stat; Statement stat;
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST"); stat.execute("DROP TABLE IF EXISTS TEST");
...@@ -372,21 +371,21 @@ public class TestCluster extends TestBase { ...@@ -372,21 +371,21 @@ public class TestCluster extends TestBase {
getConnection("jdbc:h2:tcp://localhost:"+port2+"/test", user, password); getConnection("jdbc:h2:tcp://localhost:"+port2+"/test", user, password);
// test a cluster connection // test a cluster connection
conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password); conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
check(conn, len, "'"+serverList+"'"); check(conn, len, "'"+serverList+"'");
conn.close(); conn.close();
// stop server 2, and test if only one server is available // stop server 2, and test if only one server is available
n2.stop(); n2.stop();
conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password); conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
check(conn, len, "''"); check(conn, len, "''");
conn.close(); conn.close();
conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password); conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
check(conn, len, "''"); check(conn, len, "''");
conn.close(); conn.close();
// disable the cluster // disable the cluster
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:"+port1+"/test;CLUSTER=''", user, password); conn = getConnection("jdbc:h2:tcp://localhost:"+port1+"/test;CLUSTER=''", user, password);
conn.close(); conn.close();
n1.stop(); n1.stop();
...@@ -398,7 +397,7 @@ public class TestCluster extends TestBase { ...@@ -398,7 +397,7 @@ public class TestCluster extends TestBase {
n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start(); n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start(); n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password); conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("CREATE TABLE BOTH(ID INT)"); stat.execute("CREATE TABLE BOTH(ID INT)");
...@@ -409,13 +408,13 @@ public class TestCluster extends TestBase { ...@@ -409,13 +408,13 @@ public class TestCluster extends TestBase {
n2.stop(); n2.stop();
n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start(); n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:"+port1+"/test;CLUSTER=''", user, password); conn = getConnection("jdbc:h2:tcp://localhost:"+port1+"/test;CLUSTER=''", user, password);
check(conn, len, "''"); check(conn, len, "''");
conn.close(); conn.close();
n1.stop(); n1.stop();
n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start(); n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test;CLUSTER=''", user, password); conn = getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test;CLUSTER=''", user, password);
check(conn, len, "''"); check(conn, len, "''");
conn.createStatement().execute("SELECT * FROM A"); conn.createStatement().execute("SELECT * FROM A");
conn.close(); conn.close();
......
...@@ -9,11 +9,11 @@ package org.h2.test.db; ...@@ -9,11 +9,11 @@ package org.h2.test.db;
import java.io.File; import java.io.File;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.dev.fs.FilePathZip2; import org.h2.dev.fs.FilePathZip2;
import org.h2.store.FileLister; import org.h2.store.FileLister;
...@@ -63,20 +63,20 @@ public class TestReadOnly extends TestBase { ...@@ -63,20 +63,20 @@ public class TestReadOnly extends TestBase {
stat.execute("CREATE TABLE TEST(ID INT) AS SELECT X FROM SYSTEM_RANGE(1, 20)"); stat.execute("CREATE TABLE TEST(ID INT) AS SELECT X FROM SYSTEM_RANGE(1, 20)");
conn.close(); conn.close();
Backup.execute(dir + "/readonly.zip", dir, "readonly", true); Backup.execute(dir + "/readonly.zip", dir, "readonly", true);
conn = DriverManager.getConnection( conn = getConnection(
"jdbc:h2:zip:"+dir+"/readonly.zip!/readonly", getUser(), getPassword()); "jdbc:h2:zip:"+dir+"/readonly.zip!/readonly", getUser(), getPassword());
conn.createStatement().execute("select * from test where id=1"); conn.createStatement().execute("select * from test where id=1");
conn.close(); conn.close();
Server server = Server.createTcpServer("-tcpPort", "9081", "-baseDir", dir); Server server = Server.createTcpServer("-tcpPort", "9081", "-baseDir", dir);
server.start(); server.start();
try { try {
conn = DriverManager.getConnection( conn = getConnection(
"jdbc:h2:tcp://localhost:9081/zip:readonly.zip!/readonly", "jdbc:h2:tcp://localhost:9081/zip:readonly.zip!/readonly",
getUser(), getPassword()); getUser(), getPassword());
conn.createStatement().execute("select * from test where id=1"); conn.createStatement().execute("select * from test where id=1");
conn.close(); conn.close();
FilePathZip2.register(); FilePathZip2.register();
conn = DriverManager.getConnection( conn = getConnection(
"jdbc:h2:tcp://localhost:9081/zip2:readonly.zip!/readonly", "jdbc:h2:tcp://localhost:9081/zip2:readonly.zip!/readonly",
getUser(), getPassword()); getUser(), getPassword());
conn.createStatement().execute("select * from test where id=1"); conn.createStatement().execute("select * from test where id=1");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论