I am trying to connect to the remote database using JDBC and JSCH. To achieve that, I am using this tutorial: Connect to Remote database Note that it provides the connection for MySQL, so I change driver name to:
Class.forName("org.postgresql.Driver");
and URL:
DriverManager.getConnection("jdbc:postgresql://localhost:"+nLocalPort + "/", strDbUser, strDbPassword);
But I do get this type of error:
Caused by: java.io.EOFException
Here is the full stack trace:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at SQL.main(SQL.java:42)
Caused by: java.io.EOFException
at org.postgresql.core.PGStream.receiveChar(PGStream.java:308)
at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:405)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:94)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 7 more
With the same credentials I can connect to the database through IntelliJ. And Putty as well. But I can't make it with JDBC and JSCH. What am I doing wrong? I also read almost every question about this problem in the forum but I couldn't find the right solution.
The code itself from the link:
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class CTestDriver
{
private static void doSshTunnel( String strSshUser, String strSshPassword, String strSshHost, int nSshPort, String strRemoteHost, int nLocalPort, int nRemotePort ) throws JSchException
{
final JSch jsch = new JSch();
Session session = jsch.getSession( strSshUser, strSshHost, 22 );
session.setPassword( strSshPassword );
final Properties config = new Properties();
config.put( "StrictHostKeyChecking", "no" );
session.setConfig( config );
session.connect();
session.setPortForwardingL(nLocalPort, strRemoteHost, nRemotePort);
}
public static void main(String[] args)
{
try
{
String strSshUser = "ssh_user_name"; // SSH loging username
String strSshPassword = "abcd1234"; // SSH login password
String strSshHost = "your.ssh.hostname.com"; // hostname or ip or SSH server
int nSshPort = 22; // remote SSH host port number
String strRemoteHost = "your.database.hostname.com"; // hostname or ip of your database server
int nLocalPort = 3366; // local port number use to bind SSH tunnel
int nRemotePort = 3306; // remote port number of your database
String strDbUser = "db_user_name"; // database loging username
String strDbPassword = "4321dcba"; // database login password
CTestDriver.doSshTunnel(strSshUser, strSshPassword, strSshHost, nSshPort, strRemoteHost, nLocalPort, nRemotePort);
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:"+nLocalPort, strDbUser, strDbPassword);
con.close();
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
System.exit(0);
}
}
}
question from:
https://stackoverflow.com/questions/65542102/cannot-connect-to-a-remote-postgresql-database-through-ssh-using-jdbc-and-jsch 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…