MySql jdbc URL 参数说明

        今天在写一个JDBC测试的demo时发生了一个很奇怪的错误,开始认为是驱动配错了,上网搜索了一下认为jdbc URL的错误。

        第一次填写的URL是这样的:

    "jdbc:mysql://localhost:3306/test"

        结果控制台报错:

        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 zonesupport.

        翻译过来是这样的:

      服务器时区值' ׼ʱ '无法识别或代表多个时区。 如果要利用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更具体的时区值。

          最后改成了这样:

"jdbc:mysql://localhost:3306/cart?characterEncoding=utf8&useUnicode=true&sessionVariables=storage_engine%3DInnoDB&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"

    在谷歌上查了一下各个参数所代表的意义:

    

参数名称 参数说明 缺省值 最低版本要求
user 数据库用户名(用于连接数据库)   所有版本
password 用户密码(用于连接数据库)   所有版本
useUnicode 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true false 1.1g
characterEncoding 当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk false 1.1g
autoReconnect 当数据库连接异常中断时,是否自动重新连接? false 1.1
autoReconnectForPools 是否使用针对数据库连接池的重连策略 false 3.1.3
failOverReadOnly 自动重连成功后,连接是否设置为只读? true 3.0.12
maxReconnects autoReconnect设置为true时,重试连接的次数 3 1.1
initialTimeout autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒 2 1.1
connectTimeout 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 0 3.0.1
socketTimeout socket操作(读写)超时,单位:毫秒。 0表示永不超时 0 3.0.1
   
通常mysql连接URL可以设置为:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false

在使用数据库连接池的情况下,最好设置如下两个参数:

autoReconnect=true&failOverReadOnly=false

问题解决了,课时总感觉那里不正确,我认为是编码方式的问题,但限于个人能力有限,只能这样解决了

你可能感兴趣的:(java,mysql)