I am using microsoft sql server,
If i open intellij, open new database connection in database tool window =>
and in advanced type i add:
The connection works. However now i want to connect to this ms sql server with spring jpa. So what i am using is:
spring.datasource.url=jdbc:jtds:sqlserver://<host>:<port>;instance=<instance>;domain=<domain>;useNTLMv2=true
spring.datasource.username=<user>
spring.datasource.password=<password>
spring.datasource.driverClassName=net.sourceforge.jtds.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
And it just says "login for user failed"
In my pom i am using:
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.jpa.version}</version>
</dependency>
<dependency>
<groupId>jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2</version>
</dependency>
Why does it work with database tool window, but not with jpa?
Thanks for help!
Your are mixing JDBC driver connecting with sprig data JPA connection, for JPA proper connection you can change the drive class name to
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
Then the dialect can be
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
And finally add the proper url and remove JDBC driver relavent dependencies
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=<dbname>
Remaining properties will be same as your, see both examples in here .
Related
I want my Quarkus app to connect to both a PostgreSQL database and a MySQL database.
I've researched through https://quarkus.io/guides/datasource#multiple-datasources and https://quarkus.io/guides/hibernate-orm#multitenancy, but could not figure out if this is possible.
my application.properties is as below;
quarkus.datasource.postgresql.db-kind=postgresql
quarkus.datasource.postgresql.username=xyz
quarkus.datasource.postgresql.password=xyz
quarkus.datasource.postgresql.jdbc.url=jdbc:postgresql://localhost:5432/xyz
quarkus.datasource.mysql.db-kind=mysql
quarkus.datasource.mysql.username=xyz
quarkus.datasource.mysql.password=xyz
quarkus.datasource.mysql.jdbc.url=jdbc:mysql://localhost:3305/xyz
my pom.xml has both;
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
Is it possible to connect to multiple different types of database from Quarkus?
How do I persist() an Entity to the correct database if such capability exists?
Thank you!
What you are asking for is supported, see this part of the datasource documentation and this part of the Hibernate ORM documentation.
I am implementing a program in Spring-Boot using ObjectDB. To actually use ObjectDB I have followed this approach which is working perfectly.
However, as soon as I want to use `spring-boot-starter-web`` then I am getting the following errors:
dataSource or dataSourceClassName or jdbcUrl is required. at
com.zaxxer.hikari.HikariConfig.validate
I have been fiddling around with parameter jdbc-url in the properties file as mentioned in many posts. Tried to exclude Hikari because probably ObjectDB uses his own connection pooling mechanism. But without any results.
Any ideas on how to solve this error?
I am using the exact same code as in the link. I have added Spring-Actuator in the pom like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
In some cases, frameworks that use JPA require specifying a JDBC connection details, including a JDBC driver, which is then passed to the JPA implementation and used by it to access the database. ObjectDB is a JPA implementation that does not access an external database, and therefore, does not need or use a JDBC driver.
As discussed in the comments to the question, a simple workaround is to specify a dummy JDBC driver, which will be passed to ObjectDB and then ignored. It does look weird, but this is the way to go until either ObjectDB implement its own JDBC driver, or the relevant frameworks become more flexible regarding their request of a JDBC driver.
Have you tried this (in the pom.xml?):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.objectdb</groupId>
<artifactId>objectdb</artifactId>
<version>2.8.4</version>
</dependency>
And don't forget to add the database connection in application.properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=mydbuser
spring.datasource.password=mydbpass
spring.datasource.url=jdbc:....
See https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa for more details.
I'm a beginner at java and i'm developing a big app the big issue is that i need a big database for which i should make a server side application(which i don't know how) to connect to Postgres. I did not find anything about making it and connecting it to PostgreSQL to Android Studio for instance.. I'm really confused and can't find anything about the topic. Excuse my bad English.
I recomend to see this example https://spring.io/guides/gs/accessing-data-mysql/
Mysql is being used, but you could simple change the "jdbc driver" to connect with PostgreSQL
First change driver in pom.xml
this
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
to
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
and then replace the url in src/main/resources/application.properties with the connection info of your postgres instance.
spring.datasource.url=jdbc:postgresql://host:port/database
also add this line to the properties:
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
See https://github.com/spring-projects/spring-boot/issues/12007
On postgres user is a reserved keyword, so you have to change the name of the table that maps with User entity:
import javax.persistence.Table;
#Entity // This tells Hibernate to make a table out of this class
#Table(name = "users")
In this way you have the example working.
If not needed to do in Java, you can do it on other languages and frameworks, but the main idea would be to have an API and fetch or post data from/to.
I leave you some useful links to have more information
JDBC https://en.wikipedia.org/wiki/Java_Database_Connectivity
PostgreSQL JDBC Driver : https://github.com/pgjdbc/pgjdbc
ORM: https://en.wikipedia.org/wiki/Object-relational_mapping
Hibernate, a Java ORM http://hibernate.org/orm/
REST: https://spring.io/understanding/REST
The case is that I have launched both Spring Cloud Data Flow Server and Shell apps in my local machine, and they all used the H2 memory database to store the tasks and jobs defination.
When I deployed the application that Used a H2 as the database to read data, it works perfect fine.
But when I want to deploy and run the application that read data from a local postgresql database, it just could not find it, and will then use the H2 again.
I have add postgresql dependency in my pom and also configure the properties in the application.properties , it works fine with spring batch in my local Intelij.
So my question is that how could I make my application still read data from the postgresql after it been deployed in the spring-cloud-data-flow-server? Thanks.
Configuration info:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version
<spring.cloud.task.version>2.0.0.RELEASE</spring.cloud.task.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-core</artifactId>
<version>${spring.cloud.task.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-batch</artifactId>
<version>${spring.cloud.task.version}</version>
</dependency>
You can control what datasource should be configured to SCDF, Tasks, Task-launcher, and even Composed Task Runner. The documentation for these options could be useful.
In your case, though, it sounds like you have 2 different databases: one for SCDF and the other for the Task that SCDF launches, which should be a pretty straightforward setup as long as the datasource properties are configured correctly on both the Boot apps.
It could be that either there's no Postgres driver in the classpath or the datasource properties aren't configured correctly. Review the docs of the connection properties for the supported databases - you could double check for correctness.
Is there a way to change database in application that is almost done? I am encoutering many problems with H2 that does not occur in MySQL. For example ALTER TABLE yourtable AUTO_INCREMENT = 1; does not work and instead I had to use restart at which does not work as good as MySQL version. Also now I am having problems with datediff. So is it possible to change database in ongoing application?
Yes you can.
Include dependencies for MySql in your pom file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Create your repository interface for mysql that extends JpaRepository:
public interface SqlDAO extends JpaRepository<YourPOJO,Long>{
// you can use JpaRepository methods out of the box or write custom ones
}
Add properties for your sql, you can use .properties or .yml files. I use yaml:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/coolDB
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
Don't forget to run MySql database itself and you good to go.
Your Service now should be using your repository interface to communicate with Sql.
Here is a link for Jpa documentation and how to create your custom methods:
https://docs.spring.io/spring-data/jpa/docs/1.4.1.RELEASE/reference/html/jpa.repositories.html
Edit: you have to create database in mysql console manually, Spring won't do it for you. You can include .sql file into your resource directory to create dummy data or set sql settings further on, Spring will run that for you.