Java application is not connecting with Postgres Database - java

I recently setup a java application in my spring-boot. I'm getting this error when i run it:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225)
at org.postgresql.Driver.makeConnection(Driver.java:465)
at org.postgresql.Driver.connect(Driver.java:264) ........
Caused by: java.net.UnknownHostException: postgres ........
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-02-03 17:24:15 - Application run failed
My application-dev.properties file in src/main/resources/ has this code:
spring.datasource.url=jdbc:postgresql://localhost:5432/sofra_hybrid_7?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=postgres
spring.datasource.password=admin
I have made the database too as well in the postgres server with username "postgres" and pass "admin:
lalarukh=# CREATE DATABASE sofra_hybrid_7;
CREATE DATABASE
lalarukh=# grant all privileges on database sofra_hybrid_7 to postgres ;
GRANT
I have even also used the jdbc postgres jar file inside the application to make the connection but all in vain.
Seeking Help!
I can't run my java application as postgres can't make connection with my app in spring boot.

I also faced the same issue and after a lot of working I finally resolved it this way:
As you're receiving unknown host means that your application can't detect your this file: application-dev.properties
You shall make this application.yml inside your project (yourprojectdirectory/application.yml) and this will automatically detects the DB information and all.
As you're using postgres as DB so, add this code inside this file:
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql:db_name
username: your-postgres-username
password: your-postgres-password
Run this command for application running: mvn spring-boot:run -Dspring-boot.run.profiles

Related

How to connect to MySQL container on Docker using Spring Boot project

I am trying to run a Spring Boot application where db has to be connected to a MySQL container on Docker. I used following code and commands.
Application properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://mysqldb:3306/practice
spring.datasource.username=root
spring.datasource.password=CVnit1234!
spring.jpa.show-sql=true
spring.sql.init.platform=mysql
spring.sql.init.mode=always
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
Commands used for creating container & running it on Docker:
Pulling mysql image from Docker Hub:
docker pull mysql
Creating a container:
docker run --name mysqldb --network spring-net -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password MYSQL_DATABASE=practice -d mysql:latest
After this command, a container is created and even it is up and working fine. I was able to login to sql bash and execute some queries.
I tried with multiple jdbc urls:
jdbc:mysql://mysqldb:3306/practice
jdbc:mysql://mysqldb:3307/practice
jdbc:mysql://mysqldb/practice
jdbc:mysql://host.docker.internal:3306/practice
jdbc:mysql://host.docker.internal:3307/practice
None of this worked, but I was able to connect to localhost:3306 and localhost:3307.
Below is my spring boot code. When I am trying to connect through the Spring Boot application, I was getting the following error.
com.mysql.cj.jdbc.exceptions.CommunicationsException:Communication link failure
The problem over here is when I am trying to connect to localhost the application is up and working, but when trying to connect to mysql container, I was not able to connect.
Check your network to ensure that the application network is connected to the MySQL network

Play! framework 2.7 - Cannot connect to database

i am trying to connect to my postgres database from my play 2.7 application.
The database is an docker image that is initialized with
docker run -d --name backend_local - p 127.0.0.1:5432:5432 -e POSTGRES_USER=markos -e POSTGRES-PASSWORD=**** -e POSTGRES_DB=backend_db --restart always registry.gitlab....
My configuration file looks like this
# Database
db.default.driver=org.postgresql.jdbc.Driver
db.default.url="jdbc:postgresql://localhost:5432/backend_db"
db.default.username="markos"
db.default.password="****"
db.default.logSql=true
cant see something wrong with the connection but still i am getting Cannot initialize to database[default]
the logs i am getting
2020-05-23 15:44:13.044+0200 [error] application -
! #7fpl591k9 - Internal server error, for (GET) [/] ->
play.api.Configuration$$anon$1: Configuration error[Cannot initialize to database [default]]
at play.api.Configuration$.configError(Configuration.scala:168)
at play.api.Configuration.reportError(Configuration.scala:1014)
at play.api.db.DefaultDBApi.$anonfun$initialize$1(DefaultDBApi.scala:76)
at scala.collection.immutable.List.foreach(List.scala:389)
at play.api.db.DefaultDBApi.initialize(DefaultDBApi.scala:68)
at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:93)
at play.api.db.DBApiProvider.get(DBModule.scala:77)
at play.api.db.DBApiProvider.get(DBModule.scala:59)
at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:85)
at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:77)
Caused by: play.api.Configuration$$anon$1: Configuration error[dataSource or dataSourceClassName or jdbcUrl is required.]
at play.api.Configuration$.configError(Configuration.scala:168)
at play.api.Configuration.reportError(Configuration.scala:1014)
at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:68)
at play.api.db.PooledDatabase.createDataSource(Databases.scala:248)
at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:140)
at play.api.db.DefaultDatabase.dataSource(Databases.scala:138)
at play.api.db.DefaultDBApi.$anonfun$initialize$1(DefaultDBApi.scala:72)
at scala.collection.immutable.List.foreach(List.scala:389)
at play.api.db.DefaultDBApi.initialize(DefaultDBApi.scala:68)
at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:93)
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:958)
at play.api.db.HikariCPConfig.toHikariConfig(HikariCPModule.scala:138)
at play.api.db.HikariCPConnectionPool.$anonfun$create$1(HikariCPModule.scala:55)
at scala.util.Try$.apply(Try.scala:209)
at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:52)
at play.api.db.PooledDatabase.createDataSource(Databases.scala:248)
at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:140)
at play.api.db.DefaultDatabase.dataSource(Databases.scala:138)
at play.api.db.DefaultDBApi.$anonfun$initialize$1(DefaultDBApi.scala:72)
at scala.collection.immutable.List.foreach(List.scala:389)
It seems that my local_dev.conf that was handling the database configurations was misplaced outside from the conf file where it should be located originally!

