提交 9f7d4136 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 437073c1
db1 = H2, org.h2.Driver, jdbc:h2:data/test;LOCK_TIMEOUT=10000, sa, sa
xdb2 = H2 (XTEA), org.h2.Driver, jdbc:h2:data/test_xtea;LOCK_TIMEOUT=10000;CIPHER=XTEA, sa, sa 123
xdb3 = H2 (AES), org.h2.Driver, jdbc:h2:data/test_aes;LOCK_TIMEOUT=10000;CIPHER=AES, sa, sa 123
db2 = HSQLDB, org.hsqldb.jdbcDriver, jdbc:hsqldb:data/test;hsqldb.default_table_type=cached, sa
db3 = Derby, org.apache.derby.jdbc.EmbeddedDriver, jdbc:derby:data/test;create=true, sa, sa
......
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.cases;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.DeleteDbFiles;
/*
1) the trace has one function "Enabling the Trace Option at Runtime by
Manually Creating a File". how could I disable this function? I don't
want the user could use one file to trace my database info...
*/
/*
D:\data\h2\bin>java -Xrunhprof:cpu=samples,depth=8 org.h2.test.TestAll
Java: 1.5.0_10-b03, Java HotSpot(TM) Client VM, Sun Microsystems Inc.
Env: Windows XP, x86, 5.1, Service Pack 2, \ ; \r\n CH de Cp1252
time to connect: 984ms
h2.lobFilesInDirectories: true
h2.lobFilesPerDirectory: 256
writing 1000: 5688ms
writing 2000: 4062ms
writing 3000: 5766ms
writing 4000: 3531ms
writing 5000: 3860ms
writing 6000: 8734ms
writing 7000: 3703ms
writing 8000: 3906ms
writing 9000: 11250ms
writing 10000: 3813ms
time to complete writing: 54313ms
Dumping CPU usage by sampling running threads ... done.
h2.lobFilesInDirectories: false
time to complete writing: 49828ms
time to complete reading: 2953ms
time to connect: 984ms
h2.lobFilesInDirectories: true
h2.lobFilesPerDirectory: 256
time to complete writing: 98687ms
time to complete reading: 2625ms
time to connect: 47ms
FIXED:
h2.lobFilesPerDirectory: 256
time to complete writing: 45656ms
h2.lobFilesPerDirectory: 1024
time to complete writing: 158204ms
h2.lobFilesInDirectories: false
time to complete writing: 17187ms
h2.lobFilesInDirectories: true
h2.lobFilesPerDirectory: 16
writing 1000: 5610ms
writing 2000: 1984ms
writing 3000: 3000ms
writing 4000: 6844ms
writing 5000: 7734ms
writing 6000: 11578ms
writing 7000: 6407ms
writing 8000: 6812ms
writing 9000: 15344ms
writing 10000: 7375ms
time to complete writing: 72688ms
time to complete writing: 74578ms
time to complete reading: 2734ms
time to connect: 47ms
*/
public class TestBlobDir {
// Start h2 start parameters:
// -Dh2.lobFilesInDirectories=true -Dh2.lobFilesPerDirectory=16
public static void main(String[] args) throws Exception {
System.setProperty("h2.lobFilesInDirectories", "true");
System.setProperty("h2.lobFilesPerDirectory", "256");
TestBlobDir blobtest = new TestBlobDir();
DeleteDbFiles.execute(".", "testabc", true);
Connection conn = blobtest.getConnection();
long count;
count = 10000;
blobtest.printParameters(conn);
blobtest.insertBlobs(conn, 1, count, 500);
// blobtest.testBlobs(conn, 1, count);
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// test time to build up a new connection
// conn = blobtest.getConnection();
// try {
// conn.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
}
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
// String url = "jdbc:h2:tcp://localhost:9092/test123456789012345678901234567890";
// String url = "jdbc:h2:test123456789012345678901234567890";
String url = "jdbc:h2:testabc";
Driver driver = DriverManager.getDriver(url);
driver.toString();
long start = System.currentTimeMillis();
conn = DriverManager.getConnection(url, "sa", "");
System.out.println("time to connect: " + (System.currentTimeMillis() - start + "ms"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
return conn;
}
public void printParameters(Connection conn) {
String sqlStmt = "SELECT * FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME LIKE 'h2.lob%';";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlStmt);
while (rs.next()) {
System.out.print(rs.getString("name") + ": ");
System.out.println(rs.getString("value"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertBlobs(Connection conn, long from, long to, int blobLength) {
byte[] byteblob = new byte[blobLength];
for (int i = 0; i < byteblob.length; i++) {
byteblob[i] = 'b';
}
// System.out.println("Inserting blobs of length " + blobLength + " from " + from + " to " + to);
String sqlStmt = "INSERT INTO blobtable" + " (count, blobtest) VALUES (?1, ?2)";
long startComulative = -1;
PreparedStatement prepStmt = null;
try {
conn.createStatement().execute("DROP TABLE blobtable IF EXISTS");
conn.createStatement().execute("CREATE TABLE blobtable(count INT4, blobtest BLOB, PRIMARY KEY (count))");
prepStmt = conn.prepareStatement(sqlStmt);
startComulative = System.currentTimeMillis();
long start = System.currentTimeMillis();
for (long i = 1; i <= to; i++) {
prepStmt.setLong(1, i);
InputStream blob = new ByteArrayInputStream(byteblob);
prepStmt.setBinaryStream(2, blob, -1);
prepStmt.execute();
if (i % 1000 == 0) {
System.out.println("writing " + i + ": " + (System.currentTimeMillis() - start + "ms"));
start = System.currentTimeMillis();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("time to complete writing: " + (System.currentTimeMillis() - startComulative) + "ms");
if (prepStmt != null) {
try {
prepStmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void testBlobs(Connection conn, long from, long to) {
// System.out.println("Reading blobs" + " from " + from + " to " + to);
ResultSet rs = null;
byte[] data;
BufferedInputStream imageInputStream = null;
String sqlStmt = "SELECT * FROM blobtable where count=?1";
long startComulative = -1;
PreparedStatement prepStmt = null;
try {
prepStmt = conn.prepareStatement(sqlStmt);
// long start = System.currentTimeMillis();
startComulative = System.currentTimeMillis();
for (long i = 1; i <= to; i++) {
prepStmt.setLong(1, i);
rs = prepStmt.executeQuery();
if (rs.next()) {
int size = (int) rs.getBlob("blobtest").length();
imageInputStream = new BufferedInputStream(rs.getBinaryStream("blobtest"));
try {
DataInputStream in = new DataInputStream(imageInputStream);
data = new byte[size];
in.readFully(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("error, no data");
}
// if (i % 1000 == 0) {
// System.out.println("reading " + i + ": " + (System.currentTimeMillis() - start + "ms"));
// start = System.currentTimeMillis();
// }
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("time to complete reading: " + (System.currentTimeMillis() - startComulative) + "ms");
return;
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.cases;
import java.sql.*;
import java.util.*;
import java.text.*;
public class TestCalendar {
static Connection conn;
static Statement stat;
public static void main(String[] args) throws Exception {
// Firstly, demonstrate Java behaviour with 'illegal' times:
// Change default timezone...same as setting the Windows timezone in Control Panel.
// TimeZone.setDefault(TimeZone.getTimeZone("PST"));
TimeZone.setDefault(TimeZone.getTimeZone("Australia/Melbourne"));
// String s;
// Timestamp t;
//
// System.out.println("Watch carefully: this is what Java does with 'illegal' local times:");
//
// s = "2006-10-29 01:30:00"; // Valid
// t = Timestamp.valueOf(s);
// System.out.println("Instantiated: " + s + " Local: " + t.toString());
//
// s = "2006-10-29 01:59:59"; // Valid
// t = Timestamp.valueOf(s);
// System.out.println("Instantiated: " + s + " Output: " + t.toString());
//
// s = "2006-10-29 02:00:00"; // Illegal time...does not 'exist'
// t = Timestamp.valueOf(s);
// System.out.println("Instantiated: " + s + " Output: " + t.toString());
//
// s = "2006-10-29 02:30:00"; // Illegal time...does not 'exist'
// t = Timestamp.valueOf(s);
// System.out.println("Instantiated: " + s + " Output: " + t.toString());
//
// s = "2006-10-29 02:59:59"; // Illegal time...does not 'exist'
// t = Timestamp.valueOf(s);
// System.out.println("Instantiated: " + s + " Output: " + t.toString());
//
// s = "2006-10-29 03:00:00"; // Valid again
// t = Timestamp.valueOf(s);
// System.out.println("Instantiated: " + s + " Output: " + t.toString());
//
// System.out.println();
// System.out.println();
// String url = "jdbc:pervasive://maximus:1583/FILD602";
// String driver = "com.pervasive.jdbc.v2.Driver";
// String uid = "Master"; String pwd = "master";
// String createquery = "create table TT (RECID INTEGER, MYDATETIME DATETIME, PRIMARY KEY(RECID))";
// String insertquery = "insert into TT values(1, '2006-10-29 02:00:00')";
// String selectquery = "select MYDATETIME from TT";
// String url = "jdbc:h2:mem:";
// String driver = "org.h2.Driver";
// String user = "sa";
// String password = "sa";
String url = "jdbc:h2:mem:";
String driver = "org.h2.Driver";
String user = "sa";
String password = "sa";
testDb(driver, url, user, password);
testDb("org.postgresql.Driver", "jdbc:postgresql:jpox2", "sa", "sa");
// String url = "jdbc:sybase:Tds:atlas:2638/toptier";
// String driver = "com.sybase.jdbc3.jdbc.SybDriver";
// String uid = "dba"; String pwd = "sql";
// String createquery = "create table TT (RECID INTEGER, MYDATETIME DATETIME, PRIMARY KEY(RECID))";
// String insertquery = "insert into TT values(1, '2006-10-29 02:00:00')";
// String selectquery = "select MYDATETIME from TT";
}
private static void testDb(String driver, String url, String user, String password) throws Exception {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String createquery = "create table TT (RECID INTEGER, MYDATETIME TIMESTAMP, PRIMARY KEY(RECID))";
// String insertquery = "insert into TT values(1, '2006-10-28T16:00:00')";
// String insertquery = "insert into TT values(1, '2006-10-28T16:00:00+00:00')";
// String insertquery = "insert into TT values(1, '2006-10-28T16:00:00')";
// String insertquery = "insert into TT values(1, '2006-01-28T16:00:00')";
String insertquery = "insert into TT values(1, '2006-01-20T16:00:00+11:00')";
// String insertquery = "insert into TT values(1, '2006-10-28T16:00:00+10:00')";
// String insertquery = "insert into TT values(1, '2006-10-28T16:00:00+09:00')";
String selectquery = "select MYDATETIME from TT";
try {
stat.execute("DROP TABLE TT");
} catch (SQLException e) {
// ignore
}
stat.execute(createquery);
stat.execute("delete from TT");
stat.execute(insertquery);
System.out.println("Database timestamp retrieval test.");
ResultSet results = stat.executeQuery(selectquery);
if (results != null) {
Calendar c = Calendar.getInstance();
DateFormat df;
c.setTimeZone(TimeZone.getTimeZone("UTC"));
while (results.next()) {
// Firstly exercise the default behaviours
// Because the value in the database is an 'illegal' local time in Melbourne
// we cannot expect Java to output it correctly. Java 1.5 adds an hour to it to make it a 'legal'
// time.
Timestamp t = results.getTimestamp(1);
System.out.println("Interpret db value as default tz: " + t.toString());
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); // DateFormat.getDateTimeInstance();
System.out.println("Interpret db value as default: " + df.format(t) + " <<<Should display as UTC, not local");
// Second test: instruct the jdbc driver to
// interpret the time as a UTC time, in which case it IS legal.
// It should be output as the correct UTC value (i.e. same as what's in the database)
t = results.getTimestamp(1, c);
// df.setCalendar(c);
System.out.println("Interpret db value as UTC: " + df.format(t) + " <<<Should display as UTC, not local");
df.setCalendar(c);
System.out.println("Interpret db value as UTC: " + df.format(t) + " <<<Should display as UTC, not local");
}
}
stat.close();
conn.close();
}
}
......@@ -6,127 +6,147 @@ package org.h2.test.cases;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.tools.Server;
public class TestHibernateClob {
public static void main(String[] a) throws Exception {
System.out.println("starting test...");
org.h2.tools.DeleteDbFiles.execute(null, "test", true);
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "sa");
conn.createStatement().execute("CREATE TABLE IF NOT EXISTS TEST(ID INT)");
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getTables(null, null, "TEST", null);
while(rs.next()) {
String cat = rs.getString("TABLE_CAT");
String schema = rs.getString("TABLE_SCHEM");
String table = rs.getString("TABLE_NAME");
ResultSet rs2 = meta.getColumns(cat, schema, table, null);
while(rs2.next()) {
System.out.println(table + "." + rs2.getString("COLUMN_NAME"));
}
mainClobSpeed();
mainClobSpeed();
mainClobSpeed();
}
conn.getAutoCommit();
conn.setAutoCommit(false);
DatabaseMetaData dbMeta0 =
conn.getMetaData();
dbMeta0.getDatabaseProductName();
dbMeta0.getDatabaseMajorVersion();
dbMeta0.getDatabaseProductVersion();
dbMeta0.getDriverName();
dbMeta0.getDriverVersion();
dbMeta0.supportsResultSetType(1004);
dbMeta0.supportsBatchUpdates();
dbMeta0.dataDefinitionCausesTransactionCommit();
dbMeta0.dataDefinitionIgnoredInTransactions();
dbMeta0.supportsGetGeneratedKeys();
conn.getAutoCommit();
conn.getAutoCommit();
conn.commit();
conn.setAutoCommit(true);
Statement stat0 =
conn.createStatement();
stat0.executeUpdate("drop table CLOB_ENTITY if exists");
stat0.getWarnings();
stat0.executeUpdate("create table CLOB_ENTITY (ID bigint not null, DATA clob, CLOB_DATA clob, primary key (ID))");
stat0.getWarnings();
stat0.close();
conn.getWarnings();
conn.clearWarnings();
conn.setAutoCommit(false);
conn.getAutoCommit();
conn.getAutoCommit();
PreparedStatement prep0 =
conn.prepareStatement("select max(ID) from CLOB_ENTITY");
ResultSet rs0 =
prep0.executeQuery();
rs0.next();
rs0.getLong(1);
rs0.wasNull();
rs0.close();
prep0.close();
conn.getAutoCommit();
PreparedStatement prep1 =
conn.prepareStatement("insert into CLOB_ENTITY (DATA, CLOB_DATA, ID) values (?, ?, ?)");
prep1.setNull(1, 2005);
StringBuffer buff = new StringBuffer(20000);
for(int i=0; i<10000; i++) {
buff.append((char)('0' + (i%10)));
}
Reader x = new StringReader(buff.toString());
prep1.setCharacterStream(2, x, 10000);
prep1.setLong(3, 1);
prep1.addBatch();
prep1.executeBatch();
prep1.close();
conn.getAutoCommit();
conn.getAutoCommit();
conn.commit();
conn.isClosed();
conn.getWarnings();
conn.clearWarnings();
conn.getAutoCommit();
conn.getAutoCommit();
PreparedStatement prep2 =
conn.prepareStatement("select c_.ID as ID0_0_, c_.DATA as S2, c_.CLOB_DATA as CLOB3_0_0_ from CLOB_ENTITY c_ where c_.ID=?");
prep2.setLong(1, 1);
ResultSet rs1 =
prep2.executeQuery();
rs1.next();
System.out.println("s2: " + rs1.getCharacterStream("S2"));
Clob clob0 =
rs1.getClob("CLOB3_0_0_");
System.out.println("wasNull: " + rs1.wasNull());
rs1.next();
rs1.close();
prep2.getMaxRows();
prep2.getQueryTimeout();
prep2.close();
conn.getAutoCommit();
Reader r = clob0.getCharacterStream();
char[] chars = new char[(int)clob0.length()];
int read = r.read(chars);
System.out.println("read: " + read + " " + r);
static void mainClobSpeed() throws Exception {
System.out.println("starting test...");
org.h2.tools.DeleteDbFiles.execute(null, "test", true);
Server server = Server.createTcpServer(new String[0]);
server.start();
Connection conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/test", "sa", "sa");
conn.createStatement().execute("CREATE TABLE TEST(C CLOB)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?)");
long time = System.currentTimeMillis();
for(int i=0; i<10000; i++) {
int ch = chars[i];
if(ch != ('0' + (i%10))) {
throw new Error("expected "+ (char)('0' + (i%10)) + " got: " + ch + " (" + (char)ch + ")");
}
}
int ch = r.read();
if(ch != -1) {
System.out.println("expected -1 got: " + ch );
Reader r = new StringReader("Hello World");
prep.setCharacterStream(1, r, -1);
prep.execute();
}
time = System.currentTimeMillis() - time;
System.out.println("time: " + time);
conn.close();
System.out.println("done");
server.stop();
// Class.forName("org.h2.Driver");
// Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "sa");
// conn.createStatement().execute("CREATE TABLE IF NOT EXISTS TEST(ID INT)");
// DatabaseMetaData meta = conn.getMetaData();
// ResultSet rs = meta.getTables(null, null, "TEST", null);
// while(rs.next()) {
// String cat = rs.getString("TABLE_CAT");
// String schema = rs.getString("TABLE_SCHEM");
// String table = rs.getString("TABLE_NAME");
// ResultSet rs2 = meta.getColumns(cat, schema, table, null);
// while(rs2.next()) {
// System.out.println(table + "." + rs2.getString("COLUMN_NAME"));
// }
// }
//
// conn.getAutoCommit();
// conn.setAutoCommit(false);
// DatabaseMetaData dbMeta0 =
// conn.getMetaData();
// dbMeta0.getDatabaseProductName();
// dbMeta0.getDatabaseMajorVersion();
// dbMeta0.getDatabaseProductVersion();
// dbMeta0.getDriverName();
// dbMeta0.getDriverVersion();
// dbMeta0.supportsResultSetType(1004);
// dbMeta0.supportsBatchUpdates();
// dbMeta0.dataDefinitionCausesTransactionCommit();
// dbMeta0.dataDefinitionIgnoredInTransactions();
// dbMeta0.supportsGetGeneratedKeys();
// conn.getAutoCommit();
// conn.getAutoCommit();
// conn.commit();
// conn.setAutoCommit(true);
// Statement stat0 =
// conn.createStatement();
// stat0.executeUpdate("drop table CLOB_ENTITY if exists");
// stat0.getWarnings();
// stat0.executeUpdate("create table CLOB_ENTITY (ID bigint not null, DATA clob, CLOB_DATA clob, primary key (ID))");
// stat0.getWarnings();
// stat0.close();
// conn.getWarnings();
// conn.clearWarnings();
// conn.setAutoCommit(false);
// conn.getAutoCommit();
// conn.getAutoCommit();
// PreparedStatement prep0 =
// conn.prepareStatement("select max(ID) from CLOB_ENTITY");
// ResultSet rs0 =
// prep0.executeQuery();
// rs0.next();
// rs0.getLong(1);
// rs0.wasNull();
// rs0.close();
// prep0.close();
// conn.getAutoCommit();
// PreparedStatement prep1 =
// conn.prepareStatement("insert into CLOB_ENTITY (DATA, CLOB_DATA, ID) values (?, ?, ?)");
// prep1.setNull(1, 2005);
// StringBuffer buff = new StringBuffer(20000);
// for(int i=0; i<10000; i++) {
// buff.append((char)('0' + (i%10)));
// }
// Reader x = new StringReader(buff.toString());
// prep1.setCharacterStream(2, x, 10000);
// prep1.setLong(3, 1);
// prep1.addBatch();
// prep1.executeBatch();
// prep1.close();
// conn.getAutoCommit();
// conn.getAutoCommit();
// conn.commit();
// conn.isClosed();
// conn.getWarnings();
// conn.clearWarnings();
// conn.getAutoCommit();
// conn.getAutoCommit();
// PreparedStatement prep2 =
// conn.prepareStatement("select c_.ID as ID0_0_, c_.DATA as S2, c_.CLOB_DATA as CLOB3_0_0_ from CLOB_ENTITY c_ where c_.ID=?");
// prep2.setLong(1, 1);
// ResultSet rs1 =
// prep2.executeQuery();
// rs1.next();
// System.out.println("s2: " + rs1.getCharacterStream("S2"));
// Clob clob0 =
// rs1.getClob("CLOB3_0_0_");
// System.out.println("wasNull: " + rs1.wasNull());
// rs1.next();
// rs1.close();
// prep2.getMaxRows();
// prep2.getQueryTimeout();
// prep2.close();
// conn.getAutoCommit();
// Reader r = clob0.getCharacterStream();
// char[] chars = new char[(int)clob0.length()];
// int read = r.read(chars);
// System.out.println("read: " + read + " " + r);
// for(int i=0; i<10000; i++) {
// int ch = chars[i];
// if(ch != ('0' + (i%10))) {
// throw new Error("expected "+ (char)('0' + (i%10)) + " got: " + ch + " (" + (char)ch + ")");
// }
// }
// int ch = r.read();
// if(ch != -1) {
// System.out.println("expected -1 got: " + ch );
// }
// conn.close();
// System.out.println("done");
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.cases;
import java.sql.*;
public class TestObject {
public static void main(String[] args) throws Exception {
testWith("org.hsqldb.jdbcDriver", "jdbc:hsqldb:test", "sa", "");
// testWith("org.postgresql.Driver", "jdbc:postgresql:jpox2", "sa", "sa");
// testWith("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "sa", "sa");
testWith("org.h2.Driver", "jdbc:h2:test", "sa", "sa");
}
static void testWith(String driver, String url, String user, String password) throws Exception {
Class.forName(driver);
System.out.println("URL: " + url);
Connection conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(true);
Statement st = conn.createStatement();
try {
st.executeUpdate("DROP TABLE test_object_table");
} catch (SQLException e) {
// ignore
}
// st.executeUpdate("CREATE TABLE test_object_table(id INTEGER NOT NULL, object0 JAVA_OBJECT NOT NULL, PRIMARY KEY(id))");
st.executeUpdate("CREATE TABLE test_object_table(id INTEGER NOT NULL, object0 OBJECT NOT NULL, PRIMARY KEY(id))");
// st.executeUpdate("CREATE TABLE test_object_table(id INTEGER NOT NULL, object0 OID NOT NULL, PRIMARY KEY(id))");
PreparedStatement ps = conn.prepareStatement("INSERT INTO test_object_table values(?, ?)");
ps.setInt(1, 1);
ps.setObject(2, new Integer(3));
ps.executeUpdate();
ResultSet rs = st.executeQuery("select * from test_object_table");
while (rs.next()) {
int id = rs.getInt("id");
Object object0 = rs.getObject("object0");
System.out.println("id = " + id + ", object0 = " + object0.getClass().getName() + " / " + object0);
}
rs.close();
conn.close();
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.Statement;
import org.h2.test.TestBase;
import org.h2.tools.Restore;
public class TestBackup extends TestBase {
public void test() throws Exception {
if(config.memory || config.logMode == 0) {
return;
}
testBackup();
}
private void testBackup() throws Exception {
deleteDb("backup");
Connection conn1, conn2, conn3;
Statement stat1, stat2, stat3;
conn1 = getConnection("backup");
stat1 = conn1.createStatement();
stat1.execute("create table test(id int primary key, name varchar(255))");
stat1.execute("insert into test values(1, 'first'), (2, 'second')");
stat1.execute("create table testlob(id int primary key, b blob, c clob)");
stat1.execute("insert into testlob values(1, space(10000), repeat('00', 10000))");
conn2 = getConnection("backup");
stat2 = conn2.createStatement();
stat2.execute("insert into test values(3, 'third')");
conn2.setAutoCommit(false);
stat2.execute("insert into test values(4, 'fourth (uncommitted)')");
stat2.execute("insert into testlob values(2, ' ', '00')");
stat1.execute("backup to '" + BASE_DIR + "/backup.zip'");
conn2.rollback();
Restore.execute(BASE_DIR + "/backup.zip", BASE_DIR, "restored", true);
conn3 = getConnection("restored");
stat3 = conn3.createStatement();
compareDatabases(stat1, stat3);
conn1.close();
conn2.close();
conn3.close();
}
}
......@@ -12,6 +12,35 @@ import org.h2.test.TestBase;
public class TestLinkedTable extends TestBase {
public void test() throws Exception {
testLinkSchema();
testLinkTable();
}
private void testLinkSchema() throws Exception {
deleteDb("linked1");
deleteDb("linked2");
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:"+BASE_DIR+"/linked1", "sa1", "abc");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST1(ID INT PRIMARY KEY)");
Connection conn2 = DriverManager.getConnection("jdbc:h2:"+BASE_DIR+"/linked2", "sa2", "def");
Statement stat2 = conn2.createStatement();
String link = "CALL LINK_SCHEMA('LINKED', '', 'jdbc:h2:"+BASE_DIR+"/linked1', 'sa1', 'abc', 'PUBLIC')";
stat2.execute(link);
stat2.executeQuery("SELECT * FROM LINKED.TEST1");
stat.execute("CREATE TABLE TEST2(ID INT PRIMARY KEY)");
stat2.execute(link);
stat2.executeQuery("SELECT * FROM LINKED.TEST1");
stat2.executeQuery("SELECT * FROM LINKED.TEST2");
conn.close();
conn2.close();
}
private void testLinkTable() throws Exception {
deleteDb("linked1");
deleteDb("linked2");
Class.forName("org.h2.Driver");
......
......@@ -4,11 +4,16 @@
*/
package org.h2.test.db;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.DatabaseEventListener;
import org.h2.test.TestBase;
import org.h2.tools.Backup;
import org.h2.tools.Restore;
public class TestOpenClose extends TestBase implements DatabaseEventListener {
......@@ -41,7 +46,7 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
stat.execute("BACKUP TO '"+BASE_DIR+"/test.zip'");
conn.close();
deleteDb(BASE_DIR, "openClose");
Backup.restoreFiles(BASE_DIR + "/test.zip", BASE_DIR);
Restore.execute(BASE_DIR + "/test.zip", BASE_DIR, null, true);
conn = DriverManager.getConnection(url, "sa", "abc def");
stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
......
......@@ -5,10 +5,8 @@
package org.h2.test.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import org.h2.api.Trigger;
import org.h2.test.TestBase;
......@@ -25,7 +23,6 @@ public class TestRunscript extends TestBase implements Trigger {
}
private void test(boolean password) throws Exception {
deleteDb("runscript");
Connection conn1, conn2;
Statement stat1, stat2;
......@@ -82,26 +79,6 @@ public class TestRunscript extends TestBase implements Trigger {
conn2.close();
}
private void compareDatabases(Statement stat1, Statement stat2) throws Exception {
ResultSet rs1 = stat1.executeQuery("SCRIPT NOPASSWORDS");
ResultSet rs2 = stat2.executeQuery("SCRIPT NOPASSWORDS");
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
while(rs1.next()) {
check(rs2.next());
list1.add(rs1.getString(1));
list2.add(rs2.getString(1));
}
for(int i=0; i<list1.size(); i++) {
String s = (String)list1.get(i);
if(!list2.remove(s)) {
error("not found: " + s);
}
}
check(list2.size(), 0);
checkFalse(rs2.next());
}
public void init(Connection conn, String schemaName, String triggerName, String tableName) {
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论