How can I connect with an SQL tool to an embedded h2 db?

喜欢而已 提交于 2019-12-05 07:20:35

问题


I have a simple h2 database example, I assume it is a database that is stored in a single file. But where do I find this file? I'd like to connect to that db using SQL clients like Squirrel. Where is this file placed by default?

    <property name="eclipselink.jdbc.platform"
        value="org.eclipse.persistence.platform.database.H2Platform" />
    <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
    <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/myDB;FILE_LOCK=NO" />
    <property name="javax.persistence.jdbc.user" value="sa" />
    <property name="javax.persistence.jdbc.password" value="sa" />

回答1:


Based on the following value:

jdbc:h2:~/myDB;FILE_LOCK=NO"

It appears that your database file is located in your home directory in a file called myDB

The ~ denotes your home directory.




回答2:


You can use the following code to run H2 in server mode and connect using SQuirrl SQL client.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:target/h2/ps;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

You can use SQuirrel SQL client (http://squirrel-sql.sourceforge.net/) to connect to you H2 database and look at the tables.

Create new connection. Select H2 in the driver dropdown menu Set url to your project target folder h2 folder (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true) Enter user name ("sa") Enter password ("")




回答3:


In your example the file is placed in the file myDB under your home (represented as ~ ) directory :

  <property name="javax.persistence.jdbc.url" value="jdbc:h2:**~/myDB**;FILE_LOCK=NO" />


来源:https://stackoverflow.com/questions/19231155/how-can-i-connect-with-an-sql-tool-to-an-embedded-h2-db

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!