JDBC连接各种数据库的URL 笔记
@TOC
前言
经Java15 , Springboot2.44 下测试, 指定 url 后, 可以不用指定driver
提示:以下是本篇文章正文内容,下面案例可供参考
MySQL MariaDB
MySQL 8.0.x
官方文档参考 MySQL Connector/J 8.0 Developer Guide 6.3 Configuration Properties
官方文档参考 Connectors and APIs Manual 3.5.3 Configuration Properties
参考官方的, 参数全默认, url包含用户名和密码
jdbc:mysql://localhost/test?user=minty&password=greatsqldb
spring.datasource.url=jdbc:mysql://localhost/test?user=minty&password=greatsqldb
可以指定一些参数
-
connectTimeout 默认为0, 一直等待连接
-
socketTimeout 默认为0 , 一直等待查询结果返回
-
autoReconnect=true 自动重连, 默认false
-
autoReconnectForPools=false 使用适合连接池的重新连接策略 默认false
-
failOverReadOnly=true 在自动重新连接模式下发生故障时,将连接设置为"仅读"? 默认true
-
maxReconnects=3 重连最大次数 默认3
-
initialTimeout=2 如果启用自动重新连接,重连间隔时间为, 单位秒 默认2
-
useSSL 默认为true
-
useUnicode 默认为true
-
characterEncoding 8.0.25以前默认为自动检测和server一样, 8.0.26之后默认是utf8mb4, characterEncoding =UTF-8 characterEncoding =utf8mb4 #utf8mb4是utf8的超集
-
zeroDateTimeBehavior 日期时间0值处理方式: zeroDateTimeBehavior=EXCEPTION 抛异常 (默认值) zeroDateTimeBehavior=CONVERT_TO_NULL 转换为null zeroDateTimeBehavior=ROUND
-
connectionTimeZone 默认Default is "LOCAL". 以jvm时区为准 connectionTimeZone=SERVER , 以msql服务器为准 connectionTimeZone=GMT%2B8 %2B是"+"的转义 GMT+8 connectionTimeZone=Asia/Shanghai 上海时区 等同GMT%2B8 serverTimezone是旧版connectionTimeZone是新版, serverTimezone目前作为connectionTimeZone的别名,目前还能用, 以后会被替代
-
allowMultiQueries=false 默认false 不允许在语句中加 ; 分号 allowMultiQueries=true 允许加分号
-
maxRows=-1 返回的最大行数, 默认-1 返回所有行
-
emptyStringsConvertToZero 空字符转换为0 默认true
-
createDatabaseIfNotExist 如果数据库不存在则创建 默认false
-
useUnicode , 没能在官方文档找到其说明, 只是在官方网站搜到这么一段
spring.datasource.url=jdbc:mysql://192.168.43.254/db1?autoReconnect=true&characterEncoding =UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&connectionTimeZone=Asia/Shanghai&allowMultiQueries=true&maxRows=1000&createDatabaseIfNotExist=false
MySQL 5.7
spring.datasource.url=jdbc:mysql://192.168.43.254/db1?autoReconnect=true&characterEncoding =UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&connectionTimeZone=Asia/Shanghai&allowMultiQueries=true&maxRows=1000
spring.datasource.username=root
spring.datasource.password=pwd
MariaDB 10
spring.datasource.url=jdbc:mysql://192.168.43.254/db1?autoReconnect=true&characterEncoding =UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&connectionTimeZone=Asia/Shanghai&allowMultiQueries=true&maxRows=1000
spring.datasource.username=root
spring.datasource.password=pwd
微软 Microsoft SQLServer
SQL SERVER 2019
jdbc:sqlserver://localhost:1433; DatabaseName=test
spring.datasource.url=jdbc:sqlserver://localhost:1433; DatabaseName=test
spring.datasource.username=sa
spring.datasource.password=
SQL SERVER 2005以及之后2008,2012,2014,2016...)
jdbc:sqlserver://localhost:1433; DatabaseName=test
spring.datasource.url=jdbc:sqlserver://localhost:1433; DatabaseName=test
spring.datasource.username=sa
spring.datasource.password=
用户名和密码可以写到url里面 username=sa; password=passwd ;
spring.datasource.url: jdbc:sqlserver://localhost:1433; DatabaseName=db010; username=sa; password=passwd ;
不加密 encrypt=false;
了解加密支持
spring.datasource.url: jdbc:sqlserver://localhost:1433; DatabaseName=db010; username=sa; password=passwd ; encrypt=false;
信任服务器证书 trustServerCertificate=true
spring.datasource.url: jdbc:sqlserver://localhost:1433; DatabaseName=db010; username=sa; password=passwd ; trustServerCertificate=true
不加密 且 信任服务器证书 encrypt=false;trustServerCertificate=true
spring.datasource.url: jdbc:sqlserver://localhost:1433; DatabaseName=db010; username=sa; password=passwd ; encrypt=false;trustServerCertificate=true
SQL SERVER 2000
jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=test
该处使用的url网络请求的数据。
ORACLE
jdbc:oracle:thin:127.0.0.1:port:dbname
Sqlite
jdbc:sqlite:H:/zmp/sqlite/2
H2
使用内存
jdbc:h2:mem:
使用内存
jdbc:h2:mem:databaseName
使用文件,保存到用户文件夹, 可以加 "file:" 也可以不加
jdbc:h2:file:~/databaseName
jdbc:h2:~/databaseName
使用文件,保存到指定路径, 可以加 "file:" 也可以不加
jdbc:h2:file:C:/data/databaseName
jdbc:h2:C:/data/databaseName
H2官文 Features-Database URL Overview
Topic | URL Format and Examples |
---|---|
Embedded (local) connection | jdbc:h2:[file:][<path>]<databaseName> jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only) |
In-memory (private) | jdbc:h2:mem: |
In-memory (named) | jdbc:h2:mem:<databaseName> jdbc:h2:mem:test_mem |
Server mode (remote connections) using TCP/IP | jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName> jdbc:h2:tcp://localhost/~/test jdbc:h2:tcp://dbserv:8084/~/sample jdbc:h2:tcp://localhost/mem:test |
Server mode (remote connections) using TLS | jdbc:h2:ssl://<server>[:<port>]/[<path>]<databaseName> jdbc:h2:ssl://localhost:8085/~/sample; |
Using encrypted files | jdbc:h2:<url>;CIPHER=AES jdbc:h2:ssl://localhost/~/test;CIPHER=AES jdbc:h2:file:~/secure;CIPHER=AES |
File locking methods | jdbc:h2:<url>;FILE_LOCK={FILE|SOCKET|FS|NO} jdbc:h2:file:~/private;CIPHER=AES;FILE_LOCK=SOCKET |
Only open if it already exists | jdbc:h2:<url>;IFEXISTS=TRUE jdbc:h2:file:~/sample;IFEXISTS=TRUE |
Don't close the database when the VM exits | jdbc:h2:<url>;DB_CLOSE_ON_EXIT=FALSE |
Execute SQL on connection | jdbc:h2:<url>;INIT=RUNSCRIPT FROM '~/create.sql' jdbc:h2:file:~/sample;INIT=RUNSCRIPT FROM '~/create.sql'\;RUNSCRIPT FROM '~/populate.sql' |
User name and/or password | jdbc:h2:<url>[;USER=<username>][;PASSWORD=<value>] jdbc:h2:file:~/sample;USER=sa;PASSWORD=123 |
Debug trace settings | jdbc:h2:<url>;TRACE_LEVEL_FILE=<level 0..3> jdbc:h2:file:~/sample;TRACE_LEVEL_FILE=3 |
Ignore unknown settings | jdbc:h2:<url>;IGNORE_UNKNOWN_SETTINGS=TRUE |
Custom file access mode | jdbc:h2:<url>;ACCESS_MODE_DATA=rws |
Database in a zip file | jdbc:h2:zip:<zipFileName>!/<databaseName> jdbc:h2:zip:~/db.zip!/test |
Compatibility mode | jdbc:h2:<url>;MODE=<databaseType> jdbc:h2:~/test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE |
Auto-reconnect | jdbc:h2:<url>;AUTO_RECONNECT=TRUE jdbc:h2:tcp://localhost/~/test;AUTO_RECONNECT=TRUE |
Automatic mixed mode | jdbc:h2:<url>;AUTO_SERVER=TRUE jdbc:h2:~/test;AUTO_SERVER=TRUE |
Page size | jdbc:h2:<url>;PAGE_SIZE=512 |
Changing other settings | jdbc:h2:<url>;<setting>=<value>[;<setting>=<value>...] jdbc:h2:file:~/sample;TRACE_LEVEL_SYSTEM_OUT=3 |
Topic | URL Format and Examples |
jdbc:h2:[file:][<path>]<databaseName> jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only) | |
jdbc:h2:mem: | |
jdbc:h2:mem:<databaseName> jdbc:h2:mem:test_mem | |
使用TCP/IP的服务器模式(远程连接)
| jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName> jdbc:h2:tcp://localhost/~/test jdbc:h2:tcp://dbserv:8084/~/sample |
| jdbc:h2:ssl://<server>[:<port>]/<databaseName> jdbc:h2:ssl://secureserv:8085/~/sample; |
jdbc:h2:<url>;CIPHER=[AES|XTEA] jdbc:h2:ssl://secureserv/~/testdb;CIPHER=AES jdbc:h2:file:~/secure;CIPHER=XTEA | |
jdbc:h2:<url>;FILE_LOCK={NO|FILE|SOCKET} jdbc:h2:file:~/quickAndDirty;FILE_LOCK=NO jdbc:h2:file:~/private;CIPHER=XTEA;FILE_LOCK=SOCKET | |
jdbc:h2:<url>;IFEXISTS=TRUE jdbc:h2:file:~/sample;IFEXISTS=TRUE | |
jdbc:h2:<url>;DB_CLOSE_ON_EXIT=FALSE | |
jdbc:h2:<url>[;USER=<username>][;PASSWORD=<value>] jdbc:h2:file:~/sample;USER=sa;PASSWORD=123 | |
jdbc:h2:<url>;LOG=2 jdbc:h2:file:~/sample;LOG=2 | |
jdbc:h2:<url>;TRACE_LEVEL_FILE=<level 0..3> jdbc:h2:file:~/sample;TRACE_LEVEL_FILE=3 | |
jdbc:h2:<url>;IGNORE_UNKNOWN_SETTINGS=TRUE | |
jdbc:h2:<url>;ACCESS_MODE_LOG=rws;ACCESS_MODE_DATA=rws | |
在Zip文件中的数据库 | jdbc:h2:zip:<zipFileName>!/<databaseName> jdbc:h2:zip:~/db.zip!/test |
jdbc:h2:<url>;MODE=<databaseType> jdbc:h2:~/test;MODE=MYSQL | |
jdbc:h2:<url>;AUTO_RECONNECT=TRUE jdbc:h2:tcp://localhost/~/test;AUTO_RECONNECT=TRUE | |
jdbc:h2:<url>;AUTO_SERVER=TRUE jdbc:h2:~/test;AUTO_SERVER=TRUE | |
jdbc:h2:<url>;<setting>=<value>[;<setting>=<value>...] jdbc:h2:file:~/sample;TRACE_LEVEL_SYSTEM_OUT=3 |
转载自:https://juejin.cn/post/7247740446281023549