Amaurys Sánchez
Amaurys Sánchez

Reputation: 124

how to execute a java application in a client computer from a server?

I'm trying execute a java application in a client computer from a server. But the application, when i try open this, not exetecute. Here my class Conexion:

package Conexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Conexion {   
    private static final String database = "recursos_humanos";
    private static final String login = "root";
    private static final String password = "1234";
    private static final String url = "jdbc:mysql://localhost:3306/"+database;

    public static Connection connection;
    public static Statement stm;
    public static ResultSet rs;
    public static PreparedStatement ps;

    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,login,password);                      
            if(connection != null)
                System.out.println("Base de Datos " +database +" lista.");
        } catch (ClassNotFoundException  ex) {
            System.out.println(ex);
            JOptionPane.showMessageDialog(null, "No se pudo coenctar: "+ex);
        }catch(SQLException ex){System.out.println(ex);}
        return connection;
    }
}

Help me please and thank's.

EDIT

I made the change, i erase "String url = "jdbc:mysql://localhost:3306/"+database;"

and put the ip address of server; "String url = "jdbc:mysql://10.6.12.247:3306/"+database;" but the RESULT was as follows:

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.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Ventanas.VentanaPrincipal.listar(VentanaPrincipal.java:387)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at Ventanas.VentanaPrincipal.<init>(VentanaPrincipal.java:76)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at Ventanas.VentanaPrincipal$11.run(VentanaPrincipal.java:356)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    at java.net.Socket.connect(Socket.java:538)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.net.Socket.<init>(Socket.java:434)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.net.Socket.<init>(Socket.java:244)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at Conexion.Conexion.getConnection(Conexion.java:34)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at Ventanas.VentanaPrincipal.listar(VentanaPrincipal.java:385)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    at Ventanas.VentanaPrincipal.<init>(VentanaPrincipal.java:76)
    at Ventanas.VentanaPrincipal$11.run(VentanaPrincipal.java:356)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.

Tell me about that please. Thank's.

Upvotes: 0

Views: 397

Answers (3)

Jubileu Tembe
Jubileu Tembe

Reputation: 41

Your error lies here.

private static final String url = "jdbc:mysql://localhost:3306/"+database;

Localhost is the computer in which the application is running, in this case, the client computer. In order to actually connect to the server, the IP of the server must be in place of "localhost". The correct implementation would be as represented below:

String ServerAddress = "192.168.1.4";
private static final String url = "jdbc:mysql://" + ServerAddress + ":3306/"+database;

Regards

Upvotes: 3

Kheshav Sewnundun
Kheshav Sewnundun

Reputation: 1244

Your application is trying to establish a jdbc connection to localhost which am sure does not have mysql running

Just change your code as follows:

url = "jdbc:mysql://IPofYourServer:3306/"+database;

● Replace localhost to the ip address of your server in your code.

Upvotes: 2

NIket
NIket

Reputation: 924

Just change that localhost with your server name and it will work. But make sure that your database must reside on that server.

Upvotes: 0

Related Questions