为什么我不能通过Java访问远程数据库?

时间:2011-08-14 17:29:59

标签: java database connect

我正在尝试创建一个Java应用程序,它需要与MySQL数据库连接才能读取数据。

我已经阅读了几个教程和示例,几乎尝试了所有内容,但我无法访问数据库。

目前我正在使用它来连接数据库:

try{
   Class.forName("com.mysql.jdbc.Driver");
   con = DriverManager.getConnection("jdbc:mysql://myhost/test","databaseUser",  "password");
}
catch (Exception e){
   System.out.println(e);
}

堆栈跟踪:

  

com.mysql.jdbc.CommunicationsException:由于基础异常导致通信链接失败:

     

**开始没有异常**

     

com.mysql.jdbc.CommunicationsException   消息:由于潜在的异常导致通信链路故障:

     

**开始没有异常**

     

java.io.EOFException的   消息:无法读取服务器的响应。预计读取4个字节,在连接意外丢失之前读取0个字节。

     

堆栈跟踪:

     

java.io.EOFException:无法读取服务器的响应。预计读取4个字节,在连接意外丢失之前读取0个字节。       在com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)       在com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)       在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)       在com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)       在com.mysql.jdbc.Connection。(Connection.java:1555)       在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)       在java.sql.DriverManager.getConnection(DriverManager.java:582)       在java.sql.DriverManager.getConnection(DriverManager.java:185)       在facturatiesysteem.SysteemView.zoekKlant(SysteemView.java:148)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)       在java.lang.reflect.Method.invoke(Method.java:597)       在org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:662)       在org.jdesktop.application.ApplicationAction.actionPerformed(ApplicationAction.java:698)       在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)       在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2318)       在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)       在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)       在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)       at java.awt.Component.processMouseEvent(Component.java:6288)       在javax.swing.JComponent.processMouseEvent(JComponent.java:3267)       at java.awt.Component.processEvent(Component.java:6053)       at java.awt.Container.processEvent(Container.java:2041)       at java.awt.Component.dispatchEventImpl(Component.java:4651)       at java.awt.Container.dispatchEventImpl(Container.java:2099)       在java.awt.Component.dispatchEvent(Component.java:4481)       at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)       at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)       at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)       at java.awt.Container.dispatchEventImpl(Container.java:2085)       at java.awt.Window.dispatchEventImpl(Window.java:2478)       在java.awt.Component.dispatchEvent(Component.java:4481)       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)       at java.awt.EventQueue.access $ 000(EventQueue.java:84)       at java.awt.EventQueue $ 1.run(EventQueue.java:602)       at java.awt.EventQueue $ 1.run(EventQueue.java:600)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:87)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:98)       at java.awt.EventQueue $ 2.run(EventQueue.java:616)       at java.awt.EventQueue $ 2.run(EventQueue.java:614)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:87)       at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)       在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

     

**结束了例外**

发送到服务器的最后一个数据包是16毫秒前。

还有更多......

但我无法访问它。

我可以在这做什么?

1 个答案:

答案 0 :(得分:2)

phpmyadmin是数据库,Web应用程序的前端。它不是一个数据库。您无法通过phpmyadmin访问Java中的数据库。您需要直接访问数据库本身(称为MySQL,BTW)。不确定您的托管服务提供商是否允许。