Sun公司開發一組標準api,他們只是接口,並沒有提供實現類,由資料庫廠商提供實現類,即驅動程式
jdbc操作過程:1.jar套件導入;2.定義記錄的類別(如Student類別);3.連接的取得;4.sql的執行。
// sql的执行 // insert private static int insert(Student student) { Connection conn = getConn(); int i = 0; String sql = "insert into students (Name,Sex,Age) values(?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, student.getName()); pstmt.setString(2, student.getSex()); pstmt.setString(3, student.getAge()); i = pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; } // update private static int update(Student student) { Connection conn = getConn(); int i = 0; String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; } // select xx from tableName private static Integer getAll() { Connection conn = getConn(); String sql = "select * from students"; PreparedStatement pstmt; try { pstmt = (PreparedStatement)conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); System.out.println("============================"); while (rs.next()) { for (int i = 1; i <= col; i++) { System.out.print(rs.getString(i) + "\t"); if ((i == 2) && (rs.getString(i).length() < 8)) { System.out.print("\t"); } } System.out.println(""); } System.out.println("============================"); } catch (SQLException e) { e.printStackTrace(); } return null; } // delete private static int delete(String name) { Connection conn = getConn(); int i = 0; String sql = "delete from students where Name='" + name + "'"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; }
測試:
// 测试 public static void main(String args[]) { JDBCOperation.getAll(); JDBCOperation.insert(new Student("Achilles", "Male", "14")); JDBCOperation.getAll(); JDBCOperation.update(new Student("Bean", "", "7")); JDBCOperation.delete("Achilles"); JDBCOperation.getAll(); }
輸出結果:
============================ 1 Ender male 8 2 Bean male 6 3 Petra fema 9 4 Peter male 9 5 _Graff male 40 6 GOD fema 255 ============================ ============================ 1 Ender male 8 2 Bean male 6 3 Petra fema 9 4 Peter male 9 5 _Graff male 40 6 GOD fema 255 7 Achilles Male 14 ============================ resutl: 1 resutl: 1 ============================ 1 Ender male 8 2 Bean male 7 3 Petra fema 9 4 Peter male 9 5 _Graff male 40 6 GOD fema 255 ============================
在上述資料庫增刪改查的過程中,可以發現其共通性部分,即通用的流程:
(1)建立Connection物件、SQL查詢指令字串;
(2)對Connection物件傳入SQL查詢指令,取得PreparedStatement物件;
(3)對PreparedStatement物件執行executeUpdate()或executeQurey()取得結果;
(4)先後關閉PreparedStatement物件和Connection物件。
可見,使用JDBC時,最常打交道的是Connection、PreparedStatement這兩個類,以及select中的ResultSet類別。
1.每次SQL作業都需要建立和關閉連接,這勢必會消耗大量的資源開銷,如何避免?
分析:可以採用連線池,對連線進行統一維護,不必每次都建立和關閉。事實上這是許多對JDBC進行封裝的工具所採用的。
2.Java程式碼中,傳入的資料格式與資料庫定義不同怎麼辦?如把Java的String物件賦值給資料庫的tinyint屬性。
分析:執行SQL語句時,資料庫會嘗試轉換。根據我的實驗,如果用內容為純字母的String物件傳入tinyint的age屬性時,就會轉換成0。具體轉化規則應該和資料庫有關。
以上是深入淺出JAVA開發中JDBC的操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!