Loading

搭建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 @ 2023-07-30 21:01  北兢王  阅读(227)  评论(0)    收藏  举报