1) 確定你的 MySQL 已啟動?
2) 你有沒有設定 Mysql 的 port 為 3306 以外的其它值?
有以下的程式碼試試
public static Connection GetConnection() {
Connection myConn = null; try
{
Class.fromName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception e)
{
System.err.pintln("Error");
e.printStackTrace();
}
try
{
String url = "jdbc:mysql://localhost/mysql?user=admin&password=admin"; // 修改成你自己的
myConn = DriverManager.getConnection(url);
}
catch (Exception e)
{
System.out.println("SQLException :"+e.getMessage());
System.out.println("SQL state :"+e.getSQLState());
System.out.println("Error Code :"+e.getErrorCode());
}
return myConn;
}
2) 你有沒有設定 Mysql 的 port 為 3306 以外的其它值?
有以下的程式碼試試
public static Connection GetConnection() {
Connection myConn = null; try
{
Class.fromName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception e)
{
System.err.pintln("Error");
e.printStackTrace();
}
try
{
String url = "jdbc:mysql://localhost/mysql?user=admin&password=admin"; // 修改成你自己的
myConn = DriverManager.getConnection(url);
}
catch (Exception e)
{
System.out.println("SQLException :"+e.getMessage());
System.out.println("SQL state :"+e.getSQLState());
System.out.println("Error Code :"+e.getErrorCode());
}
return myConn;
}
改为
String url ="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=gb2312";