将Java App连接到我的在线数据库JDBC MySql

时间:2014-09-17 11:29:24

标签: java mysql jdbc

我正在尝试从我的在线数据库中获取数据,但是会出现以下错误。但它对我的本地数据库工作正常。

Java代码

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.sql.*;
import javax.swing.JOptionPane;

public class BankingSystem extends JFrame {
    public static void main(String[] args) throws Exception
    {
        int ur=0;
        int PIN;
        String ID;
        int userSelection=0;
        Boolean pinCorrect = false;
        int amountDeposite;
        int amountWithdraw;
        int updateResult;
        Boolean isExit=false;
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://mysql11.000webhost.com/a46854323_javaapp";
        String user = "a46854323_javaapp";
        String pass="password";
        Connection con = DriverManager.getConnection(url,user,pass);
        Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

    }

}

我收到以下错误:

`

Exception in thread "main" java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection timed out: connect


java.net.SocketException
MESSAGE: java.net.ConnectException: Connection timed out: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection timed out: connect at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
        at com.mysql.jdbc.Connection.<init>(Connection.java:452)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at BankingSystem.main(BankingSystem.java:22)


        at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
        at com.mysql.jdbc.Connection.<init>(Connection.java:452)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at BankingSystem.main(BankingSystem.java:22)

可以解决什么问题?

1 个答案:

答案 0 :(得分:0)

我已经完成了上述所有评论,发现000host.com没有提供其数据库可以在其自己的空间外访问的工具。因此,通过google搜索,我发现http://www.db4free.net提供免费的mySql数据库用于测试目的。这样我的问题就解决了。感谢所有的comentators。