JDBC MYSQL 學習筆記(一) JDBC 基本使用

來源:互聯網
上載者:User

標籤:mysql   資料庫   java   jdbc   

1、JDBC簡介

SUN公司為了簡化、統一對資料庫的操作,定義了一套Java操作資料庫的規範,稱之為JDBC。 JDBC全稱為:Java Data Base Connectivity(java資料庫連接),它主要由介面組成。 組成JDBC的2個包:java.sql  javax.sql

開發JDBC應用需要以上2個包的支援外,還需要匯入相應JDBC的資料庫實現(即資料庫驅動)。


2、使用JDBC的步驟——第一個JDBC程式

需求:編程從user表中讀取資料,並列印在命令列視窗中。
     (1) 搭建實驗環境 :
         a、在mysql中建立一個資料庫,並建立user表,同時插入資料到表中。
         b、建立一個Java工程,並匯入資料庫驅動。
    (2) 編寫程式,在程式中載入資料庫驅動

         a、方式一:DriverManager. registerDriver(Driver driver)          b、方式二:Class.forName(“com.mysql.jdbc.Driver”);
    (3) 建立串連(Connection)
          
Connection conn = DriverManager.getConnection(url,user,pass);

    (4) 建立用於向資料庫發送SQL的Statement對象,並發送sql
 
         Statement st = conn.createStatement();          ResultSet rs = st.excuteQuery(sql);
 
    (5) 從代表結果集的ResultSet中取出資料,列印到命令列視窗
 
    (6) 斷開與資料庫的串連,並釋放相關資源

dome:

