JDBC API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC API相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%bf%ae%e6%94%b9' title='淇敼'>淇敼   jdbc   eating   ddl   where   ack   etc   database   host   

閫氳繃浣跨敤 JDBC Statement, CallableStatement 鍜?PreparedStatement 鎺ュ彛瀹氫箟鐨勬柟娉曞拰灞炴€э紝浣垮彲浠ヤ娇鐢?SQL 鎴?PL/SQL 鍛戒护鍜屼粠鏁版嵁搴撴帴鏀舵暟鎹€?/p>

鎺ュ彛搴旂敤鍦烘櫙
Statement 褰撳湪杩愯鏃朵娇鐢ㄩ潤鎬?SQL 璇彞鏃讹紙Statement 鎺ュ彛涓嶈兘鎺ユ敹鍙傛暟锛?/td>
CallableStatement 褰撹璁块棶鏁版嵁搴撲腑鐨勫瓨鍌ㄨ繃绋嬫椂锛圕allableStatement 瀵硅薄鐨勬帴鍙h繕鍙互鎺ユ敹杩愯鏃惰緭鍏ュ弬鏁帮級
PreparedStatement 褰撹鍒掑娆′娇鐢?SQL 璇彞鏃讹紙PreparedStatement 鎺ュ彛鎺ユ敹鍦ㄨ繍琛屾椂杈撳叆鍙傛暟锛?/td>

1.浣跨敤statement

绗竴姝ヨ偗瀹氭槸鍒涘缓涓€涓?Statement 瀵硅薄浜嗐€傛垜浠渶瑕佷娇鐢?Connection 瀵硅薄鐨?createStatement() 鏂规硶杩涜鍒涘缓銆?/p>

statement鏂规硶锛?/p>

鏂规硶璇存槑
boolean execute(String SQL) 濡傛灉 ResultSet 瀵硅薄鍙互琚绱㈣繑鍥炲竷灏斿€?true锛屽惁鍒欒繑鍥?false銆備娇鐢ㄨ繖涓柟娉曟潵鎵ц SQL DDL 璇彞锛屾垨褰撻渶瑕佷娇鐢ㄧ湡姝g殑鍔ㄦ€?SQL
int executeUpdate(String SQL) 鐢ㄤ簬鎵ц INSERT銆乁PDATE 鎴?DELETE 璇彞浠ュ強 SQLDDL锛堟暟鎹畾涔夎瑷€锛夎鍙ャ€傝繑鍥炲€兼槸涓€涓暣鏁帮紝鎸囩ず鍙楀奖鍝嶇殑琛屾暟锛堝嵆鏇存柊璁℃暟锛?/td>
ResultSet executeQuery(String SQL) 杩斿洖 ResultSet 瀵硅薄銆傜敤浜庝骇鐢熷崟涓粨鏋滈泦鐨勮鍙ワ紝渚嬪 SELECT 璇彞

渚嬪瓙锛?/p>

import java.sql.*;

public class JdbcTest {
   // JDBC 椹卞姩鍣ㄧ殑鍚嶇О鍜屾暟鎹簱鍦板潃
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "jdbc:mysql://localhost/EXAMPLE";


   static final String USER = "root";
   static final String PASS = "";杩欓噷涓鸿嚜宸辩殑sql鐨勫瘑鐮?
   public static void main(String[] args) {
       Connection conn = null;
       PreparedStatement stmt = null;
       try{
           //娉ㄥ唽 JDBC 椹卞姩鍣?/span>
           Class.forName("com.mysql.jdbc.Driver");

           //鎵撳紑杩炴帴
           System.out.println("Connecting to database...");
           conn = DriverManager.getConnection(DB_URL,USER,PASS);

           //鎵ц鏌ヨ
           System.out.println("Creating statement...");
           //杩欓噷鎴戜滑瑕佹洿鏀逛竴涓悓瀛︾殑骞撮緞锛屽弬鏁板緟瀹?/span>
           String sql = "UPDATE Students set age=? WHERE id=?";
           stmt = conn.prepareStatement(sql);

           //灏嗗€肩粦瀹氬埌鍙傛暟锛屽弬鏁颁粠宸﹁嚦鍙冲簭鍙蜂负1锛?...
           stmt.setInt(1, 22);  // 缁戝畾 age 鐨勫€?搴忓彿涓?)
           stmt.setInt(2, 1); // 缁戝畾 ID 鐨勫€?
           // 鏇存柊 ID 涓?鐨勫悓瀛︾殑骞撮緞
           int rows = stmt.executeUpdate();
           System.out.println("琚奖鍝嶇殑琛屾暟 : " + rows );

           // 鏌ヨ鎵€鏈夎褰曪紝骞舵樉绀?
           sql = "SELECT id, name, age FROM Students";
           ResultSet rs = stmt.executeQuery(sql);

           //澶勭悊缁撴灉闆?/span>
           while(rs.next()){
               //妫€绱?/span>
               int id  = rs.getInt("id");
               int age = rs.getInt("age");
               String name = rs.getString("name");


               //鏄剧ず
               System.out.print("ID: " + id);
               System.out.print(", Age: " + age);
               System.out.print(", Name: " + name);
               System.out.println();
           }
           //娓呯悊
           rs.close();
           stmt.close();
           conn.close();
       }catch(SQLException se){
           se.printStackTrace();
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           try{
               if(stmt!=null)
                   stmt.close();
           }catch(SQLException se2){
           }
    try{
         if(conn!=null)
                 conn.close();
      }catch(SQLException se){
              se.printStackTrace();
          }
       }
           System.out.println("Goodbye!");
   }
}

