I am expirencing a problem with the JDBC driver. My database is at postgres://... but the driver only works with postgresql://.... What should I do?
I am trying to connect to a heroku postgres database from java.
CODE:
Connection conn = null;
Below I am trying with postgresql://
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://bfjxvagjalvmcu:n9Y2VO0myQ5sgWVrzJbLX59TD3#ec2-54-204-15-41.compute-1.amazonaws.com:5432/*****&username=******&password=******&ssl=true");
And I get the error:
org.postgresql.util.PSQLException: The connection attempt failed.
And if I use this code, with postgres
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgres://bfjxvagjalvmcu:n9Y2VO0myQ5sgWVrzJbLX59TD3#ec2-54-204-15-41.compute-1.amazonaws.com:5432/****&username=******&password=****&ssl=true");
I get the error:
java.sql.SQLException: No suitable driver found for jdbc:postgres://bfjxvagjalvmcu:n9Y2VO0myQ5sgWVrzJbLX59TD3#ec2-54-204-15-41.compute-1.amazonaws.com:5432/*******&username=********&password=*******&ssl=true
Related
This question already has answers here:
java.net.ConnectException: Connection refused
(19 answers)
Closed 1 year ago.
I'm using Eclipse to connect to a remote database with the following details:
name: MSbdd**
Hostname 155.158.xxx.xx
Port: 1521
SID: olt*****
And the authentication type: Default
username: msbd**
password: haslo****
This is the code I have in Eclipse:
package net.codejava;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JavaOracleTest {
public static void main(String[] args) {
String dbURL = "jdbc:oracle:thin:#localhost:1521:xe";
String username = "msbd**";
String password = "haslo****";
try {
Connection connection = DriverManager.getConnection(dbURL, username, password);
System.out.println("Połączono z serwerem Oracle");
} catch (SQLException e) {
System.out.println("Error");
e.printStackTrace();
}
}
}
And this is the error I get:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=yV+3U5v4TK2js7gMFTixxA==)
Error
at oracle.jdbc.driver.T4CConnection.handleLogonNetException(T4CConnection.java:882)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:687)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:1086)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:90)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:728)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:649)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.JavaOracleTest.main(JavaOracleTest.java:15)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection (CONNECTION_ID=yV+3U5v4TK2js7gMFTixxA==)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:677)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:568)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:953)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:350)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:2155)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:652)
... 7 more
Caused by: java.io.IOException: Connection refused: connect, socket connect lapse 2003 ms. localhost 1521 0 (2/2) true
at oracle.net.nt.TcpNTAdapter.establishSocket(TcpNTAdapter.java:421)
at oracle.net.nt.TcpNTAdapter.doLocalDNSLookupConnect(TcpNTAdapter.java:303)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:265)
at oracle.net.nt.ConnOption.connect(ConnOption.java:238)
at oracle.net.nt.ConnStrategy.executeConnOption(ConnStrategy.java:902)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:638)
... 12 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:482)
at java.base/sun.nio.ch.Net.connect(Net.java:474)
at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:694)
at java.base/java.nio.channels.SocketChannel.open(SocketChannel.java:194)
at oracle.net.nt.TimeoutSocketChannel.connect(TimeoutSocketChannel.java:184)
at oracle.net.nt.TimeoutSocketChannel.<init>(TimeoutSocketChannel.java:158)
at oracle.net.nt.TcpNTAdapter.establishSocket(TcpNTAdapter.java:380)
... 17 more
I've replaced some details with * because it's a database from school.
Your error is:
Caused by: java.net.ConnectException: Connection refused: connect
Check that you have:
The correct hostname.
For example, you say that the hostname is 155.158.xxx.xx but you are using:
String dbURL = "jdbc:oracle:thin:#localhost:1521:xe";
Should it be:
String dbURL = "jdbc:oracle:thin:#155.158.xxx.xx:1521:xe";
the correct port.
the correct SID.
For example, should it be:
String dbURL = "jdbc:oracle:thin:#155.158.xxx.xx:1521:olt*****";
the correct username and password.
access to the database.
I.e. you are running it on a network that can access the server and not from a network that cannot access the server (your school may require you to be directly connected to or logged in to their network to access the server and may refuse connections from unauthenticated users outside their network).
When trying to issue "SHOW POOLS" or any stats query command on pgbouncer database via JDBC, facing the below exception.
org.postgresql.util.PSQLException: ERROR: unsupported pkt type: 80
at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:406)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:286)
JDBC code:
String connectionUrl = "jdbc:postgresql://"+ipaddress+":"+port+"/"+database;
con = DriverManager.getConnection(connectionUrl, userName, password);
statement = con.createStatement(); statement.executeQuery("SHOW POOLS");
JDBC Driver Version: 42.2.14 ;
PgBouncer Version: 1.14.0 ;
Postgres Version: 11.4;
PS:
Manually able to connect to pgbouncer database and issue all pgbouncer admin commands like SHOW POOLS or SHOW STATS. Just not able to execute the same from JDBC.
JDBC use extended query protocol by default, try simple protocol for such query
String connectionUrl = "jdbc:postgresql://"+ipaddress+":"+port+"/"+database+"?preferQueryMode=simple";
PGBouncer currently supports only the simple protocol - the packet type 80 is for 'Parse', which is the first step in the extended protocol. The message you see in the exception PSQLException actually comes from PGBouncer.
I'm trying to establish an SSL connection to a Sybase ASE 16.0 using JDBC driver. On the server, through interactive SQL, I am able to access the ASE server with SSL. I am using Jconnect 4 (jconn4.jar) and JDK 8 with the following connection string: jdbc:sybase:Tds:host:port/dbname?ENABLE_SSL=true. Server certificate imported to the java application trust store using keytool. I have gone through the other question already asked here but it doesn't help me to rectify this problem. Test code is below.
try {
Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
Connection con = DriverManager.getConnection(
"jdbc:sybase:Tds:host:5000/master?ENABLE_SSL=TRUE", "sa",
"password");
System.out.println("*********Connected**********");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select ##ssl_ciphersuite");
int i = 1;
while (rs.next()) {
System.out.println("Row: " + i);
System.out.println("ID: " + rs.getString(1).trim());
System.out.println(" ");
i = i + 1;
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
My JDBC code throwing below exception.
The connection should be established over the secure network but facing below error.
java.sql.SQLException: JZ00L: Login failed. Examine the SQLWarnings chained to this exception for the reason(s).
at com.sybase.jdbc4.jdbc.SybConnection.getAllExceptions(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.handleSQLE(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.handleHAFailover(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc4.jdbc.SybDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.testing.Test.main(Test.java:41)
I checked the Sybase log see the following error:
kernel SSL or Crypto Error Message: 'The SSL handshake failed. Root error: error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol'.
I went through the multiple Sybase documentation but no luck. Any help will be much appreciated. Thanks in advance!!
I try to connect to Azure SQL with:
import java.sql.*;
public class ExampleJTDS {
public static void main(String[] args) {
// Setting.
String connectionUrl = "jdbc:jtds:sqlserver://SERVER.database.windows.net:1433/DATABASE;ssl=off";
String user = "USER#SERVER";
String pass = "PASSWORD";
// Declare the JDBC object.
Connection conn = null;
try {
// Establish the connection.
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(connectionUrl, user, pass);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
But I am getting:
java.sql.SQLException: I/O Error: DB server closed connection.
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2481)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:632)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:371)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at run.ExampleJTDS.main(ExampleJTDS.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
If I force the encryption by substituting ssl=off with ssl=require, I am getting:
java.sql.SQLException: Network error IOException: Connection reset
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at run.ExampleJTDS.main(ExampleJTDS.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Interestingly, I can connect to the database from the same computer and with the same JDBC driver with SQuirreL SQL (although without SSL - SQuirreL SQL manages to put the credentials into the first TDS packet and Azure accepts that). Hence, the problem should not be in the setting of firewalls.
Metadata:
Server: Azure V12
Driver: jtds-1.3.1
JRE: 1.8.0_72-b15 (from Oracle)
_JAVA_OPTIONS: -Djsse.enableCBCProtection=false
security.provider.1: sun.security.provider.Sun
OS: OS X 10.11.5
SQuirreL SQL: 3.7.1
How can I connect to Azure SQL from Java?
Per my experience, I think the issue was caused by the connection string which is the variable connectionUrl of your code. I have answered your similar question of the SO thread, please see How to connect to Azure SQL with JDBC.
However, using jTDS instead of Microsoft JDBC driver for SQL Server has a little difference, you can refer to a note in the step 3 of the tutorial to know it. As reference, I post the contento of the note here.
Note:
If you are using the JTDS JDBC driver, then you will need to add "ssl=require" to the URL of the connection string and you need to set the following option for the JVM "-Djsse.enableCBCProtection=false". This JVM option disables a fix for a security vulnerability, so make sure you understand what risk is involved before setting this option.
Hope it helps. Any concern, please feel free to let me know.
I want to connect both Oracle and DB2 databases for some reason using JDBC. In main class when I try to connect Oracle, Connection is successfull but DB2 connection is NOT successfull giving this error: "Yuva acilirken hata olustu" means "Socket can not be opened" . What can be the problem??? Oracle works but DB2 does not work. I checked all password, usernames, host names and ports again and again for DB2.
COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0616E Yuva açılırken hata oluştu. SQLSTATE=08S01
at COM.ibm.db2.jdbc.net.SQLExceptionGenerator.socketException(Unknown Source)
at COM.ibm.db2.jdbc.net.DB2Connection.create(Unknown Source)
at COM.ibm.db2.jdbc.net.DB2Connection.<init>(Unknown Source)
at COM.ibm.db2.jdbc.net.DB2Driver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:590)
at java.sql.DriverManager.getConnection(DriverManager.java:232)
To get connection, I write these in main,
connORA = DirectConnection.getOracleConnection();
connDB2 = DirectConnection.getDB2Connection();
My Connection Class I defined Oracle COnnection and DB2 connection as follows: (Maybe there is a problem in DB2 Connection Method? Oracle connects succesfully)
public static Connection getOracleConnection() throws SQLException, ClassNotFoundException{
return getConnection("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:#host:port:name", "username", "password");}
public static Connection getDB2Connection() throws SQLException{
return getConnection("COM.ibm.db2.jdbc.net.DB2Driver", "jdbc:db2://host:port:name","username", "password");}
Firstly, as #ThePhantom05 mentioned, you should be using the JCC driver, not the older net driver. The correct URL pattern for a DB2 JDBC connection will be jdbc:db2://host:port/database_name
Try using this as your driver instead:
String driver = "com.ibm.db2.jcc.DB2Driver";