10

I have a maven project that connects to a database that already exists. however i wanted to change it so that if the database doesn't already exist. it gets created. so i added in this extra code ?createDatabaseIfNotExist=true to this line.

dataSource.url=jdbc:mysql://localhost/rays-database?createDatabaseIfNotExist=true

And i got a lot of errors, so my question is; is there any extra configuration that im missing in order to use ?createDatabaseIfNotExist=true

I also tried adding the port number which didn't make any difference which i didn't expect it to.

dataSource.url=jdbc:mysql://localhost:3306/rays-database?createDatabaseIfNotExist=true

here is my .properties file:

 dataSource.driverClassName=com.mysql.jdbc.Driver
 dataSource.url=jdbc:mysql://localhost/rays-database?createDatabaseIfNotExist=true
 dataSource.username=root
 dataSource.password=

 hibernate.dialect=org.hibernate.dialect.MySQLDialect
 hibernate.show_sql=true
 hibernate.hbm2ddl.auto=create
Shaun Lavelle
  • 133
  • 1
  • 1
  • 10

2 Answers2

4

Please check this Why is Hibernate not creating database for MySQL?. Hope it helps.What kind of errors you are getting?

Community
  • 1
  • 1
Vikas Sharma
  • 745
  • 9
  • 19
  • I have updated my question with the .properties file. seems pretty much the same. – Shaun Lavelle Oct 08 '15 at 10:44
  • i get errors like, something to do with memory leak, and something wrong with my sql syntax, Error creating bean with name 'configureDataSource' – Shaun Lavelle Oct 08 '15 at 10:48
  • Replace hibernate.dialect=org.hibernate.dialect.MySQLDialect with hibernate.dialect=org.hibernate.dialect.MySQL5Dialect – Vikas Sharma Oct 08 '15 at 10:51
  • i added the 5 to the dialect but still no change – Shaun Lavelle Oct 08 '15 at 10:53
  • WARNING: The web application [/RaysRentalSystem] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: – Shaun Lavelle Oct 08 '15 at 10:54
  • 1
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-database' at line 1 – Shaun Lavelle Oct 08 '15 at 10:55
  • remove '-' from rays-database in dataSource.url=jdbc:mysql://localhost/rays-database?createDatabaseIfNotExist=true – Vikas Sharma Oct 08 '15 at 10:57
1

You can try this one, you have to add port and delete this(-) on the db name.

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/DbName?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&useUnicode=yes&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
Salil Das
  • 11
  • 1