使用JDBC連接MySql時出現
問題一:
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.
將com.mysql.jdbc.Driver改為com.mysql.cj.jdbc.Driver
原因:當使用驅動mysql-connector-java 5時,JDBC連接MySQL應使用com.mysql.jdbc.Driver 。
當使用驅動mysql-connector-java 6及以上版本時,JDBC連接MySQL應使用com.mysql.cj.jdbc.Driver ,同時,還需要指定時區serverTimezone(解決方法如下)
問題二:
SQLException: 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.
在連接字符串后面加上?serverTimezone=UTC
其中UTC是統一標准世界時間。
解決中文亂碼輸入問題:?useUnicode=true&characterEncoding=UTF-8
完整的連接字符串示例:
jdbc:mysql://127.0.0.1:3306/(數據庫名稱)?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
或者:
jdbc:mysql://localhost:3306/(數據庫名稱)?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
問題三:
如圖

在Maven項目中連接MySQL數據庫時NullPointerException
原因: .properties文件在maven項目中不能加載
解決方法:在maven項目中,配置文件一定要放到resources中,在默認情況下,不加載/src項目下的配置文件
