1

谢谢你给了我重要的时间。我正在尝试使用WampServer 2.4在应用程序和数据库之间建立连接我的配置文件包含与默认 MySQL 特权帐户相对应的设置(没有密码的 root)。我的 MySQL 服务器正在使用此默认值运行,我正在使用此代码进行连接。

import java.lang.ClassNotFoundException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;


public class implementation {

    public static void main(String[]arg)
    {

        Connection connection = null;
        Statement statement = null;

        try
        {
            System.out.println("conneting to Database...");
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:80/db","root","");
            System.out.println("Connection Successful");

        }
        catch(ClassNotFoundException error)
        {
            System.out.println("Error:" + error.getMessage()); 
        }

        catch(SQLException error)
        {
            System.out.println("Error:" + error.getMessage());
        }
        finally
        {
            if (connection != null) 
                try {
                    connection.close();
                    }
            catch(SQLException ignore)
            {

            }

            if (statement != null) 
                try {
                    statement.close();
                    }

            catch(SQLException ignore)
            {

            }
        }
    }

}

当我运行此代码时,它不会将我连接到数据库。所以我试图获得正确的端口,我检查了phpinfo() 并找到了这个Hostname:Port | 本地主机:0

所以我把一行代码改成了这个,

connection = DriverManager.getConnection("jdbc:mysql://localhost:0/db","root","");

当我运行这条新线路时,出现此错误。

连接到数据库...

错误:通讯链路故障

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

请给我你的指导来解决这个问题。谢谢你

4

2 回答 2

3

这是您可能的错误:

 connection = DriverManager.getConnection("jdbc:mysql://localhost:80/db","root","");

您在此命令中使用的端口 80 是 Apache 侦听的端口。MySQL 默认监听 3306 端口。

如果您没有更改它,那么您可能根本不需要将 :xxx 端口号放在此命令上。

所以试试

 connection = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");

看看这是否适合你

否则试试

 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","");
于 2013-09-29T02:26:42.913 回答
0

首先检查 JDBC 使用的端口号以及其他应用程序是否使用了该端口号。

检查您的端口号80是否被其他应用程序使用,如果是,则您必须终止端口号 80 或者您必须重新配置 JDBC 服务器以在其他端口号上运行

检查以下链接:http: //ye5.blogspot.in/2011/01/wamp-server-localhost-shows-blank-page.html

于 2013-10-05T19:31:02.400 回答