</pre><p><pre name="code" class="java">import java.sql.*;import com.sun.org.apache.regexp.internal.recompile;public class dome {/** * @param args */public static void main(String[] args) throws SQLException,ClassNotFoundException{// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/skyfin";String username = "root";String password = "skyfin";//1.載入驅動//DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");//2擷取串連Connection connection = DriverManager.getConnection(url,username,password);//3.擷取向資料庫發sql語句的statament對象Statement stat = connection.createStatement();//4.向資料庫發送sql,擷取資料庫返回的結果集ResultSet rsResultSet = stat.executeQuery("select * from user");//5.從結果集中擷取資料while (rsResultSet.next()) {System.out.println("id = "+ rsResultSet.getObject("id"));System.out.println("name = "+ rsResultSet.getObject("name"));System.out.println("password = "+ rsResultSet.getObject("password"));} //6.釋放資源(釋放連結)rsResultSet.close();stat.close();connection.close();}}

3、DriverManager ——載入資料庫驅動

    Jdbc程式中的DriverManager用於載入驅動,並建立與資料庫的連結,這個API的常用方法:

    DriverManager.registerDriver(new Driver());    DriverManager.getConnection(url, user, password);
注意:在實際開發中並不推薦採用registerDriver方法註冊驅動。原因有二:
  一、查看Driver的原始碼可以看到,如果採用此種方式,會導致驅動程式註冊兩次,也就是在記憶體中會有兩個Driver對象。
二、程式依賴mysql的api,脫離mysql的jar包,程式將無法編譯,將來程式切換底層資料庫將會非常麻煩。
 推薦方式:

Class.forName(“com.mysql.jdbc.Driver”);
採用此種方式不會導致驅動對象在記憶體中重複出現,並且採用此種方式,程式僅僅只需要一個字串,不需要依賴具體的驅動,使程式的靈活性更高。     
        同樣,在開發中也不建議採用具體的驅動類型指向getConnection方法返回的connection對象。

4、資料庫URL ——標識資料庫的位置

URL用於標識資料庫的位置,程式員通過URL地址告訴JDBC程式串連哪個資料庫,

MySql 資料庫的URL寫法為: jdbc:mysql:[]//localhost:3306/test ?參數名:參數值

  常用資料庫URL地址的寫法:
Oracle:jdbc:oracle:thin:@localhost:1521:skyfin
SqlServer:jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=skyfin
MySql:jdbc:mysql://localhost:3306/skyfin
  Mysql的url地址的簡寫形式: jdbc:mysql://skyfin
常用屬性:useUnicode=true&characterEncoding=UTF-8

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly

5、Connection ——代表資料庫的連結    Jdbc程式中的Connection,它用於代表資料庫的連結。Connection 是資料庫編程中最重要的一個對象,用戶端與資料庫所有互動都是通過Connection 對象完成的,這個對象的常用方法:
        (1) createStatement():建立向資料庫發送sql的statement對象。
        (2) prepareStatement(sql) :建立向資料庫發送先行編譯sql的PrepareSatement對象。
        (3) prepareCall(sql):建立執行預存程序的callableStatement對象。
        (4) setAutoCommit(boolean autoCommit):設定事務是否自動認可。
        (5) commit() :在連結上提交事務。
        (6) rollback() :在此連結上復原事務。
 6、Statement  ——向資料庫發送SQL語句    Jdbc程式中的Statement對象用於向資料庫發送SQL語句, Statement對象常用方法:
        (1) executeQuery(String sql) :用於向資料庫發送查詢語句。
        (2) executeUpdate(String sql):用於向資料庫發送insert、update或delete語句
        (3) execute(String sql):用於向資料庫發送任意sql語句
        (4) addBatch(String sql) :把多條sql語句放到一個批處理中。
        (5) executeBatch():向資料庫發送一批sql語句執行。
        (6) clearBatch() :清空此 Statement 對象的當前 SQL 命令列表。

7、ResultSet  ——代表Sql語句的執行結果        Jdbc程式中的ResultSet用於代表Sql語句的執行結果。Resultset封裝執行結果時,採用的類似於表格的方式。ResultSet 對象維護了一個指向表格資料行的遊標,初始的時候,遊標在第一行之前,調用ResultSet.next() 方法,可以使遊標指向具體的資料行,進行調用方法擷取該行的資料。
    (1) ResultSet提供了對結果集進行滾動的方法:            a、next():移動到下一行
            b、Previous():移動到前一行
            c、absolute(int row):移動到指定行
            d、beforeFirst():移動resultSet的最前面。
            e、 afterLast() :移動到resultSet的最後面。
    (2) ResultSet既然用於封裝執行結果的,所以該對象提供了用於擷取資料的get方法:       擷取任意類型的資料
                getObject(int index)
                getObject(string columnName)
        擷取指定類型的資料,例如:
                getString(int index)
                getString(String columnName)
                其他擷取指定類型資料的方法見下表:
常用資料類型轉換表:


8、釋放資源 ——釋放與資料庫進行互動的對象         Jdbc程式運行完後,切記要釋放程式在運行過程中,建立的那些與資料庫進行互動的對象,這些對象通常是ResultSet, Statement和Connection對象。 
        特別是Connection對象,它是非常稀有的資源,用完後必須馬上釋放,如果Connection不能及時、正確的關閉,極易導致系統宕機。Connection的使用原則是盡量晚建立,盡量早的釋放。 
        為確保資源釋放代碼能運行,資源釋放代碼也一定要放在finally語句中。
9、用JDBC對資料庫進行CRUD         jdbc中的statement對象用於向資料庫發送SQL語句,想完成對資料庫的增刪改查,只需要通過這個對象向資料庫發送增刪改查語句即可。 
        Statement對象的executeUpdate方法,用於向資料庫發送增、刪、改的sql語句,executeUpdate執行完後,將會返回一個int整數(即增刪改語句導致了資料庫幾行資料發生了變化)。 
        Statement.executeQuery方法用於向資料庫發送查詢語句,executeQuery方法返回代表查詢結果的ResultSet對象。

public static void main(String[] args) throws SQLException,ClassNotFoundException{// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/skyfin";String username = "root";String password = "skyfin";//DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");Connection  connection = DriverManager.getConnection(url,username,password);Statement stat = connection.createStatement();/* * 執行尋找操作 */ResultSet rsResultSet = stat.executeQuery("select * from user");while (rsResultSet.next()) {System.out.println("id = "+ rsResultSet.getObject("id"));System.out.println("name = "+ rsResultSet.getObject("name"));System.out.println("password = "+ rsResultSet.getObject("password"));}/* * 執行插入操作 */String sql = "insert into user(id,name,password) value(6,"+"'staff'"+","+"'staff'"+")";System.out.println(sql);int statentnum = stat.executeUpdate(sql);if (statentnum>0) {System.out.println("insert ok");}/* * 執行更新操作 */sql = "update user set name = 'skstaff' where name = 'staff'";System.out.println(sql);statentnum = stat.executeUpdate(sql);if (statentnum>0) {System.out.println("update ok");}/* * 執行刪除操作 */sql = "delete from user where name = 'skstaff'";System.out.println(sql);statentnum = stat.executeUpdate(sql);if (statentnum>0) {System.out.println("delete ok");}/* * 資源的釋放 */rsResultSet.close();stat.close();connection.close();}

10、用JDBC的PreparedStatement啟動事務使用批處理executeBatch()JDBC使用MySQL處理大資料的時候,自然而然的想到要使用批處理,
普通的執行過程是:每處理一條資料,就訪問一次資料庫;
而批處理是:累積到一定數量,再一次性提交到資料庫,減少了與資料庫的互動次數,所以效率會大大提高
至於事務:事務指邏輯上的一組操作,組成這組操作的各個單元,要不全部成功,要不全部不成功,預設是關閉事務的。

1. PreparedStatement使用批處理 executeBatch()        1.1. 不使用executeBatch(),而使用executeUpdate(),速度很慢

public static void main(String[] args) throws  SQLException,ClassNotFoundException{// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/skyfin";String username = "root";String password = "skyfin";//DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");Connection  connection = DriverManager.getConnection(url,username,password);String sql = "insert into user1(id,name) value(?,?)";PreparedStatement preparedStatement = connection.prepareStatement(sql);for(int i = 0;i<10000;i++){preparedStatement.setInt(1, i);preparedStatement.setString(2, "skyfin"+i);preparedStatement.executeUpdate();}}

1.2. 而使用executeBatch()

public static void main(String[] args) throws  SQLException,ClassNotFoundException{// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/skyfin";String username = "root";String password = "skyfin";//DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");Connection  connection = DriverManager.getConnection(url,username,password);String sql = "insert into user1(id,name) value(?,?)";PreparedStatement preparedStatement = connection.prepareStatement(sql);for(int i = 0;i<10000;i++){preparedStatement.setInt(1, i);preparedStatement.setString(2, "skyfin"+i);//preparedStatement.executeUpdate();/* * 使用executeBatch() */preparedStatement.addBatch();}//執行批處理preparedStatement.executeBatch();}

 注意:1. 如果使用了 addBatch() -> executeBatch() 還是很慢,那就得使用到這個參數了rewriteBatchedStatements=true (啟動批處理操作)
在資料庫連接URL後面加上這個參數:      String dbUrl =  "jdbc:mysql://localhost:3306/User? rewriteBatchedStatements=true";
     2. 在代碼中,pstmt的位置不能亂放,必須放在迴圈體外2. 啟用交易處理

public static void main(String[] args) throws  SQLException,ClassNotFoundException{// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/skyfin";String username = "root";String password = "skyfin";//DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");Connection  connection = DriverManager.getConnection(url,username,password);//關閉自動認可connection.setAutoCommit(false);String sql = "update user1 set name = ?where id = ?";PreparedStatement preparedStatement = connection.prepareStatement(sql);for(int i = 0;i<10000;i++){preparedStatement.setString(1, "loco"+i);preparedStatement.setInt(2, i);//preparedStatement.executeUpdate();/* * 使用executeBatch() */preparedStatement.addBatch();}//執行批處理preparedStatement.executeBatch();preparedStatement.close();//執行完後手動提交事務connection.commit();//開啟自動認可connection.setAutoCommit(true);connection.close();}

3. 事務和批處理混合使用
public static void main(String[] args) throws  SQLException,ClassNotFoundException{// TODO Auto-generated method stubString url = "jdbc:mysql://localhost:3306/skyfin";String username = "root";String password = "skyfin";//DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");Connection  connection = DriverManager.getConnection(url,username,password);//關閉自動認可connection.setAutoCommit(false);String sql = "update user1 set name = ?where id = ?";PreparedStatement preparedStatement = connection.prepareStatement(sql);for(int i = 0;i<10000;i++){preparedStatement.setString(1, "skyfin"+i);preparedStatement.setInt(2, i);//preparedStatement.executeUpdate();/* * 使用executeBatch() */if (i>0&&i%500 == 0) {preparedStatement.executeBatch();//如果不想出錯後,完全沒保留資料,則可以沒執行一次提交一次,但得保證資料不會重複connection.commit();}preparedStatement.addBatch();}//執行批處理preparedStatement.executeBatch();preparedStatement.close();//執行完後手動提交事務connection.commit();//開啟自動認可connection.setAutoCommit(true);connection.close();}
11、SQL 注入的防範  SQL 注入是使用者利用某些系統沒有對輸入資料進行充分的檢查,從而進行惡意破壞的行為。
1、statement存在sql注入攻擊問題,例如登陸使用者名稱採用‘ or 1=1 or username=‘
2、對於防範 SQL 注入,可以採用PreparedStatement取代Statement。
備忘:本例只是最基本的防止SQL注入方式,其他情況還請查閱資料。

12、PreparedStatement    PreperedStatement是Statement的孩子,它的執行個體對象可以通過調用Connection.preparedStatement()方法獲得,相對於Statement對象而言的優勢:
    (1) 防止SQL注入:PreperedStatement可以避免SQL注入的問題。
    (2) 先行編譯SQL語句:Statement會使資料庫頻繁編譯SQL,可能造成資料庫緩衝區溢位。PreparedStatement 可對SQL進行先行編譯,從而提高資料庫的執行效率。
    (3) 使用預留位置簡化語句:並且PreperedStatement對於sql中的參數,允許使用預留位置的形式進行替換,簡化sql語句的編寫。 (例如多次迴圈插入資料)

public List<Customer> getAll(){         Connection conn = null;        PreparedStatement st = null;        ResultSet rs = null;        try{              conn = JdbcUtils.getConnection();              String sql = "select * from customer";              st = conn.prepareStatement(sql);              rs = st.executeQuery();              List list = new ArrayList();              while(rs.next()){                     Customer c = new Customer();                     c.setBirthday(rs.getDate("birthday"));                     c.setCellphone(rs.getString("cellphone"));                     c.setDescription(rs.getString("description"));                     c.setEmail(rs.getString("email"));                     c.setGender(rs.getString("gender"));                     c.setId(rs.getString("id"));                     c.setName(rs.getString("name"));                     c.setPreference(rs.getString("preference"));                     c.setType(rs.getString("type"));                     list.add(c);              }              return list;        }catch (Exception e) {              throw new DaoException(e);        }finally{              JdbcUtils.release(conn, st, rs);        }  } 



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

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