MySql連接字符串的說明
下文對MySql連接字符串的相關(guān)參數(shù)及格式進(jìn)行了詳細(xì)的說明,供您參考,如果對您MySql連接字符串感興趣的話,不妨一看。
mysql JDBC 驅(qū)動常用的有兩個(gè),一個(gè)是gjt(Giant Java Tree)組織提供的mysql驅(qū)動,其JDBC Driver名稱(JAVA類名)為:org.gjt.mm.mysql.Driver
另一個(gè)是mysql官方提供的JDBC Driver,其JAVA類名為:com.mysql.jdbc.Driver
mysql JDBC URL格式如下:
jdbc:mysql://[host:port]/[database][?參數(shù)名1][=參數(shù)值1][&參數(shù)名2][=參數(shù)值2]...
參數(shù)名稱 | 參數(shù)說明 | 缺省值 | 最低版本要求 |
user | 數(shù)據(jù)庫用戶名(用于連接數(shù)據(jù)庫) | 所有版本 | |
password | 用戶密碼(用于連接數(shù)據(jù)庫) | 所有版本 | |
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ù)庫連接異常中斷時(shí),是否自動重新連接? | false | 1.1 |
autoReconnectForPools | 是否使用針對數(shù)據(jù)庫連接池的重連策略 | false | 3.1.3 |
failOverReadOnly | 自動重連成功后,連接是否設(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ù)庫服務(wù)器建立socket連接時(shí)的超時(shí),單位:毫秒。 0表示永不超時(shí),適用于JDK 1.4及更高版本 | 0 | 3.0.1 |
socketTimeout | socket操作(讀寫)超時(shí),單位:毫秒。 0表示永不超時(shí) | 0 | 3.0.1 |
對應(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ù)庫連接池的情況下,最好設(shè)置如下兩個(gè)參數(shù):
autoReconnect=true&failOverReadOnly=false
MySql連接字符串需要注意的是,在xml配置文件中,url中的&符號需要轉(zhuǎn)義。比如在tomcat的server.xml中配置數(shù)據(jù)庫連接池時(shí),mysql jdbc url樣例如下:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
&autoReconnect=true&failOverReadOnly=false
【編輯推薦】
mysql建主從服務(wù)器的實(shí)現(xiàn)方法