我一直在尝试将程序连接到非本地主机数据库,因为它将在其他计算机上使用,但是我一直遇到通讯链接失败的情况。
这是我遇到的错误:
Communications link failure
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at ContactList.SignUp.jLabelCreateMouseClicked(SignUp.java:456)
at ContactList.SignUp.access$1300(SignUp.java:16)
at ContactList.SignUp$8.mouseClicked(SignUp.java:164)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)
当我将localhost与XAMPP一起使用时,代码可以正常工作。可能是OVH的限制或MySQL中的某些设置。 MySQL在Linux Ubuntu VPS上。 这是连接的代码。
import java.sql.Connection;
import java.sql.DriverManager;
public class myConnection {
// Init Database Constants
private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://54.39.145.0:3306/contact";
private static final String USERNAME = "turtle";
private static final String PASSWORD = "ILikeTurtles321!";
private static final String MAX_POOL = "250";
// Init Connection Object
private Connection con;
// Connect Database
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(DATABASE_DRIVER);
con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return con;
}
}