數(shù)據(jù)庫(kù)之-------Mysql(JDBC實(shí)現(xiàn)&解決存儲(chǔ)亂碼問(wèn)題)

數(shù)據(jù)庫(kù)之-------MySQL(JDBC實(shí)現(xiàn)&解決存儲(chǔ)亂碼問(wèn)題)

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、龍泉驛網(wǎng)站維護(hù)、網(wǎng)站推廣。

1、亂碼問(wèn)題的解決很簡(jiǎn)單啦!

    首先在建立數(shù)據(jù)庫(kù)的時(shí)候要指定字符集為utf-8,然后再進(jìn)行JDBC編程的時(shí)候,在下面代碼的url后面加上參數(shù)characterEncoding即可!更多關(guān)于url參數(shù)的設(shè)置可以查看mysql官網(wǎng)文檔:

http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html

2、MySQL的 JDBC URL 格式 for  Connector/J 如下例:

jdbc:mysql://[host][,failoverhost...][:port]/[database] 
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

jdbc:mysql://[host:port],[host:port].../[database] 
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

實(shí)例:
jdbc:mysql://localhost:3306/sakila?profileSQL=true

package java_data_jdbc;

現(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è)置為truefalse1.1g
characterEncoding當(dāng)useUnicode設(shè)置為true時(shí),指定字符編碼。比如可設(shè)置為gb2312或gbkfalse1.1g
autoReconnect當(dāng)數(shù)據(jù)庫(kù)連接異常中斷時(shí),是否自動(dòng)重新連接?false1.1
autoReconnectForPools是否使用針對(duì)數(shù)據(jù)庫(kù)連接池的重連策略false3.1.3
failOverReadOnly自動(dòng)重連成功后,連接是否設(shè)置為只讀?true3.0.12
maxReconnectsautoReconnect設(shè)置為true時(shí),重試連接的次數(shù)31.1
initialTimeoutautoReconnect設(shè)置為true時(shí),兩次重連之間的時(shí)間間隔,單位:秒21.1
connectTimeout和數(shù)據(jù)庫(kù)服務(wù)器建立socket連接時(shí)的超時(shí),單位:毫秒。 0表示永不超時(shí),適用于JDK 1.4及更高版本03.0.1
socketTimeoutsocket操作(讀寫(xiě))超時(shí),單位:毫秒。 0表示永不超時(shí)03.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

3、代碼如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Jdbc_01 {
	
	public static void main(String[] args) {
		
		String userName = "root";
		String password = "root";
		/*
		 * 這個(gè)url的格式后面是可以帶很多參數(shù)的,詳細(xì)請(qǐng)參考mysql官網(wǎng)http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
		 */
		String url = "jdbc:mysql://localhost:3306?characterEncoding=utf-8&useSSL=true";
		String sql1 = "Select * from Class";
		String sql = "INSERT INTO Class (name,age) VALUES ('你好','21')";

		try {
			
			/*
			 * 這個(gè)驅(qū)動(dòng)的寫(xiě)法有兩種,兩者是繼承關(guān)系,還有是:org.gjt.mm.mysql.Driver
			 */
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection conn = DriverManager.getConnection(url, userName, password);
			
			Statement stmt = conn.createStatement();
			
			stmt.execute("use student;");
			stmt.execute(sql);
			
			ResultSet res = stmt.executeQuery(sql1);
			
			while(res.next()){
				String id = res.getString("id");
				String name = res.getString("name");
				int age = res.getInt("age");
				
				System.out.println("序號(hào): "+id + "  " +"姓名: "+ name + "  "+ age+"歲!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

部分資料來(lái)源:http://elf8848.iteye.com/blog/1684414

網(wǎng)頁(yè)名稱:數(shù)據(jù)庫(kù)之-------Mysql(JDBC實(shí)現(xiàn)&解決存儲(chǔ)亂碼問(wèn)題)
URL分享:http://muchs.cn/article0/jpeiio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)