搭建H2数据库

下载链接:http://www.h2database.com/

  1. 解压压缩包

    unzip h2-2023-07-04.zip
    
  2. 启动H2数据库服务器

    [root@localhost ~]# cd ./h2/bin/
    [root@localhost bin]# java -cp h2-2.2.220.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers
    TCP server running at tcp://localhost:9092 (others can connect)
    Web Console server running at http://localhost:8082 (others can connect)
    
    

    可以通过参数修改端口

    [root@localhost bin]# java -cp h2-2.2.220.jar org.h2.tools.Server -web -webPort 18082 -webAllowOthers -tcp -tcpPort 19092 -tcpAllowOthers
    TCP server running at tcp://localhost:19092 (others can connect)
    Web Console server running at http://localhost:18082 (others can connect)
    
  3. 创建数据库

    [root@localhost bin]# java -cp h2-*.jar org.h2.tools.Shell
    
    Welcome to H2 Shell 2.2.220 (2023-07-04)
    Exit with Ctrl+C
    [Enter]   jdbc:h2:~/test
    URL       jdbc:h2:/db/upp	#数据库url
    [Enter]   org.h2.Driver
    Driver    
    [Enter]   
    User      admin		#用户名
    Password  			#密码
    Type the same password again to confirm database creation.
    Password  
    Connected
    Commands are case insensitive; SQL statements end with ';'
    help or ?      Display this help
    list           Toggle result list / stack trace mode
    maxwidth       Set maximum column width (default is 100)
    autocommit     Enable or disable autocommit
    history        Show the last 20 statements
    quit or exit   Close the connection and exit
    
    sql> 
    

    完成后在/db目录下可以看到数据库文件upp.mv.db

  4. 连接数据库

    • 浏览器连接数据库

      网址:http://localhost:8082

    • TCP连接数据库

      URL:jdbc:h2:tcp://localhost:9092//db/upp

      使用IDEA连接数据库时,IDEA尝试使用以下sql语句SELECT CATALOG_NAME FROM INFORMATION_SCHEMA.CATALOGS查找h2中的表,而h2新版本不再支持该语句,可通过添加OLD_INFORMATION_SCHEMA=TRUE 到URL来解决

      例:jdbc:h2:tcp://localhost:9092//db/upp;OLD_INFORMATION_SCHEMA=TRUE

posted @   北兢王  阅读(196)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示
主题色彩