怎么远程访问H2数据库的内存模式

怎么远程访问H2数据库的内存模式,第1张

简单来说就是用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");

以上就是关于怎么远程访问H2数据库的内存模式全部的内容,包括:怎么远程访问H2数据库的内存模式、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9422332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存