0

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.

2
  • are you sure the username,password and db name of the server are same as you have written and the server is listening at port 3306? Commented Aug 24, 2014 at 6:16
  • yes, i sure. But the problem is solved. Thank's. Commented Aug 25, 2014 at 18:16

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can you ping the server from the client? Make sure that it's up and running, then try again.
Yes, i can PING the server, and i can PING the client computer.
2

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.

6 Comments

Can you try a telnet on the port 3306 and the ip 10.6.12.247 from your client? That is 'telnet 10.6.12.247 3306' and please check your firewall configuration?
the cmd show me the next message: Host IS NOT ALLOWED TO CONNECT TO THIS MYSQL SERVER .... what about that?
Allow remote hosts to connect to your database. On your mysql server type these commands GRANT ALL PRIVILEGES ON recursos_humanos.* to ‘root’@'%' IDENTIFIED BY '1234'; Then flush privileged FLUSH PRIVILEGES;
Yes, thanks, this is ready! thanks ... but now the database, when i put a not specific host ('%'), mysql throw me the nest error: "java.sql.SQLException: access denied for user 'remoto'@'myServerName' (using password: YES) "
Can you please clarify if you are connecting with the user 'remoto' or 'root'.?
|
0

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

1 Comment

Well, my server ip address is 10.12.6.247 and the database is in my server too. The ip address can use alike the name?? or how i see/know the server name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.