JDBC中對資料庫做增刪改操作
阿新 • • 發佈:2020-11-21
對資料庫中的操作只有倆種方式,增刪改的操作是一種,查詢時一種,增刪改沒有結果集,而查詢有結果集,接下來我就先介紹增刪改的通用操作程式碼
public void testInsert(){ Connection connection = null; PreparedStatement ps = null; try { InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(is); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String url = properties.getProperty("url"); String driver = properties.getProperty("driver"); Class.forName(driver); connection = DriverManager.getConnection(url, user, password); //4,預編譯sql語句,返回PreparedStatement例項 String sql = "insert into users(id,name,password,email,birthday)values(?,?,?,?,?)"; ps = connection.prepareStatement(sql); //5,填充佔位符 ps.setInt(1,5); ps.setString(2,"羅志祥"); ps.setString(3,"123147"); ps.setString(4,"[email protected]"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("1000-01-01"); ps.setDate(5,new java.sql.Date(date.getTime())); //6,執行操作 ps.execute(); } catch (Exception e) { e.printStackTrace(); } finally { //7,資源的關閉 try { if (ps != null) ps.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } try { if (connection != null) connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } }
第一步就是先讀入jdbc.properties檔案中的四個基本資訊
第二步是載入驅動
第三步是獲取連線
第四步預編譯sql語句,返回preparedStatement例項
第五步填充佔位符:上邊的程式碼只是第一遍做,熟悉了一下過程,如果在我們不知道型別是,一般會選擇setObjec()方法
第六步是執行操作了:除了execute()方法,我們一般也會寫成executeUpdate()方法,execute()返回的是boolean型別的值,這個方法指的是如果返回true,則表示有結果集,如果返回false,就表示沒有結果集,而executeUpdate()方法,則返回的是一個int型別的值,指的是被影響到的行數,比如說刪除多少行,或者修改多少行這樣的
第七步是資源的關閉。
通過反覆的學習,發現第一步第二步,第三步和第七步都是固定的套路,所以可以寫到一個工具類裡
例如
public class JDBCUtils { public static Connection getConnection() throws Exception { InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(is); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String url = properties.getProperty("url"); String driver = properties.getProperty("driver"); Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); return connection; } public static void closeResource(Connection connection, Statement ps){ try { if (ps != null) ps.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } try { if (connection != null) connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } }
這樣下次我們就可以直接呼叫已經寫好的方法,不用再重複寫固定的程式碼了
然後接下來就是寫一個通用的增刪改操作,來,直接上程式碼
public void update(String sql,Object ...args){ Connection connection = null; PreparedStatement ps = null; try { //1,獲取資料庫的連線 connection = JDBCUtils.getConnection(); //2,預編譯sql語句 ps = connection.prepareStatement(sql); //3,填充佔位符 for (int i = 0; i < args.length; i++) { ps.setObject(i+1,args[i]); } //4,執行操作 ps.execute(); } catch (Exception e) { e.printStackTrace(); } finally { //5,關閉資源 JDBCUtils.closeResource(connection,ps); } }
這次就只有這五步了:
第一步:這是學習jdbc的必須的一個步驟,只是這裡呼叫了已經寫好的方法
第二步:預編譯sql語句
第三步:填充佔位符:這一步尤為關鍵:i+1表示的是sql中的位置,args[i]:可變引數佔位符,有幾個?就寫幾個值進來
第四步:執行操作
第五步:關閉資源也是直接呼叫已經寫好的方法
最後寫一個測試的程式碼
public void testUpdate1(){ String sql = "delete from users where id = ?"; update(sql,2); String sql = "update account set name = ? where id = ?"; update(sql,"DD","2");
這裡寫了一個刪除的測試,一個更新的測試,可以對照上便程式碼進行學習
好了,這就是增刪改的全部內容,我的下一步部落格會更新查詢操作,希望大家持續關注,感謝