0

I have a difficulties to connect to SQL-Oracle from my Java program using an SSH tunnel.

My current situation:

I have a ubuntu 16.04 server running SQL BBDD and I'm using a Java app to connect to the server BBDD. In the local red everything is fine. The app can connect to the server without problems, but when the app connects to the server outside to the local red I get a null pointer exception. The connection with the SSH tunnel is successful but the NPE gets thrown when the app trying to connect to the DDBB. Will got you all the information about the server and the app

public static void sshTunel() {
        sshHost = "bahguta.ddns.net";
        String sshuser = "plam";
        String SshKeyFilepath = "/home/plam/.ssh/id_rsa";
        Session session;
        int localPort = 8740;
        String remoteHost = "bahguta.ddns.net";
        int remotePort = 1521;
        try {
            java.util.Properties config = new java.util.Properties();
            JSch jsch = new JSch();
            session = jsch.getSession(sshuser, sshHost, 22);
            jsch.addIdentity(SshKeyFilepath, "key pass");
            config.put("StrictHostKeyChecking", "no");
            config.put("ConnectionAttempts", "3");
            session.setConfig(config);
            session.connect();

            System.out.println("SSH Connected ...");

            int assinged_port = session.setPortForwardingL(localPort, remoteHost, remotePort);

            System.out.println("localhost:" + assinged_port + " -> " + remoteHost + ":" + remotePort);
            System.out.println("Port Forwarded ...");
            
            try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Error con el driver,comprueba que lo has metido en el proyecto y que es el correcto");
        }

          String url = "jdbc:oracle:thin:@192.168.0.160:1521:XE";
          try {
              conexion = DriverManager.getConnection(url, usr, pwd);
              if (conexion != null) {
                  System.out.println("Conexión establecida con exito");
              }
            } catch (SQLException ex) {
              System.out.println("Error al conectar con la BBDD, compruebe si está levantada");
          }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
    

And the Output is:

run:
SSH Connected ...
localhost:8740 -> bahguta.ddns.net:1521
Port Forwarded ...
jdbc:oracle:thin:@192.168.0.160:1521:XE
Error al conectar con la BBDD, compruebe si está levantada
conexion null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

What I tried:

// Server IP 192.168.0.160 i tried using jdbc:oracle:thin:@localhost:1521:XE but without success. The user who tried to connect to the BBDD got the resource and connect privileges.

blkpingu
  • 1,556
  • 1
  • 18
  • 41

0 Answers0