[问题] MySQL 8.0.1 连线问题

楼主: Souseasou3 (Almighty)   2018-07-01 19:59:13
开发环境作业系统: 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();
}
}
}
作者: asd56799001 (贝尔菲格诺)   2018-07-02 03:45:00
我会先找一个mysql客户端软件来连连看如果客户端软件可以连 表示程式有问题如果客户端软件也不能练 表示mysql可能没设置好
楼主: Souseasou3 (Almighty)   2018-07-02 10:26:00
MySQL Workbench 8.0CE 算吗? 我用这个可以连喔用cmd连也可以
作者: sqrt998001 (平方根999)   2018-07-02 10:33:00
localhost?为啥你的范例都是locolhost
楼主: Souseasou3 (Almighty)   2018-07-02 11:21:00
不是用local host吗?
作者: sqrt998001 (平方根999)   2018-07-02 12:09:00
但你的code都打locolhost
楼主: Souseasou3 (Almighty)   2018-07-02 12:36:00
所以要用什么? 不是localhost的话https://goo.gl/HHFMD5我看这篇也是localhost
作者: asd56799001 (贝尔菲格诺)   2018-07-02 14:54:00
要不要试试把locolhost改成本地IP
楼主: Souseasou3 (Almighty)   2018-07-02 15:30:00
试过了 还是无法QQ我是用笔电 在不同地方上网会有不同ip
作者: asd56799001 (贝尔菲格诺)   2018-07-02 15:31:00
mysql和java程式是在同一台电脑上吗
楼主: Souseasou3 (Almighty)   2018-07-02 18:59:00
对!
作者: sqrt998001 (平方根999)   2018-07-02 19:11:00
我的意思是你可能拼错字了
楼主: Souseasou3 (Almighty)   2018-07-02 19:43:00
真的欸!低级错误但我改了变成以下错误" target="_blank" rel="nofollow">
Stackoverflow了一下 似乎连成了" target="_blank" rel="nofollow">
正确url感谢帮我发现低级错误的大大><
作者: asd56799001 (贝尔菲格诺)   2018-07-02 20:03:00
恭喜 连我复制你的都没发现
楼主: Souseasou3 (Almighty)   2018-07-02 20:08:00
低级错误 (摊)
作者: adrianshum (Alien)   2018-07-04 22:34:00
其实有留心看stack trace 的话,最原始的cause 就是说unknown host.

Links booklink

Contact Us: admin [ a t ] ucptt.com