java 板


LINE

開發環境作業系統: 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://webptt.com/m.aspx?n=bbs/java/M.1530446358.A.B84.html
1F:→ asd56799001: 我會先找一個mysql客戶端軟體來連連看 07/02 03:45
2F:→ asd56799001: 如果客戶端軟體可以連 表示程式有問題 07/02 03:46
3F:→ asd56799001: 如果客戶端軟體也不能練 表示mysql可能沒設置好 07/02 03:47
4F:→ Souseasou3: MySQL Workbench 8.0CE 算嗎? 我用這個可以連喔 07/02 10:26
5F:→ Souseasou3: 用cmd連也可以 07/02 10:26
6F:→ sqrt998001: localhost? 07/02 10:33
7F:→ sqrt998001: 為啥你的範例都是locolhost 07/02 10:34
8F:→ Souseasou3: 不是用local host嗎? 07/02 11:21
9F:→ sqrt998001: 但你的code都打locolhost 07/02 12:09
10F:→ Souseasou3: 所以要用什麼? 不是localhost的話 07/02 12:36
11F:→ Souseasou3: https://goo.gl/HHFMD5我看這篇也是localhost 07/02 12:37
12F:推 asd56799001: 要不要試試把locolhost改成本地IP 07/02 14:54
13F:→ Souseasou3: 試過了 還是無法QQ 07/02 15:30
14F:→ Souseasou3: 我是用筆電 在不同地方上網會有不同ip 07/02 15:30
15F:→ asd56799001: mysql和java程式是在同一台電腦上嗎 07/02 15:31
16F:→ Souseasou3: 對! 07/02 18:59
17F:→ sqrt998001: 我的意思是你可能拼錯字了 07/02 19:11
18F:→ Souseasou3: 真的欸!低級錯誤 07/02 19:43
19F:→ Souseasou3: 但我改了變成以下錯誤 07/02 19:44
20F:→ Souseasou3: https://imgur.com/a/u9EVZEL 07/02 19:45
21F:→ Souseasou3: Stackoverflow了一下 似乎連成了 07/02 19:51
22F:→ Souseasou3: https://imgur.com/VsJr6X1 正確url 07/02 19:52
23F:→ Souseasou3: 感謝幫我發現低級錯誤的大大>< 07/02 19:52
24F:→ asd56799001: 恭喜 連我複製你的都沒發現 07/02 20:03
25F:→ Souseasou3: 低級錯誤 (攤) 07/02 20:08
26F:→ adrianshum: 其實有留心看stack trace 的話,最原始的cause 就是 07/04 22:34
27F:→ adrianshum: 說unknown host. 07/04 22:34







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BabyMother站內搜尋

TOP