简体   繁体   中英

Trying to connect to mysql database from virtual machine (Java)

I'm trying to show the data form the empleados table. The table is inside the ucol database, which is in an Ubuntu virtual machine. I already configured the host-only network, it pings both ways. But when I run the program it catches an exception

static final String controlador = "com.mysql.jdbc.Driver";
static final String direccion = "jdbc:mysql://192.168.1.73:3306/ucol";
static final String usuario = "user";
static final String clave = "pass";  

public static void main(String[] args) {
   Connection conexion = null;
   Statement consulta = null;
   String sql;

   int id;
   String nombre;
   String apellidos;
   double salarios;

   try {
       System.out.println("Connecting...");
       Class.forName(controlador);
       conexion = DriverManager.getConnection(direccion, usuario, clave);
       consulta = conexion.createStatement();
       System.out.println("Connection done.");


       sql = "SELECT id, nombre, apellidos, salarios FROM empleados;";
       ResultSet resultado = consulta.executeQuery(sql);

       while(resultado.next()) {
           id = resultado.getInt("id");
           nombre = resultado.getString("nombre");
           apellidos = resultado.getString("apellidos");
           salarios = resultado.getInt("salarios");

           System.out.println("\nID: " + id 
                   + "\tNombre: " + nombre 
                   + "\tApellido: " + apellidos
                   + "\tSalarios: " + salarios);
       }

       resultado.close();
       consulta.close();
       conexion.close();
   }
   catch(Exception e) {
       System.err.println("Error at showing: " + e.toString());
   }
   finally {
       try {
           if(consulta != null)
               consulta.close();
           if(conexion != null)
               conexion.close();
       }
       catch(Exception e) {
           System.out.println("Error at closing: " + e.toString());
       }
   }
}

Here's the output:

Connecting
Error at showing: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection timed out: connect

STACKTRACE:

java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at Consulta.main(Consulta.java:37)


** END NESTED EXCEPTION **



Last packet sent to the server was 2 ms ago.

Here's how it shows the ifconfig on the virtual machine:

eth0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
         Direc. inet: 192.168.1.73 Difus.192.168.1.55 Másc.: 255.255.255.0
         ~~~~~~~~~~~~~~~~~~~~~~~~~~
         ~~~~~~~~~~~~~~~~~~~~~~~~~~

Am I missing something?

我发现了问题,我在虚拟机上停止了ufw服务。

sudo service ufw stop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM