大家好,又见面了,我是你们的朋友全栈君。
通常MySQL连接URL可以设置为:
jdbc:mysql://localhost:3306/test?user=root&password=123456&useUnicode=true&characterEncoding=gbk
&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC&driver=com.mysql.cj.jdbc.Driver
注:test
是数据库名;user
指定登录用户名;password
指定密码。
参数名称 | 参数说明 | 缺省值 | 最低版本要求 |
---|---|---|---|
user | 数据库用户名(用于连接数据库) | ||
password | 用户密码(用于连接数据库) | ||
useUnicode | 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true | false | 1.1g |
useSSL | MySQL在高版本需要指明是否进行SSL连接 在mysql连接字符串url中加入ssl=true或者false即可 | ||
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 |
allowMultiQueries | mysql驱动开启批量执行sql的开关 | false | |
serverTimezone | 设置时区 例如 serverTimezone=UTC(统一标准世界时间)或serverTimezone=Asia/Shanghai(中国时区) | ||
tinyInt1isBit | 如果tinyInt1isBit =true(默认),且tinyInt存储长度为1 ,则转为java.lang.Boolean 。否则转为java.lang.Integer。 | true |
characterEncoding
配置为gbk
useUnicode=true&characterEncoding=UTF-8
tinyInt1isBit =true(默认)
,且把数据类型tinyInt
存储长度设为1
,那么数据库tinyInt
类型会自动转成Java的Boolean
,否则自动转成Java的Integer
。如果希望转成Java的整数型,则将tinyInt1isBit
设为false
,或者把tinyInt
的长度设为大于1
,即tinyInt(N), N>1
,例如 tinyInt(2)
。也就是说,默认情况下,把字段的数据类型定义为tinyInt(1)
,是用来代表Boolean含义的字段,对应的是Java的Boolean
类型,如果插入true
,数据库会自动保存1
,插入false
数据库会自动保存0
inyInt1isBit
参数名区分大小写,否则不生效autoReconnect=true&failOverReadOnly=false
&
符号需要转义成& ;
。比如在tomcat的server.xml中配置数据库连接池时,MySQL JDBC URL如下:jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
&autoReconnect=true&failOverReadOnly=false
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160552.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有