怎么远程访问H2数据库的内存模式
1个回答
推荐于2017-09-09 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517196
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
简单来说就是用jdbc:h2:mem:h2db来建立内存模式,并建表,
然后jdbc:h2:tcp://192.168.20.141:8082/mem:h2db来访问上面的内存数据库
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.Server;
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://192.168.20.141:8082/mem:h2db";
private String user = "shorturl";
private String password = "123456";
public void startServer() {
try {
System.out.println("正在启动h2...");
server = Server.createTcpServer(
new String[] { "-tcpPort", port }).start();
} catch (SQLException e) {
System.out.println("启动h2出错:" + e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void stopServer() {
if (server != null) {
System.out.println("正在关闭h2...");
server.stop();
System.out.println("关闭成功.");
}
}
public void useH2() {
try {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(sourceURL1,user, password);
Statement stat = conn.createStatement();
// insert data
stat.execute("CREATE MEMORY Table TEST(NAME VARCHAR)");
stat.execute("INSERT INTO TEST VALUES('Hello World')");
//stat.execute("delete mappedURL");
// use data
ResultSet result = stat.executeQuery("select name from test ");
int i = 1;
while (result.next()) {
System.out.println(i++ + ":" + result.getString("name"));
}
result.close();
stat.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void useH2i() {
try {
Class.forName("org.h2.Driver");
//Connection conn = DriverManager.getConnection("jdbc:h2:" + dbDir+";AUTO_SERVER=TRUE;MVCC=TRUE",user, password);
Connection conn = DriverManager.getConnection(sourceURL2,user, password);
Statement stat = conn.createStatement();
// use data
ResultSet result = stat.executeQuery("select name from test");
然后jdbc:h2:tcp://192.168.20.141:8082/mem:h2db来访问上面的内存数据库
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.Server;
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://192.168.20.141:8082/mem:h2db";
private String user = "shorturl";
private String password = "123456";
public void startServer() {
try {
System.out.println("正在启动h2...");
server = Server.createTcpServer(
new String[] { "-tcpPort", port }).start();
} catch (SQLException e) {
System.out.println("启动h2出错:" + e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void stopServer() {
if (server != null) {
System.out.println("正在关闭h2...");
server.stop();
System.out.println("关闭成功.");
}
}
public void useH2() {
try {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(sourceURL1,user, password);
Statement stat = conn.createStatement();
// insert data
stat.execute("CREATE MEMORY Table TEST(NAME VARCHAR)");
stat.execute("INSERT INTO TEST VALUES('Hello World')");
//stat.execute("delete mappedURL");
// use data
ResultSet result = stat.executeQuery("select name from test ");
int i = 1;
while (result.next()) {
System.out.println(i++ + ":" + result.getString("name"));
}
result.close();
stat.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void useH2i() {
try {
Class.forName("org.h2.Driver");
//Connection conn = DriverManager.getConnection("jdbc:h2:" + dbDir+";AUTO_SERVER=TRUE;MVCC=TRUE",user, password);
Connection conn = DriverManager.getConnection(sourceURL2,user, password);
Statement stat = conn.createStatement();
// use data
ResultSet result = stat.executeQuery("select name from test");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |