[問題] MySQL 8.0.1 連線問題
開發環境作業系統: win8.1
開發環境: Netbeans 8.0.2 + jdk1.8.0_171
MySQL版本: 8.0.1
connector/J 版本: 8.0.11
小弟新手(還請各位前輩鞭小力點)
最近在學習MySQL連JDBC
但網路上的資源都是第五版的connector/J
一開始用的是JDBC v8.0.11而jdk是1.7.0_80
後來有更新jdk到1.8.0_171
也有更換driver為:"com.mysql.cj.jdbc.Driver"
系統有成功載入driver但始終連不上伺服器
google以後發現8.0版以後getConnection的url不太一樣(但怎麼試還是無法QQ)
我的account: root 密碼:souseasou3 table名稱: customertable
因此url為: "jdbc:mysql://locolhost:3306/customertable"
也有照這個篇去增加權限:https://goo.gl/PYGNL2
方法為:
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
但始終連接不上QQ
以下是錯誤訊息:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link
failure
The last packet sent successfully to the server was 0 milliseconds ago. The
driver has not received any packets from the server.
at
com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
at
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at crawler_to_db.Crawler_to_db.main(Crawler_to_db.java:32)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications
link failure
The last packet sent successfully to the server was 0 milliseconds ago. The
driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
at
com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
at
com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
at
com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 6 more
Caused by: java.net.UnknownHostException: localtest
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at
com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:150)
at
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
... 9 more
google後各種url都試過(如:https://goo.gl/HHFMD5這篇的解法,或是
改成:conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql?"
+ "user=root&password=onelife");)
還是出現一樣的問題
還請各位高手指點,謝謝!
以下是我的code:
package crawler_to_db;
import java.sql.*;
/**
*
* @author Angelo
*/
public class Crawler_to_db {
/**
* @param args the command line arguments
*/
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://locolhost:3306/customertable";
static final String usr = "root"; //username of db
static final String pss = "souseasou3"; //password of db
public static void main(String[] args) {
// TODO code application logic here
Connection conn = null;
Statement stmt = null;
try{
Class.forName(JDBC_DRIVER);
System.out.println("Loading driver successfully!.");
conn = DriverManager.getConnection(DB_URL,usr,pss);
System.out.println("Connecting successfully.");
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e) { //Handling for Class.forName()
e.printStackTrace();
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.24.51.178
※ 文章網址: https://www.ptt.cc/bbs/java/M.1530446358.A.B84.html
→
07/02 03:45,
6年前
, 1F
07/02 03:45, 1F
→
07/02 03:46,
6年前
, 2F
07/02 03:46, 2F
→
07/02 03:47,
6年前
, 3F
07/02 03:47, 3F
→
07/02 10:26,
6年前
, 4F
07/02 10:26, 4F
→
07/02 10:26,
6年前
, 5F
07/02 10:26, 5F
→
07/02 10:33,
6年前
, 6F
07/02 10:33, 6F
→
07/02 10:34,
6年前
, 7F
07/02 10:34, 7F
→
07/02 11:21,
6年前
, 8F
07/02 11:21, 8F
→
07/02 12:09,
6年前
, 9F
07/02 12:09, 9F
→
07/02 12:36,
6年前
, 10F
07/02 12:36, 10F
→
07/02 12:37,
6年前
, 11F
07/02 12:37, 11F
推
07/02 14:54,
6年前
, 12F
07/02 14:54, 12F
→
07/02 15:30,
6年前
, 13F
07/02 15:30, 13F
→
07/02 15:30,
6年前
, 14F
07/02 15:30, 14F
→
07/02 15:31,
6年前
, 15F
07/02 15:31, 15F
→
07/02 18:59,
6年前
, 16F
07/02 18:59, 16F
→
07/02 19:11,
6年前
, 17F
07/02 19:11, 17F
→
07/02 19:43,
6年前
, 18F
07/02 19:43, 18F
→
07/02 19:44,
6年前
, 19F
07/02 19:44, 19F
→
07/02 19:45,
6年前
, 20F
07/02 19:45, 20F
→
07/02 19:51,
6年前
, 21F
07/02 19:51, 21F
→
07/02 19:52,
6年前
, 22F
07/02 19:52, 22F
→
07/02 19:52,
6年前
, 23F
07/02 19:52, 23F
→
07/02 20:03,
6年前
, 24F
07/02 20:03, 24F
→
07/02 20:08,
6年前
, 25F
07/02 20:08, 25F
→
07/04 22:34,
6年前
, 26F
07/04 22:34, 26F
→
07/04 22:34,
6年前
, 27F
07/04 22:34, 27F
java 近期熱門文章
PTT數位生活區 即時熱門文章