java连接MySql数据库(源码)

java连接MySql数据库(源码)

——————————————————————————————————————————————————————

准备工作
1.下载一个mysql-connector-java-.jar包。
百度mysqljar包。或者点点下面这条链接。选择Platform巴拉巴拉~下载~
https://dev.mysql.com/downloads/connector/j/
这里写图片描述
2.解压,随你解压到哪吧,开心就好~
3.添加到项目结构。如idea,在已经打开的project中点击File–>Project Structure。在弹出的窗口中选择Modules,点击右侧+号,选择JARs~~噗啦噗啦,然后弹出一个窗口,选择刚才你解压出来的文件夹里的**.jar文件。然后一路OK就好了。
这里写图片描述
4.这里写图片描述
——————————————————————————————————————————————————————写代码咯
emmm,贴源码吧,先贴我刚开始参考网上写的连接有问题的源码(也不算是有问题,因为MySql和jar包版本更新了,有一些地方不一样了,需要更改一下),说明一下几个警告错误以及怎么修改,正确的源码翻到最下面文件末尾就好了。

import java.sql.*;

/*
 * 测试连接数据库(会有错误的源码)
 */
public class Test {
    //驱动程序名
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    //URL
    public static final String URL = "jdbc:mysql://localhost:3306/cTest";
    public static final String USER = "root";
    public static final String PASSWORD = "wh123456";

    public static void main(String[] args) {
        try {

            Class.forName(DRIVER);
            Connection con = DriverManager.getConnection(URL,USER ,PASSWORD);
            if(!con.isClosed())
                System.out.println("成功连接至数据库!");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

运行,会有以下几个警告及错误:
1.

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.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’。 驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。

按他说的,把驱动程序DRIVER改成com.mysql.cj.jdbc.Driver就好了,或者按他说的不用手动加载(我没试过, ̄へ ̄)
2.

Mon Aug 06 12:11:57 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.
谷歌翻译:
Mon Aug 06 12:11:57 CST 2018警告:不建议在没有服务器身份验证的情况下建立SSL连接。 根据MySQL 5.5.45 +,5.6.26 +和5.7.6+要求如果未设置显式选项,则必须默认建立SSL连接。 为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。 您需要通过设置useSSL = false显式禁用SSL,或者设置useSSL = true并为服务器证书验证提供信任库。

按他说的把设置useSSL=true,设置方法是在URL后面加“?…&…&…”,“…”里是各种条件,“&”连接不同条件

3.

java.sql.SQLException: The server time zone value ‘?D1ú±ê×?ê±??’ 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.
谷歌翻译
java.sql.SQLException:服务器时区值’?D1ú±ê×?ê±??’ 无法识别或代表多个时区。 如果要使用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更具体的时区值。

什么鬼时区错误,都是新版本带来的问题,查了一下,URL加上serverTimezone=GMT%2B8代表东八区就好了
2、3URL改完如下:

URL = “jdbc:mysql://localhost:3306/cTest” +
“?useUnicode=true&characterEncoding=utf-8&” +
“useSSL=false&serverTimezone=GMT%2B8”;

扫描二维码关注公众号,回复: 2689957 查看本文章

就这三个因为版本不同带来的问题,写这篇blog就是为了记录这几个问题,修改一下驱动名称和URL就好了,其他的网上也都很好查到。修改过的源代码如下:


import java.sql.*;

public class Test {

    public static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/ctest" +
                                        "?useUnicode=true&characterEncoding=utf-8&" +
                                         "useSSL=false&serverTimezone=GMT%2B8";
    public static final String USER = "root";
    public static final String PASSWORD = "wh123456";

    public static void main(String[] args) {
        try {
            //加载驱动程序
            Class.forName(DRIVER);
            //创建连接对象
            Connection con = DriverManager.getConnection(URL,USER ,PASSWORD);
            if(!con.isClosed())
                System.out.println("成功连接至数据库!");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

试着运行一哈~~
这里写图片描述

后续的在java代码中操作数据库,做一些增删改查等基本操作,大家可以上网学习。如果不忙,做完家务了,又没人陪我玩♂游戏的话,闲着无聊可能会写一下 (/▽\)。

猜你喜欢

转载自blog.csdn.net/qq_36640955/article/details/81479272