Cannot create JDBC driver of class '' for connect URL - java

I am trying to connect from my java app running on a Google Cloud Run instance, to a Google Cloud Sql instance that is part of the same Google Cloud project.
When my app tries to open a connection to the db, it throws the following error...
org.jdbi.v3.core.ConnectionException: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:google:mysql://my-gc-app:my-cloud-sql-instance/mydb'
I have enabled all the correct roles for the cloud run service to connect to the cloud sql instance.
The java code that is establishing the DataSource is as follows...
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:google:mysql://my-gc-app:my-cloud-sql-instance/mydb");
dataSource.setUsername(mySqlUser);
dataSource.setPassword(mySqlusersPassword);
dataSource.setMinIdle(5);
dataSource.setMaxIdle(10);
dataSource.setMaxOpenPreparedStatements(100);
Database.instantiate(dataSource);

You're missing a line that specifies which DB type should be used.
For example if you're going to connect to Mysql server.
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
If it leads to a driver not found error or sth like that, try adding the library with the keyword mysql:mysql-connector-java:8.0.12

Related

Azure SQL database connection issue from an Azure VM by Java JDBC

We have a java project in an Azure virtual machine (VM), and need connect to Azure SQL db by JDBC connection, so we use the JDBC connection string provided by Azure SQL db as follows:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
but we got an exception:
"java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.
The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net."
then we updated the JDBC connection string to:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cr2.eastus1-a.control.database.windows.net;loginTimeout=30;"
but we got another exception:
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Cannot open server "ZZZ1" requested by the login. The login failed.)
where "ZZZ1" is our username for the Azure VM.
Any idea to solve this issue?
By default, Azure VM has not blocked the outbound connection to the Internet. You could first verify the network connectivity from your VM to the Azure SQL database via running the command telnet ZZZdbserver.database.windows.net 1433 in CMD or Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433 in Powershell. If it fails, you may check if there is any firewall blocking this database connection or port or your application is listening on that port or your SQL database is online.
Additionally, if the network connectivity has succeeded, you could verify if the username or password is correct when you are using to connect to your database. Make sure there is not any typo. Also, the username should have enough privileges to access this database. You can try to access this database via Server admin login in the properties of the SQL database. Moreover, you could refer to this sample code to use Java to connect to access the Azure SQL database.
When using the Microsoft JDBC Driver for SQL Server to connect to an Azure SQL Database. You should note this:
Appending the server name to the userId in the connection string
Prior to the 4.0 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you were required to append
the server name to the UserId in the connection string. For example,
user#servername. Beginning in version 4.0 of the Microsoft JDBC Driver
for SQL Server, it's no longer necessary to append #servername to the
UserId in the connection string.
Using encryption requires setting hostNameInCertificate
Prior to the 7.2 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you should specify
hostNameInCertificate if you specify encrypt=true (If the server name
in the connection string is shortName.domainName, set the
hostNameInCertificate property to *.domainName.). This property is
optional as of version 7.2 of the driver.
Hope this helps.
If I need set encrypt=true, and hostNameInCertificate=cr2.eastus1-a.control.database.windows.net. Where do I need get the certificate for cr2.eastus1-a.control.database.windows.net from Azure SQL DB's service?

Sql server jdbc driver trying to connect via wrong username. JDBC using integrated security/windows authentication and username,password

I have upgrade from JTDS driver to Sql server JDBC driver 4.1 and trying to connect to Sql server database using following connection string
jdbc:sqlserver://<System_IP>;instance=MSSQLSERVER;domain=domainname;IntegratedSecurity=true;sendStringParametersAsUnicode=false;selectMethod=cursor;user=administrator;password=pass
It's failing with error message:
Login failed for user domainname/current_machinename.
This connection was working fine till I upgraded to JDBC driver 4.1
Why JDBC is trying to connect via domainname/current_machinename. It should have been domainname/administrator which I am passing in connection string.

Connect to postgresql instance on Cloud SQL: The Google Cloud SQL API is not enabled for project

