【java】【eclipse】jdbc连接mysql


【java】【eclipse】jdbc连接mysql8.0.13错误


摘要
driver问题:……mysql.jdbc……改成……mysql.cj.jdbc……
time zone value问题:url追加serverTimezone=GMT%2B8
SSL问题:url追加useSSL=FALSE

先按照我一开始知道的方式写(去掉了全部功能,另外static修饰的变量直接写在main里就不用加static了)

import java.sql.*;
public class JDBC_frist
{
    static String url ="jdbc:mysql://127.0.0.1:3306/mydatabase",
    		user = "root", pwd = "1234";
    static Connection con = null;
    static Statement sql = null;
    static ResultSet rs = null;
    
    public static void main(String[] args)
    {
    	// 驱动driver
    	try {Class.forName("com.mysql.jdbc.Driver");}
    	catch(ClassNotFoundException cnfe)
    	{System.out.println(cnfe.getMessage());}
    	// 操作
    	try
    	{
    		con = DriverManager.getConnection(url, user, pwd);
    		sql = con.createStatement();
    		rs = sql.executeQuery("SELECT * FROM customers");
    		con.close();
    	}
    	catch(SQLException se) 
    	{System.out.println(se.getMessage());}
    }
}

运行之后,出现了不少问题(其中第一行的是红色)。

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

第一行的问题容易解决,把 // 驱动driver 下方try里的内容

Class.forName("com.mysql.jdbc.Driver");

改为

Class.forName("com.mysql.cj.jdbc.Driver");

第二个问题经查找,出现这个的原因是因为 mysql返回的时间总是有问题,比实际时间要早8小时。在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B8
也就是将url改为

 "jdbc:mysql://127.0.0.1:3306/mydatabase?serverTimezone=GMT%2B8"

但是再次运行,又出现了新的错误(***是我加的和谐啦)

Sat Dec 22 17:29:55 CST 2018 WARN: Caught while disconnecting...

EXCEPTION STACK TRACE:



** BEGIN NESTED EXCEPTION ** 

javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify

STACKTRACE:

javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
	at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:129)
	at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:308)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:255)
	at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:645)
	at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:624)
	at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1312)
	at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
	at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1750)
	at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:720)
	at ***.JDBC_frist.main(JDBC_frist.java:32)


** END NESTED EXCEPTION **

我找了一转,最后采用了关闭SSL的方式绕开这个问题。一提EXception无法处理这个异常,也catch不到javax.net.ssl.SSLException这个异常(甚至写进去就报错)。
关闭SSL方式如下:

String url ="jdbc:mysql://127.0.0.1:3306/mydatabase?useSSL=FALSE&serverTimezone=GMT%2B8",

注意’:

  • ?'必须打
  • ‘&‘不能打成’,’(打成‘&&’虽然还看不出来什么问题,还是不要作比较好)
  • FALSE不能小写

差点忘了
time zone问题解决利用:https://blog.csdn.net/weixin_37577564/article/details/80329775
SSL问题解决利用:http://www.cnblogs.com/television/p/8529963.html

猜你喜欢

转载自blog.csdn.net/user_987654321/article/details/85214715