java.sql.Connection extension for SSH

I have a MySQL database behind a firewall which can only be accessed via an SSH connection. Does anyone know of an implementation of java.sql.Connection which would allow me to make an SSH connection to this database?

Asked by: Audrey584 | Posted: 28-01-2022

Answer 1

You can use SSH's port forwarding to do this. While not a pure java.sql.Connection, it will allow you to tunnel the connection through ssh.

ssh -L 3306:localhost:3306

This will forward port 3306 on your local machine to port 3306 on This will allow you to connect to port 3306 on your local machine, and it will be tunnelled to

If you're looking to do it all in Java, create the ssh connection with JSch.

Answered by: Lily888 | Posted: 01-03-2022

Answer 2

Here is an example using the library sshj.

Answered by: Edgar821 | Posted: 01-03-2022

Similar questions

jdbc - Should I catch exceptions thrown when closing java.sql.Connection

Connection.close() may throw SqlException, but I have always assumed that it is safe to ignore any such exceptions (and I have never seen code that does not ignore them). Normally I would write: try{ connection.close(); }catch(Exception e) {} Or try{ connection.close(); }catch(Exception e) { logger.log(e.getMessage(), e);...

ThreadLocal + java.sql.Connection + servlet filter = 2009?

I am writing some servlets with plain old mostly-JDBC patterns. I realized that I have several objects that would like to share a single transaction, and I'd like to enforce that one HTTP transaction = one database transaction. I think I can do this via passing a Connection around in a ThreadLocal variable, and then having a servlet filter handling the creation/commit/rollback of said Connection. Is there a...

multithreading - Is java.sql.Connection thread safe?

To rephrase the question: should I avoid sharing instances of classes which implement java.sql.Connection between different threads?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)