jdbc連結mysql轉

來源:互聯網
上載者:User

標籤:

  1. JDBC串連資料庫   
  2. •建立一個以JDBC串連資料庫的程式,包含7個步驟:   
  3.  1、載入JDBC驅動程式:   
  4.     在串連資料庫之前,首先要載入想要串連的資料庫的驅動到JVM(Java虛擬機器),   
  5.     這通過java.lang.Class類的靜態方法forName(String  className)實現。   
  6.     例如:   
  7.     try{   
  8.     //載入MySql的驅動類   
  9.     Class.forName("com.mysql.jdbc.Driver") ;   
  10.     }catch(ClassNotFoundException e){   
  11.     System.out.println("找不到驅動程式類 ,載入驅動失敗!");   
  12.     e.printStackTrace() ;   
  13.     }   
  14.    成功載入後,會將Driver類的執行個體註冊到DriverManager類中。   
  15.  2、提供JDBC串連的URL   
  16.    •串連URL定義了串連資料庫時的協議、子協議、資料來源標識。   
  17.     •書寫形式:協議:子協議:資料來源標識   
  18.     協議:在JDBC中總是以jdbc開始   
  19.     子協議:是橋串連的驅動程式或是資料庫管理系統名稱。   
  20.     資料來源標識:標記找到資料庫來源的地址與串連連接埠。   
  21.     例如:(MySql的串連URL)   
  22.     jdbc:mysql:   
  23.         //localhost:3306/test?useUnicode=true&characterEncoding=gbk ;   
  24.    useUnicode=true:表示使用Unicode字元集。如果characterEncoding設定為   
  25.    gb2312或GBK,本參數必須設定為true 。characterEncoding=gbk:字元編碼方式。   
  26.  3、建立資料庫的串連   
  27.     •要串連資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,   
  28.      該對象就代表一個資料庫的串連。   
  29.     •使用DriverManager的getConnectin(String url , String username ,    
  30.     String password )方法傳入指定的欲串連的資料庫的路徑、資料庫的使用者名稱和   
  31.      密碼來獲得。   
  32.      例如:   
  33.      //串連MySql資料庫,使用者名稱和密碼都是root   
  34.      String url = "jdbc:mysql://localhost:3306/test" ;    
  35.      String username = "root" ;   
  36.      String password = "root" ;   
  37.      try{   
  38.     Connection con =    
  39.              DriverManager.getConnection(url , username , password ) ;   
  40.      }catch(SQLException se){   
  41.     System.out.println("資料庫連接失敗!");   
  42.     se.printStackTrace() ;   
  43.      }   
  44.  4、建立一個Statement   
  45.     •要執行SQL語句,必須獲得java.sql.Statement執行個體,Statement執行個體分為以下3  
  46.      種類型:   
  47.       1、執行靜態SQL語句。通常通過Statement執行個體實現。   
  48.       2、執行動態SQL語句。通常通過PreparedStatement執行個體實現。   
  49.       3、執行資料庫預存程序。通常通過CallableStatement執行個體實現。   
  50.     具體的實現方式:   
  51.         Statement stmt = con.createStatement() ;   
  52.        PreparedStatement pstmt = con.prepareStatement(sql) ;   
  53.        CallableStatement cstmt =    
  54.                             con.prepareCall("{CALL demoSp(? , ?)}") ;   
  55.  5、執行SQL語句   
  56.     Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate   
  57.    和execute   
  58.     1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句   
  59.         ,返回一個結果集(ResultSet)對象。   
  60.      2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或   
  61.         DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等   
  62.      3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的   
  63.         語句。   
  64.    具體實現的代碼:   
  65.           ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;   
  66.     int rows = stmt.executeUpdate("INSERT INTO ...") ;   
  67.     boolean flag = stmt.execute(String sql) ;   
  68.  6、處理結果   
  69.     兩種情況:   
  70.      1、執行更新返回的是本次操作影響到的記錄數。   
  71.      2、執行查詢返回的結果是一個ResultSet對象。   
  72.     • ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些   
  73.       行中資料的訪問。   
  74.     • 使用結果集(ResultSet)對象的存取方法擷取資料:   
  75.      while(rs.next()){   
  76.          String name = rs.getString("name") ;   
  77.     String pass = rs.getString(1) ; // 此方法比較高效   
  78.      }   
  79.     (列是從左至右編號的,並且從列1開始)   
  80.  7、關閉JDBC對象    
  81.      操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲   
  82.      明順序相反:   
  83.      1、關閉記錄集   
  84.      2、關閉聲明   
  85.      3、關閉連線物件   
  86.           if(rs != null){   // 關閉記錄集   
  87.         try{   
  88.             rs.close() ;   
  89.         }catch(SQLException e){   
  90.             e.printStackTrace() ;   
  91.         }   
  92.           }   
  93.           if(stmt != null){   // 關閉聲明   
  94.         try{   
  95.             stmt.close() ;   
  96.         }catch(SQLException e){   
  97.             e.printStackTrace() ;   
  98.         }   
  99.           }   
  100.           if(conn != null){  // 關閉連線物件   
  101.          try{   
  102.             conn.close() ;   
  103.          }catch(SQLException e){   
  104.             e.printStackTrace() ;   
  105.          }   
  106.           }  

jdbc連結mysql轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.