2.PreparedStatement鍜孋allableStatement 

涓夌绫诲瀷鐨勫弬鏁版湁锛欼N锛孫UT 鍜?INOUT銆?/p>

PreparedStatement 瀵硅薄鍙娇鐢?IN 鍙傛暟銆?/p>

CallableStatement 瀵硅薄鍙互浣跨敤鎵€鏈変笁涓?/p>

鍙傛暟鎻忚堪
IN 瀹冪殑鍊兼槸鍦ㄥ垱寤?SQL 璇彞鏃舵湭鐭ョ殑鍙傛暟锛屽皢 IN 鍙傛暟浼犵粰 CallableStatement 瀵硅薄鏄€氳繃 setXXX() 鏂规硶瀹屾垚鐨?/td>
OUT 鍏跺€肩敱瀹冭繑鍥炵殑 SQL 璇彞鎻愪緵鐨勫弬鏁般€備粠 OUT 鍙傛暟鐨?getXXX() 鏂规硶妫€绱㈠€?/td>
INOUT 鍚屾椂鎻愪緵杈撳叆鍜岃緭鍑哄€肩殑鍙傛暟锛岀粦瀹氱殑 setXXX() 鏂规硶鐨勫彉閲忥紝骞朵娇鐢?getXXX() 鏂规硶妫€绱㈠€?/td>

 

鍦?JDBC 涓皟鐢ㄥ瓨鍌ㄨ繃绋嬬殑璇硶濡備笅鎵€绀恒€傛敞鎰忥紝鏂规嫭鍙疯〃绀哄叾闂寸殑鍐呭鏄彲閫夐」锛涙柟鎷彿鏈韩骞朵笉鏄娉曠殑缁勬垚閮ㄤ唤銆?/p>

杩斿洖缁撴灉鍙傛暟鐨勮繃绋嬬殑璇硶涓猴細

{? = call 瀛樺偍杩囩▼鍚峓(?, ?, ...)]}

涓嶅甫鍙傛暟鐨勫瓨鍌ㄨ繃绋嬬殑璇硶绫讳技锛?/p>

{call 瀛樺偍杩囩▼鍚峿

 

鍒涘缓缁撴灉闆?/p>

try {
   Statement stmt = conn.createStatement(
                           ResultSet.TYPE_SCROLL_INSENSITIVE,
娓告爣鍙互鍚戝墠鍜屽悜鍚庢粴鍔紝浣嗕笉鍙婃椂鏇存柊锛屽氨鏄鏋滄暟鎹簱閲岀殑鏁版嵁淇敼杩囷紝骞朵笉鍦?ResultSet 涓弽搴斿嚭鏉?/td>

                           ResultSet.CONCUR_UPDATABLE);
}
catch(Exception ex) {
   ....
}
finally {
   ....
}

 

以上是关于JDBC API的主要内容,如果未能解决你的问题,请参考以下文章

面试常用的代码片段

mysql jdbc源码分析片段 和 Tomcat's JDBC Pool

onActivityResult 未在 Android API 23 的片段上调用

Kylin实战—— REST API演示和Kylin_JDBC演示代码

导航到另一个片段时触发 API 调用

Flink实时统计入MySQL