感谢您给我重要的时间。 我正在尝试使用 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(),我发现这个主机名:端口 | localhost:0
所以我把一行代码改成了这个,
connection = DriverManager.getConnection("jdbc:mysql://localhost:0/db","root","");
当我运行这个新行时,收到此错误。
连接数据库...
错误:通讯链接失败
发送到服务器的最后一个数据包是0毫秒前。
请告诉我你的指导。 谢谢
答案 0 :(得分:3)
这是你可能犯的错误:
connection = DriverManager.getConnection("jdbc:mysql://localhost:80/db","root","");
您在此命令上使用的端口80是Apache侦听的端口。 MySQL默认侦听端口3306。
如果你没有改变那么你可能根本不需要在这个cmmand上放置:xxx端口号。
所以试试
connection = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");
并查看它是否适合您
否则尝试
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","");
答案 1 :(得分:0)
首先检查使用的端口号是由JDBC编写的,该端口号是否由其他应用程序使用。
检查您的端口号80
是否被其他应用程序使用,如果是,则必须终止端口号80,或者您必须重新配置JDBC服务器以在其他端口号上运行
检查以下链接: http://ye5.blogspot.in/2011/01/wamp-server-localhost-shows-blank-page.html