最近想学习一下 Java 连接 MySQL 数据库,于是有了这样的一些问题&解决办法。
首先是解决 JDBC(Java Data Base Connectivity)驱动问题,因为默认安装的JDK的扩展中没有JDBC驱动,不然报错如下:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) Sorry,can`t find the Driver! at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at Main.main(Main.java:24)
所以要去 https://dev.mysql.com/downloads/connector/j/ 下载 MySQL Connector/J,也就是 JDBC 驱动,访问链接如图,可以从下拉菜单中选择对应系统下载,不过作为大众的 Windows 系统下面有个区域是 MySQL Installer for Windows ,之后绕过一登录帐号下载。
下载后打开安装,Select Products and Features 中, Available Products 下的 MySQL Connectors 中会有 Connector/J 选择 Next 即可,此图是因为已经安装后匆情况;
这样安装后,再使用 Eclipse 或 Intellij 再运行代码发现,好像并没有什么用呀,依然是报同样的错误,反正笔者是这个情况。那怎么办?用 MySQL 安装了 JDBC 驱动,为什么还不可以呢。
难道要将驱动放到 JDK 下么,百度了一下 https://jingyan.baidu.com/article/3aed632e1a4ceb70108091f6.html 大概是要在项目中关联此驱动的 jar 包,也可以将其放到 jre 目录中的 lib\ext 下,这样就为所有的项目添加了。
在 Intellij 中则是 File -> Project Structure -> SDKs 的jdk的classpath点击右侧加号添加,如果你在将驱动包复制到 jre 目录下的 lib\ext 就不需要在此设置了,不管此处有没有包名,驱动已经在 Eclipse 或 Intellij 运行之前已经加载。
那么怎样获取这个驱动包呢,或者已经安装了MySQL怎么去找到呢?
默认的安装路径在 C:\Program Files (x86)\MySQL\Connector J 8.0 , mysql-connector-java-8.0.12.jar 就是JDBC驱动包,其它的文件大概是这个包的源码吧。当然如果你不想安装MySQL的话,可以通过此链接下载 https://pan.baidu.com/s/1paML6HezE9ok-8ZBJU6EgA
注意:不保证以后是最新版本的,所以欲求最新版还是关注 MySQL 吧。
可以通过项目的设置来关联此 jar 驱动文件,不想这么麻烦的话,就复制到 jre 包所在目录下的 bin\ext 下,注意并不是要和笔者一样的路径,具体根据自身情况来。
这样后,重启 Eclipse 或 Intellij 等IDE后,再去运行时,估计不会错了。
当然笔者遇到一个错误就是
java.sql.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.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
关于此错误 ,MySQL时区问题,可参阅
https://blog.csdn.net/weixin_37577564/article/details/80329775
https://blog.csdn.net/dongsl161/article/details/56495361
按照此设置,应该不会有错误的。(注意的是要区别大小写!!!)
代码如下,参考了网上的文章后做了一些修改:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) { // 声明Connection对象 Connection con; // 驱动程序名 String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名 test String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC"; // MySQL配置时的用户名 String user = "root"; // MySQL配置时的密码 String password = "root"; // 遍历查询结果集 try { // 加载驱动程序 Class.forName(driver); // 1.getConnection()方法,连接MySQL数据库!! con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) System.out.println("\n\t\t成功以 " + user + " 身份连接到数据库!!!"); // 2.创建statement类对象,用来执行SQL语句!! Statement statement = con.createStatement(); // 要执行的SQL语句 String sql = "select * from demo"; // 3.ResultSet类,用来存放获取的结果集!! ResultSet rs = statement.executeQuery(sql); System.out.println("\n\t\t执行结果如下所示:"); System.out.println("\t\t-----------------------------------------------------------------"); System.out.println("\t\t|\t" + "ID" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "手机号码" + "\t\t" + "地址\t|"); System.out.println("\t\t-----------------------------------------------------------------"); int ID = 0; String Name = null; String Sex = null; int Age = 0; String Phone = null; String Address = null; while (rs.next()) { // 获取 ID 这列数据 ID = rs.getInt("ID"); // 获取 Name 这列数据 Name = rs.getString("Name"); // 获取 Sex 这列数据 Sex = rs.getString("Sex"); // 获取 Age 这列数据 Age = rs.getInt("Age"); // 获取 Phone 这列数据 Phone = rs.getString("Phone"); // 获取 Address 这列数据 Address = rs.getString("Address"); // 输出结果 System.out.println("\t\t|\t" + ID + "\t" + Name + "\t" + Sex + "\t" + Age + "\t" + Phone + "\t" + Address + "\t|\t\t"); } System.out.println("\t\t-----------------------------------------------------------------"); rs.close(); con.close(); } catch (ClassNotFoundException e) { // 数据库驱动类异常处理 System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch (SQLException e) { // 数据库连接失败异常处理 e.printStackTrace(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { System.out.println("\t\t\t\t\t\t\t\t获取数据库数据完毕!!!"); } }
MySQL数据库结构
运行后效果视图
如有错误,欢迎指正!