如何在使用springboot/JPA和SQL Server时,如果数据库不存在就创建数据库?

huangapple go评论76阅读模式
英文:

How to create a database if it does not exist with springboot/JPA and SQL Server?

问题

我很惊讶我还没有找到一个Stack Overflow的问题来回答这个。我正在尝试将一个Spring Boot/JPA应用程序连接到我的本地机器上的SQL Server。我已经设置了应用程序,使其能够连接到一个现有的数据库,但如果我将JDBC URL更改为在数据库不存在时创建数据库,那么它就会失败。以下是在失败时属性的样式。

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testing;createDatabaseIfNotExist=true;
spring.datasource.username=hello
spring.datasource.password=Hello1234
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.show-sql=true

以下是启动应用程序时我收到的错误片段:

> com.microsoft.sqlserver.jdbc.SQLServerException: 登录用户
> 'hello' 失败。ClientConnectionId:971a3369-258b-4713-bddc-cda559b9fe94 at
> com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
> ~[mssql-jdbc-8.4.0.jre11.jar:na] at
> com.microsoft.sqlserver.jdbc.TDSTokenHandle

如果有人对我如何更改这个问题有任何想法,使得在数据库不存在时也能创建数据库,我将非常感谢。提前感谢。

英文:

I am surprised I haven't found an SO question that answers this. I am trying to connect a springboot/JPA application to an SQL Server on my local machine. I have the application setup so that it can connect to a database if it it exists, but if I change the JDBC URL to create the database if it doesn't exist then it fails. Here is what the properties look like when it fails.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testing;createDatabaseIfNotExist=true;
spring.datasource.username=hello
spring.datasource.password=Hello1234
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.show-sql=true

Here is a snippet of the error I receive when starting the app:

> com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
> 'hello'. ClientConnectionId:971a3369-258b-4713-bddc-cda559b9fe94 at
> com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
> ~[mssql-jdbc-8.4.0.jre11.jar:na] at
> com.microsoft.sqlserver.jdbc.TDSTokenHandle

If anybody has any thoughts as to how I can change this so the database is created if it does not exist I would be very thankful. Thanks in advnace.

答案1

得分: 2

可以肯定地说,您可以使用JPA自动创建一个数据库,方法如下:

spring.datasource.url=jdbc:sqlserver://localhost:1433
/testing?createDatabaseIfNotExist=true
英文:

Yes, definitely you can auto-create a database with JPA for that

spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testing;
createDatabaseIfNotExist=true;

line should be changed to:

spring.datasource.url=jdbc:sqlserver://localhost:1433
/testing?createDatabaseIfNotExist=true

答案2

得分: 1

我不认为可以使用JPA来创建数据库。
必须手动创建或以其他方式创建,但JPA不会为您执行此操作。

而且,使用应用程序自身来创建数据库,并且使用相同的凭据,这也是一种不良的做法。

英文:

I don't think a database can be created using JPA.
It has to be created manually or in some other ways, but JPA won't do that for you.

And it would be a bad practice as well to create the database using the application itself, and the use of same credentials.

答案3

得分: 0

在实际应用中,你的应用程序通常不应该创建数据库,因此在大多数情况下并不是真正的问题(除了像sqlite3这样的小型数据库)。不同的数据库也会以不同的方式处理这种情况。

在你的情况下,我在文档中并没有看到这个作为有效的jdbc参数。我建议提前使用一个与你的应用程序用户分开的特权用户创建数据库。

英文:

In practice your application should never create your database so its not really a problem most of the time(Outside small databases like sqlite3). Different databases would handle this situation differently as well.

In your case I do not see this as a valid jdbc parameter in the documentation.
I would recommend creating the database in advance with a privileged user separate from your application user.

huangapple
  • 本文由 发表于 2020年9月9日 04:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63801402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定