打开idea软件
点击右下角的database按钮
接下来单击+按钮
点击Data Source按钮
在依次点击H2按钮
下载驱动,填写连接信息,点击测试连接,测试成功之后,,之后点击OK按钮
简单来说就是用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");
以上就是关于IDEA 2019.2.3怎么创建H2数据库全部的内容,包括:IDEA 2019.2.3怎么创建H2数据库、怎么远程访问H2数据库的内存模式、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)