java 向資料庫插入記錄

2021-08-01 19:51:21 字數 1234 閱讀 2705

通過statement

/**

* * @author administrator

*介面statement

* 所有已知子介面:

* callablestatement, preparedstatement (一般使用這個)

* 物件由connection.createstatement()建立

public class insert

insert() catch (exception e)

}}

通過preparedstatement

/**

* * @author administrator

*preparedstatement介面 表示預編譯的 sql 語句的物件

* 比statement的好處

* 1、相對比較安全,可以防止sql注入

* 2、有預編譯功能,相同操作批量資料效率較高

public class insert

insert() catch (exception e)

}}

使用statement插入20000條記錄,不使用preparedstatement因為預處理消耗較大

/** 

* @author administrator

*批量處理, 一次性插入大量資料

*1 關閉自動提交模式 conn.setautocommit(false);

*2 通過st.addbatch()將給定的 sql 命令新增到此 statement 物件的當前命令列表中。

* 還要 通過呼叫方法 executebatch 批量執行此列表中的命令。

*3 提交 conn.commit();

*/public class insert

insert()

st.executebatch();

conn.commit();

resultset re = st.executequery("select * from t_student");

while(re.next())

system.out.println("插入成功");

st.close();

conn.close();

} catch (exception e)

}}

資料庫中插入記錄

把一張表中的資料插入資料庫中 現在,我們將建立乙個html表單 通過它我們可以向 person 表中加入新的記錄。下面演示這個html表單 在上述案例中,當乙個使用者點選html表單中的 提交submit 按鈕後,表單中的資料會傳送到 insert.php insert.php 檔案與資料庫建立連線...

MyBatis向資料庫中批量插入資料

foreach collection 指定要遍歷的集合 表示傳入過來的引數的資料型別。該引數為必選。要做 foreach 的物件,作為入參時,list 物件預設用 list 代替作為鍵,陣列物件有 array 代替作為鍵,map 物件沒有預設的鍵。當然在作為入參時可以使用 param keyname...

用Java向資料庫中插入大量資料時的優化

使用jdbc向資料庫插入100000條記錄,分別使用statement,preparedstatement,及preparedstatement 批處理3種方式進行測試 1.使用statement插入100000條記錄 public void exec connection conn 結束時間 lo...