資料庫之-------Mysql(JDBC實現&解決儲存亂碼問題)

來源:互聯網
上載者:User

標籤:建立資料庫   password   public   mysql   import   


資料庫之-------Mysql(JDBC實現&解決儲存亂碼問題)



1、亂碼問題的解決很簡單啦!

    首先在建立資料庫的時候要指定字元集為utf-8,然後再進行JDBC編程的時候,在下面代碼的url後面加上參數characterEncoding即可!更多關於url參數的設定可以查看mysql官網文檔:

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]...

執行個體:
jdbc:mysql://localhost:3306/sakila?profileSQL=true

package java_data_jdbc;



現只列舉幾個重要的參數,如下表所示:

參數名稱 參數說明 預設值 最低版本要求
user 資料庫使用者名稱(用於串連資料庫)
所有版本
password 使用者密碼(用於串連資料庫)
所有版本
useUnicode 是否使用Unicode字元集,如果參數characterEncoding設定為gb2312或gbk,本參數值必須設定為true false 1.1g
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

 

對應中文環境,通常mysql串連URL可以設定為:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false

在使用資料庫連接池的情況下,最好設定如下兩個參數:
autoReconnect=true&failOverReadOnly=false

需要注意的是,在xml設定檔中,url中的&符號需要轉義成&。比如在tomcat的server.xml中設定資料庫串連池時,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";/* * 這個url的格式後面是可以帶很多參數的,詳細請參考mysql官網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 {/* * 這個驅動的寫法有兩種,兩者是繼承關係,還有是: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("序號: "+id + "  " +"姓名: "+ name + "  "+ age+"歲!");}} catch (Exception e) {e.printStackTrace();}}}

部分資料來源:http://elf8848.iteye.com/blog/1684414

本文出自 “11941149” 部落格,請務必保留此出處http://11951149.blog.51cto.com/11941149/1850911

資料庫之-------Mysql(JDBC實現&解決儲存亂碼問題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.