I have deployed on appengine a basic java web app that is supposed to connect to a postgresql instance on Cloud SQL (works locally).
After deployment It (wrongly?) raise this exception when I browse the page that tries to connect to the DB:
org.postgresql.Driver connect: Unexpected connection error: (Driver.java:271)
java.lang.RuntimeException: The Google Cloud SQL API is not enabled for project [ad*****manager-XXXXXXX]. Please use the Google Developers Console to enable it: https://console.cloud.google.com/apis/api/sqladmin/overview?project=ad*****manager-XXXXXXX
I have checked that the Google Cloud SQL API is enabled as you can see on
that screenshot
I have granted the cloud sql editor rights to my app on the project that hosts the instance (Note that the postgresql instance and the web app live in two separate projects.)
It is a flex environment app
Here is the part of the code that fails:
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(dburl, dbuser, dbpwd);
And the connection url:
jdbc:postgresql://google/name_of_my_database?useSSL=false&socketFactoryArg=ad*****manager-XXXXXXX:europe-west1:XXXX-pgdb-preprod&socketFactory=com.google.cloud.sql.postgres.SocketFactory
Actually I had to enable the SQL API not in the project that hosts the DB as said in the error message but in the project that hosts the application
To do so, go in the API section, choose library and search for SQL API (see screenshot below)
screenshot

Error occured while connecting to cloud sql from android studio

I am trying to connect to cloud sql from app engine using cloud back-end java servlet in android studio. But I got error in getting connection from DriverManager.
Connection conn = DriverManager.getConnection("jdbc:google:mysql://<id:<sql-id>/database",
"myuser","password");
and driver is
Class.forName("com.mysql.jdbc.GoogleDriver");
Please assist me if fixing this error...
The com.mysql.jdbc.GoogleDriver driver and jdbc:google:mysql connection string only work from Google App Engine. When connecting from your development workstation you should use the plain MySQL java connector (ie com.mysql.jdbc.Driver and jdbc:mysql). You will need to authorize you workstation's IP address and add an IP address to your Cloud SQL instance to connect to.
For more information follow the guide at https://cloud.google.com/sql/docs/external.
Actually i got it,problem is with some_user database permission for using database in appengine(Localhost for cloud sql database).
FIX:
Type code in cloud sql database and execute
grant all privileges on databasename.* to 'user'#'localhost' identified by 'password';
This should help.........

Exception while connecting to DB2 in java using JDBC

I am trying to connect to a db2 database in Java. Below the driver and the connection string and the driver details i am giving
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://hostname:portnumber/databasename";
sourceConnection=DriverManager.getConnection(url,"username","password");
But I am getting the below exception
"COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0615E Error receiving from socket, server is not responding. SQLSTATE=08S01"
I also tried changing the connection string to
String url="jdbc:db2:hostname:portnumber/databasename";
Still it is resulting the same exception above while trying to get the Connection.
And i have tried the below option also using JDBC app driver
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
DB2DataSource db2ds = new DB2DataSource();
db2ds.setServerName("hostname");
db2ds.setPortNumber(portnumber);
db2ds.setDatabaseName("databasename");
db2ds.setUser("username");
db2ds.setPassword("password");
sourceConnection=db2ds.getConnection();
For the above two connection I used the jar "db2java.jar"
And i have tried using the JCC driver:
Class.forName("com.ibm.db2.jcc.DB2Driver");
String url="jdbc:db2://hostname:portnumber/databasename";
sourceConnection=DriverManager.getConnection(url,"username","password");
For this connection i have added the below jars
1)db2jcc.jar
2)db2jcc_license_cu.jar
This time around I am getting the below error,
"com.ibm.db2.jcc.am.go: [jcc][t4][201][11237][3.57.82] Connection authorization failure occurred.
Reason: Security mechanism not supported. ERRORCODE=-4214, SQLSTATE=28000"
I tried to connect to the same db2 source using "Quest for DB2" tool and the connection was successful.
Am i missing something in the code and is it a problem with DB2 drivers or connection string?
Can someone please guide me.
Thanks in advance.
Cause:
If the DB2® instance where InfoSphere Optim Performance Manager is running has the authentication configuration parameter set to DATA_ENCRYPT, you cannot log in to the web console.
Resolving the problem:
Do the following steps:
On the DB2 instance where Optim Performance Manager is running, set the authentication configuration parameter to SERVER by issuing the following command:
db2 update dbm cfg using authentication server
Restart the DB2 instance and InfoSphere Optim Performance Manager.
For more details visit here.
Your first two attempts were not supposed to work. You're using the JCC driver URL format, so it wouldn't be valid for either "net" or "app" drivers, which are deprecated anyway.
Use the JCC driver (com.ibm.db2.jcc.DB2Driver) and the URL format of "jdbc:db2://hostname:portnumber/databasename" and see this technote for the solution to the "Security mechanism not supported" problem. In short, you need to use a supported JDK.

Categories

Resources