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 的片段上调用