当我将localhost更改为服务器上的主机时为什么会出错?

时间:2016-06-06 07:49:23

标签: java mysql

import java.sql.*;
import javax.swing.*;
public class Connect {
     Connection con=null;

        public static Connection ConnectDB(){
             try{

          Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://cybersolutionstoreco.ipagemysql.com:3306/java?useUnicode=true&characterEncoding=UTF-8","myuser","mypassword");
         //System.out.println("Connect Success");
          return con;

        }catch(ClassNotFoundException | SQLException e){
            JOptionPane.showMessageDialog(null, e);
            return null;
    }      
}
}

如果我将url更改为“localhost:3306 / java?useUnicode = true& characterEncoding = UTF-8”,则此代码可以在我的机器上运行。我不知道为什么它显示如下错误。请帮帮我。

  

com.mysql.jdbc.CommunicationsException:通信链接失败   由于潜在的例外:

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect
  at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  at java.net.Socket.connect(Socket.java:589)
  at java.net.Socket.connect(Socket.java:538)
  at java.net.Socket.<init>(Socket.java:434)
  at java.net.Socket.<init>(Socket.java:244)
  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:664)
  at java.sql.DriverManager.getConnection(DriverManager.java:247)
  at myfirstproject.Connect.ConnectDB(Connect.java:29)
  at myfirstproject.Login.txtpasswordKeyPressed(Login.java:108)
  at myfirstproject.Login.access$200(Login.java:13)
  at myfirstproject.Login$3.keyPressed(Login.java:87)
  at java.awt.Component.processKeyEvent(Component.java:6493)
  at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
  at java.awt.Component.processEvent(Component.java:6312)
  at java.awt.Container.processEvent(Container.java:2236)
  at java.awt.Component.dispatchEventImpl(Component.java:4891)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
  at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
  at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
  at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
  at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
  at java.awt.Component.dispatchEventImpl(Component.java:4762)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Window.dispatchEventImpl(Window.java:2750)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  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)


** END NESTED EXCEPTION **



Last packet sent to the server was 4 ms ago.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at myfirstproject.Login.txtpasswordKeyPressed(Login.java:109)
  at myfirstproject.Login.access$200(Login.java:13)
  at myfirstproject.Login$3.keyPressed(Login.java:87)
  at java.awt.Component.processKeyEvent(Component.java:6493)
  at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
  at java.awt.Component.processEvent(Component.java:6312)
  at java.awt.Container.processEvent(Container.java:2236)
  at java.awt.Component.dispatchEventImpl(Component.java:4891)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
  at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
  at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
  at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
  at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
  at java.awt.Component.dispatchEventImpl(Component.java:4762)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Window.dispatchEventImpl(Window.java:2750)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  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)
BUILD SUCCESSFUL (total time: 11 seconds)

1 个答案:

答案 0 :(得分:0)

关于您的问题可以有多种解决方案:

  1. 您的计算机上是否有正在运行的MySQL实例?
  2. 如果有,您使用的端口是否正确?
  3. 您是否尝试过不使用参数的localhost:3306
  4. 如果在您的计算机出现问题之前连接正常,而不是您的代码。您给出的代码对我来说很好。

    希望它有用。