can not connect to mysql8 with correct password in my java project

I am using mysql 8.0.18 as my db, and I can log in the console with my password
but when I lanuch my springboot 2.1.3 project, and request the interface, the exception said that I can not get a jdbc connection,because "Access denied for user 'root'#'localhost' (using password: YES)", the mysql password I enter is the same when I enter in the console
I had tried to set the password in springboot properties or in comandline with -Dspring.datasource.password=xxx, but both of them did not work. I am using Window10.
Can somebody help me?
now,i add two config in my.ini files,and i can log in the mysql db without password in window console
but my springboot project still say 'Access denied for user 'root'#'localhost'',what can i do ?
here is the sprintboot yml pros
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
url: jdbc:MySQL://localhost:3306/maple_valley?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8

Correct JDBC Connection URL for a PostgreSQL DB in a Docker Container

I am using Gitpod cloud IDE which uses Docker images to provide a nice linux environment with VSCode IDE.
https://gitpod.io
It is possible to use a Docker file in Gitpod to install additional software in the environment. So by following Gitpod docs I installed PostgreSQL.
https://medium.com/gitpod/bring-your-own-docker-image-to-gitpod-52db1aa861de
I can now connect to the PostgreSQL with this command. Note that I had to provide the sockets folder to get this working.
psql -h ~/pg/sockets postgres
I have a spring boot application which should connect to the PostgreSQL DB. The connection URL is taken from an Environment variable.
export JDBC_DATABASE_URL=jdbc:postgresql://gitpod#127.0.0.1/postgres
And this is what I have in my properties file.
spring.datasource.url=${JDBC_DATABASE_URL}
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
this is what I get when I run \du on psql to list the users.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
gitpod | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
The Gitpod user logged in to this linux environment is named gitpod. ( I do not know the password )
When I run the command netstat -plunt | grep postgres I get the following output
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 955/postgres
But when I run my App it throws the following exception
Caused by: java.net.UnknownHostException: gitpod#127.0.0.1
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_202]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_202]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_202]
at org.postgresql.core.PGStream.<init>(PGStream.java:75) ~[postgresql-42.2.6.jar!/:42.2.6]
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.6.jar!/:42.2.6]
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.6.jar!/:42.2.6]
... 56 common frames omitted
I think it should be a problem with the Connection URL. Can someone please explain me how to get this working ?
Really appreciate your help.
Thanks.
I managed to get this working in gitpod with the help of the gitpod community.
Used the prebuilt postgres docker image by gitpod team as discribed in this doc to install postgreSQL.
https://www.gitpod.io/blog/gitpodify/#postgresql
And I changed the URL to the following
export JDBC_DATABASE_URL=jdbc:postgresql://localhost:5432/postgres?user=gitpod

Cannot connect spring-data with SQL server ( Failed to load the sqljdbc_auth.dll )

I'm using windows 10 , when I try to compile my project I get the following error :
c.m.s.jdbc.internals.AuthenticationJNI : Failed to load the sqljdbc_auth.dll
2016-05-11 02:18:00.558 ERROR 5424 --- [ost-startStop-1] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170) ~[sqljdbc4-2.0.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2338) ~[sqljdbc4-2.0.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929) ~[sqljdbc4-2.0.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) ~[sqljdbc4-2.0.jar:na]
What I tried so far :
1. copy the authsql dll in jre of intellij idea => nothing
2. copy the dll to system32 => JVM error ACCESS_VIOLATION
Sring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=MyTest;integratedSecurity=true;
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.ddl-auto=update
The connection works in Datagrip I can connect to the database without any problem but intellij I get this error is there a way to fix it?
Sring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=MyTest;integratedSecurity=true
try changing ports to 3306 and see what happens:
Sring.datasource.url=jdbc:sqlserver://localhost:3306;databaseName=MyTest;integratedSecurity=true

Categories

Resources