1

我正在尝试使用 java 连接到我在 Web 主机而不是 localhost 上托管的 mysql 数据库。我已将 mysql-connector 包含在项目的 java 构建路径中。我有以下代码:

package com;
import java.sql.*;

public class Retrieve{
    public static void main(String[] args) throws InstantiationException, IllegalAccessException
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String host = "mysql4.000webhost.com/a6136644_bustrac";
        String user = "";
        String password = "";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(host, user, password);
            if(conn != null)
            {
                System.out.println("Connection Successfull");
            }
        }
        catch (SQLException e)
        {
        System.out.println(e.getMessage());
        System.exit(0);
        }
   }

}

我没有收到任何错误,但是当我运行程序时,我得到:

No suitable driver found for mysql4.000webhost.com/a6136644_bustrac

可能是什么问题?

编辑:主机 url 中的 a6136644_bustrac 是指数据库

4

1 回答 1

1

代替:

String host = "mysql4.000webhost.com/a6136644_bustrac";

尝试:

String host = "jdbc:mysql://mysql4.000webhost.com/a6136644_bustrac";

解释:

您误解了 JDBC url 的概念。

它必须(对于 MySQL)至少具有基本格式:

jdbc:mysql://[host:port]/[database]

不只是:

[host]

正如你正在使用的那样。

检查更多可能的格式:http ://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

于 2013-04-13T07:41:24.787 回答