根据系统安装的MySQL版本找到对应的驱动文件,笔者本机安装的是:
查看MySQL版本
Mysql驱动下载地址:MySQL Connector/J
准备好文件:
mysql-connector-java-8.0.9-rc
笔者电脑安装的IDE是IntelliJ IDEA CE,导入jar驱动文件。
执行准备好的操作MySQL数据的代码:
操作MySQL代码
MySQL数据库中的表的文件是这样的,本次实验就读取其中两列的数据。
student表
迫不及待的运行一下:
获得如下数据:
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=61842:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/ruanfumin/Documents/LearnJava/out/production/LearnJava:/Users/ruanfumin/Documents/mysql-connector-java-8.0.9-rc.jar main
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.
Thu Dec 13 00:14:07 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
成功连接数据库
张老大 北京市海淀区
张老二 北京市昌平区
张三 湖南省永州市
李四 辽宁省阜新市
王五 福建省厦门市
王六 湖南省衡阳市
Thu Dec 13 00:14:07 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.mysqla.io.MysqlaProtocol.quit(MysqlaProtocol.java:1261)
at com.mysql.cj.mysqla.MysqlaSession.quit(MysqlaSession.java:314)
at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1950)
at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:740)
at main.main(main.java:36)
** END NESTED EXCEPTION **
数据库数据成功获取
Process finished with exit code 0
证明数据读取成功!宾果~
网友评论