我已经在MAC OS上安装了Tomcat6和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 */ }
}
}
}
}好了,它返回我“无法连接到数据库服务器”。
发布于 2011-03-22 19:46:58
问题出在JDBC驱动程序上。您必须将其包含到类路径中。
你可以在这里下载:http://dev.mysql.com/downloads/connector/j/5.0.html
将mysql-connector-java-5.1.15-bin.jar添加到类路径中
那么它就会起作用。
https://stackoverflow.com/questions/5390243
复制相似问题