【发布时间】:2010-07-15 00:59:15
【问题描述】:
我正在尝试使用“mysql-connector-java-5.1.13”连接 Mysql。我已经下载了它并将其放入我正在使用 Netbeans 的项目的库中。我找到了一个免费的主机并创建了 Mysql 数据库。现在我正在尝试以下代码;
import java.sql.*;
public class MysqlConnect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "a6990612_jdict";
String password = "0jdict";
String url = "jdbc:mysql://216.108.239.44:3306/a6990612_jdict";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
但捕获到异常并显示:无法连接到数据库服务器。
网址有问题吗,因为我不确定我会放什么,所以我放了我所在的免费主机的IP地址。
知道如何连接 Mysql 吗?提前致谢!
【问题讨论】:
-
您需要查看异常的详细信息。尝试将 catch 块更改为 e.printStackTrace();并将其添加到您的问题中。
-
添加到之前的评论:将这一行 - System.err.println ("Cannot connect to database server") - 改为 e.printStackTrace()
标签: java mysql connection