简单来说就是用jdbc:h2:mem:h2db来建立内存模式,并建表,
然后jdbc:h2:tcp://19216820141:8082/mem:h2db来访问上面的内存数据库
package test;
import javasqlConnection;
import javasqlDriverManager;
import javasqlResultSet;
import javasqlSQLException;
import javasqlStatement;
import orgh2toolsServer;
public class H2Demo {
private Server server;
private String port = "8082";
private static String sourceURL1 = "jdbc:h2:mem:h2db";
private static String sourceURL2 = "jdbc:h2:tcp://19216820141:8082/mem:h2db";
private String user = "shorturl";
private String password = "123456";
public void startServer() {
try {
Systemoutprintln("正在启动h2");
server = ServercreateTcpServer(
new String[] { "-tcpPort", port })start();
} catch (SQLException e) {
Systemoutprintln("启动h2出错:" + etoString());
// TODO Auto-generated catch block
eprintStackTrace();
throw new RuntimeException(e);
}
}
public void stopServer() {
if (server != null) {
Systemoutprintln("正在关闭h2");
serverstop();
Systemoutprintln("关闭成功");
}
}
public void useH2() {
try {
ClassforName("orgh2Driver");
Connection conn = DriverManagergetConnection(sourceURL1,user, password);
Statement stat = conncreateStatement();
// insert data
statexecute("CREATE MEMORY Table TEST(NAME VARCHAR)");
statexecute("INSERT INTO TEST VALUES('Hello World')");
//statexecute("delete mappedURL");
// use data
ResultSet result = statexecuteQuery("select name from test ");
int i = 1;
while (resultnext()) {
Systemoutprintln(i++ + ":" + resultgetString("name"));
}
resultclose();
statclose();
connclose();
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
public void useH2i() {
try {
ClassforName("orgh2Driver");
//Connection conn = DriverManagergetConnection("jdbc:h2:" + dbDir+";AUTO_SERVER=TRUE;MVCC=TRUE",user, password);
Connection conn = DriverManagergetConnection(sourceURL2,user, password);
Statement stat = conncreateStatement();
// use data
ResultSet result = statexecuteQuery("select name from test");
1、h2数据库你写一个dao类(例如:insert方法),mysql也写一个mydao类(例如:insertmysql()),
当往h2数据库执行插入的时候 new dao()inser(sql);
new mydao()insertmysql(sql);
2、也可以直接到数据库 *** 作写存储过程和游标自动同步。
3、使用第三方插件。有很多这样子的平台做企业数据一体化的
以上就是关于怎么远程访问H2数据库的内存模式全部的内容,包括:怎么远程访问H2数据库的内存模式、用java 实现 h2数据库和mysql数据库实时数据同步、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)