zoukankan      html  css  js  c++  java
  • Java连接MySQL简单步骤及常见问题解析

    这篇文章跟读者们介绍Java语言如何连接到MySQL数据库相关操作,及FAQ(Frequently Asked Questions)介绍。

    选择工具和准备数据集

    IDE:Eclipse

    数据库:使用的是MySQL 8.0

    驱动程序包:mysql-connector-java-8.0.12.jar

    驱动程序类名:com.mysql.cj.jdbc.Driver

    提前准备工作:笔者使用MySQL Workbench 工具来进行数据库CRUD各种操作,在数据库中建立了一个test数据库,接着在test数据库中建立teacher表格和往向表格中增加数据。

    并且已经往表中插入了6项数据。

    在Eclipse中建立一个数据库操作的Java project。并且在project里面引入(配置)驱动程序jar包

    主要步骤

    1.导入所需要的包

    2.注册JDBC驱动

    3.建立连接到数据库

     

    4.执行 CRUD 操作

    5.处理得到的结果

    6.关闭资源,释放连接

    提供源代码参考:

     1 //STEP 1:导入包
     2 //大多数情况下,使用import java.sql.*;就足够了。
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.ResultSet;
     6 import java.sql.SQLException;
     7 import java.sql.Statement;
     8 
     9 public class DBConnettion {
    10     
    11     //不同版本的数据库驱动,名字会不相同同。
    12     //数据库驱动 mysql-connector-java 6.0以上的版本, 驱动名是:"com.mysql.cj.jdbc.Driver"
    13     //旧版本驱动名是:"com.mysql.jdbc.Driver"
    14     private static final String JDBC_DEIVER = "com.mysql.cj.jdbc.Driver";
    15     //笔者查看本机数据库进程,默认使用3306端口,大家根据自己数据库进程端口号来相应修改。
    16     //比如我要访问MySQL的test数据库, 主机ip:端口号/数据库名称  数据库URL格式:---"jdbc:mysql://localhost:3306/test
    17     private static final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false";//?useUnicode=true&characterEncoding=utf8&useSSL=false
    18     private static final String USER = "****"; //"****"替换成自己的数据库用户名
    19     private static final String PASS = "****"; //"****"替换成自己的数据库密码
    20     
    21     public static void main(String[] args) {
    22         Connection conn = null;
    23         Statement stmt = null;
    24         
    25         try {
    26             //STEP 2:注册 JDBC 驱动
    27             Class.forName(JDBC_DEIVER);
    28             
    29             //STEP 3:建立连接到数据库
    30             System.out.println("正在连接数据库......");
    31             conn = DriverManager.getConnection(DB_URL, USER, PASS);
    32             stmt = conn.createStatement();
    33             
    34             //STEP 4:执行 CRUD 操作
    35             System.out.println("正在查询数据:");
    36             String sql = "select * from teacher;";
    37             ResultSet rs = stmt.executeQuery(sql);
    38             
    39             //STEP 5:处理得到结果
    40             System.out.println("Department   ID     Name");
    41             while (rs.next()) {
    42                 int id = rs.getInt("id");
    43                 String name = rs.getString("name");
    44                 if (!rs.isLast())
    45                     System.out.println("	    " + id + "   " + name);
    46                 else
    47                     System.out.println( "   " + id + "   " + name);
    48             }
    49             
    50             //STEP 6:关闭资源,释放连接。
    51             rs.close();
    52             stmt.close();
    53             conn.close();
    54         } catch (ClassNotFoundException e) {
    55             e.printStackTrace();
    56         } catch (SQLException e) {
    57             e.printStackTrace();
    58         } finally {
    59             try {
    60                 if (stmt != null)
    61                     stmt.close();
    62                 if (conn != null)
    63                     conn.close();
    64             } catch (SQLException e) {
    65                 // TODO Auto-generated catch block
    66                 e.printStackTrace();
    67             }
    68         }
    69         System.out.println("释放数据库连接......");
    70     }
    71 }

    运行结果:

    常见问题解析及解决方法

    出现问题:

    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.

    解析:

    从错误提示可知道是时区的错误,因此只要将数据库系统的时区设置为你当前系统时区即可。

    解决方法:

    因此使用root用户登录mysql,按照如下图所示操作即可。

    查看数MySQL据库系统的时区设置

    show variables like '%time_zone%';

    笔者电脑系统时区设置为GMT+8:北京时间。而数据库系统时区默认为GMT+0(格林尼治时间),因此修改数据库系统时区跟我的系统时区相互一致。

    set global time_zone = '+8:00';

    对数据库系统时区变量做修改,不会立即发生改变。退出本次黑窗命令行(客户端)后

    重新登录数据库,再次进行数据库系统时区查询,发现已经做出更改。

    show variables like '%time_zone%';

     

    出现问题:

    Sat May 04 19:01:08 CST 2019 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.

    解析:

    从MySQL数据库 5.5.45+ 以后,对数据库的访问都要求建立SSL加密连接。我们只是做一个简单的数据库访问实例,因此访问数据库不进行SSL加密连接。

    解决方法:

    可以在数据库URL连接语句末尾加上"?useSSL=false"来取消数据库的警告。

    或者可以给数据库服务器提供证书验证的信任库,进行SSL加密连接。

  • 相关阅读:
    Linux常用命令大全(非常全!!!)
    Springboot项目与vue项目整合打包
    Spring Boot开启Druid数据库监控功能
    (八)CXF添加自定义拦截器
    (七)CXF添加拦截器
    (六)cxf处理一些Map等复杂类型
    (无)webservice执行过程深入理解
    (四)CXF处理JavaBean以及复合类型
    (三)使用CXF开发WebService客户端
    (二)使用CXF开发WebService服务器端接口
  • 原文地址:https://www.cnblogs.com/Lints/p/10809235.html
Copyright © 2011-2022 走看看