mysql JDBC URL格式各個(gè)參數(shù)詳解
mysql JDBC URL格式如下:
jdbc:mysql://[host:port],[host:port].../[database][?參數(shù)名1][=參數(shù)值1][&參數(shù)名2][=參數(shù)值2]...
現(xiàn)只列舉幾個(gè)重要的參數(shù),如下表所示:
參數(shù)名稱 參數(shù)說(shuō)明 缺省值 最低版本要求 user 數(shù)據(jù)庫(kù)用戶名(用于連接數(shù)據(jù)庫(kù)) ? 所有版本 password 用戶密碼(用于連接數(shù)據(jù)庫(kù)) ? 所有版本 useUnicode 是否使用Unicode字符集,如果參數(shù)characterEncoding設(shè)置為gb2312或gbk,本參數(shù)值必須設(shè)置為true false 1.1g characterEncoding 當(dāng)useUnicode設(shè)置為true時(shí),指定字符編碼。比如可設(shè)置為gb2312或gbk false 1.1g autoReconnect 當(dāng)數(shù)據(jù)庫(kù)連接異常中斷時(shí),是否自動(dòng)重新連接? false 1.1 autoReconnectForPools 是否使用針對(duì)數(shù)據(jù)庫(kù)連接池的重連策略 false 3.1.3 failOverReadOnly 自動(dòng)重連成功后,連接是否設(shè)置為只讀? true 3.0.12 maxReconnects autoReconnect設(shè)置為true時(shí),重試連接的次數(shù) 3 1.1 initialTimeout autoReconnect設(shè)置為true時(shí),兩次重連之間的時(shí)間間隔,單位:秒 2 1.1 connectTimeout 和數(shù)據(jù)庫(kù)服務(wù)器建立socket連接時(shí)的超時(shí),單位:毫秒。 0表示永不超時(shí),適用于JDK 1.4及更高版本 0 3.0.1 socketTimeout socket操作(讀寫(xiě))超時(shí),單位:毫秒。 0表示永不超時(shí) 0 3.0.1
?
對(duì)應(yīng)中文環(huán)境,通常mysql連接URL可以設(shè)置為:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false
在使用數(shù)據(jù)庫(kù)連接池的情況下,最好設(shè)置如下兩個(gè)參數(shù):
autoReconnect=true&failOverReadOnly=false
需要注意的是,在xml配置文件中,url中的&符號(hào)需要轉(zhuǎn)義成“&”。比如在tomcat的server.xml中配置數(shù)據(jù)庫(kù)連接池時(shí),mysql jdbc url樣例如下:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
&autoReconnect=true&failOverReadOnly=false
其他參數(shù)請(qǐng)參見(jiàn)mysql jdbc官方文檔:?MySQL Connector/J Documentation