我尝试从服务器在客户端计算机上执行java应用程序。但应用程序,当我尝试打开这个,而不是exetecute。 在这里我的课堂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;
}
}
请帮帮我,谢谢。
我做了更改,我擦除" String url =" jdbc:mysql:// localhost:3306 /" + database;"
并输入服务器的IP地址; " String url =" jdbc:mysql://10.6.12.247:3306 /" + database;"但结果如下:
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.
请告诉我。谢谢&#39; S。
答案 0 :(得分:3)
你的错误就在这里。
private static final String url = "jdbc:mysql://localhost:3306/"+database;
Localhost是运行应用程序的计算机,在本例中是客户端计算机。 为了实际连接到服务器,服务器的IP必须代替“localhost”。 正确的实施如下所示:
String ServerAddress = "192.168.1.4";
private static final String url = "jdbc:mysql://" + ServerAddress + ":3306/"+database;
此致
答案 1 :(得分:2)
您的应用程序正在尝试建立与localhost的jdbc连接,但肯定没有运行mysql
只需更改您的代码,如下所示:
url =“jdbc:mysql:// IPofYourServer:3306 /”+ database;
●将localhost替换为代码中服务器的IP地址。
答案 2 :(得分:0)
只需使用您的服务器名称更改该localhost即可。 但请确保您的数据库必须驻留在该服务器上。