【发布时间】:2011-07-20 09:37:27
【问题描述】:
我已经在 MAC OS 上安装了 Tomcat 6 和 apache XAMPP。 XAMPP 包括 MySQL。
我打开 TOMCAT 和 XAMPP。
然后我尝试用 JDBC 连接到 MySQL。
public class main {
public static void main(String[] args) {
Connection conn = null;
try
{
String userName = "root";
String password = "";
//<facility> is the name of the database i created
String url = "jdbc:mysql://localhost/facility";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.out.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 */ }
}
}
}
}
嗯,它给了我“无法连接到数据库服务器”。
【问题讨论】:
-
是否可以在没有任何进一步配置的情况下将 XAMPP 与 TOMCAT 进行交互。在我的示例中,我收到连接失败的异常。
-
xampp中的phpMyAdmin可以连接mysql吗?
-
打印异常并读取消息:System.out.println("Cannot connect to database server" + e.getMessage());
-
可以。使用 phpMyAdmin 我创建了“设施”数据库
-
还能打印异常类型e.getClass().getName()吗?我认为它可能无法找到/加载驱动程序。
标签: java mysql tomcat jdbc xampp