使用 jdbc:embedded-database 时,如何连接到 Spring 创建的 HSQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8879636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:52:28  来源:igfitidea点击:

How to connect to HSQL which Spring creates when jdbc:embedded-database is used?

springhsqldb

提问by Slava Semushin

I have a HSQL database which Spring automatically creates for me:

我有一个 Spring 自动为我创建的 HSQL 数据库:

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:scheme.sql" /
</jdbc:embedded-database>

And now I want to connect to this database. My question is how to do this, because I don't known which address I should use.

现在我想连接到这个数据库。我的问题是如何做到这一点,因为我不知道应该使用哪个地址。

回答by fredt

This embedded HSQL database is all-in-memory and in-process, therefore accessible only from the Spring Java process. If you want to access the database from another tool as well, for example to check the contents with a database manager, you can start an HSQLDB server with an all-in-memory instance, then connect to the server from Spring and other tools.

这个嵌入式 HSQL 数据库是全内存和进程内的,因此只能从 Spring Java 进程访问。如果您还想从其他工具访问数据库,例如使用数据库管理器检查内容,您可以使用全内存实例启动 HSQLDB 服务器,然后从 Spring 和其他工具连接到服务器。

This is covered in the HSQLDB Guide http://hsqldb.org/doc/2.0/guide/listeners-chapt.html

这在 HSQLDB 指南http://hsqldb.org/doc/2.0/guide/listeners-chapt.html 中有介绍

The server is started with this command:

服务器使用以下命令启动:

java -cp ../lib/hsqldb.jar org.hsqldb.Server --database.0 mem:test --dbname.0 test

You need to create a Spring data source with username "SA" and password "". The database driver and URL (from the same machine) to configure the Spring data source are:

您需要使用用户名“SA”和密码“”创建一个 Spring 数据源。配置Spring数据源的数据库驱动和URL(来自同一台机器)是:

org.hsqldb.jdbcDriver
jdbc:hsqldb:hsql://localhost/test

回答by emilan

I recomend you to use external Database, but just in case if you want to use HSQL, then this may will help you http://java.dzone.com/articles/spring-3-makes-use-embedded-easy

我建议您使用外部数据库,但以防万一如果您想使用 HSQL,那么这可能会对您有所帮助http://java.dzone.com/articles/spring-3-makes-use-embedded-easy

回答by Aravind A

Embedded-database is an in memory DB and Spring supports HSQL, H2, and Derby . You could go to their respective site for the connection details .

嵌入式数据库是内存数据库,Spring 支持 HSQL、H2 和 Derby。你可以去他们各自的网站了解连接详情。

For H2 see here. For HSQL see hereand here.

对于 H2,请参见此处。对于 HSQL,请参见此处此处

As far as I understand , the

据我了解,

<jdbc:embedded-database id="dataSource" type="HSQL">
  <jdbc:script location="classpath:scheme.sql" /
</jdbc:embedded-database>

uses an in-memory DB and so is not accessible externally . You'll be able to access this within the same VM and same class loader .

使用内存数据库,因此无法从外部访问。您将能够在同一个 VM 和同一个类加载器中访问它。

回答by Tanmay Saha

you can do like this

你可以这样做

final ApplicationContext ctx = new ClassPathXmlApplicationContext("dao-context.xml");
final DataSource dataSource = (DataSource)ctx.getBean("dataSource");
final Connection conn = dataSource.getConnection();

回答by Aaron

You can connect to the embedded database in the normal fashion, (SQL Developer, SQL Explorer etc); I used my debugger to look at the URL property in the embedded database bean I created with Spring, in your case dataSource. I would think your url would be something along the lines of jdbc:hsqldb:mem:dataSource.

您可以以正常方式(SQL Developer、SQL Explorer 等)连接到嵌入式数据库;我使用我的调试器查看我用 Spring 创建的嵌入式数据库 bean 中的 URL 属性,在你的例子中是 dataSource。我认为您的网址将类似于jdbc:hsqldb:mem:dataSource.

回答by gajos

For some people a sufficient solution would be to use the h2 console - as described here:

对于某些人来说,足够的解决方案是使用 h2 控制台 - 如下所述:

spring boot default H2 jdbc connection (and H2 console)

spring boot默认H2 jdbc连接(和H2控制台)

You must only remember to set hsqldb drivers where needed. This way the database doesn't have to be started separately. You also don't have to install any additional software to browse it.

你只需要记住在需要的地方设置 hsqldb 驱动程序。这样数据库就不必单独启动。您也不必安装任何其他软件来浏览它。