Ⅰ 如何將List集合插入到資料庫中
可以用QueryRunner的batch方法
public class Test {
private static final String DRIVER_CLASS_NAME = "";
private static final String JDBC_URL = "";
private static final String USER = "";
private static final String PASSWORD = "";
public static void main(String[] args) {
int count = 10; // 插入記錄的數目
Object[][] params = new Object[count][];
for (int i = 0; i < count; i++)
// 將每條記錄的數據插入數組
params[i] = new Object[] { "", "", "" };
batch(params);
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER_CLASS_NAME);
conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
return conn;
} catch (Exception e) {
return null;
}
}
public static void batch(Object[][] params) {
QueryRunner queryRunner = new QueryRunner(true);
String sql = "INSERT INTO TABLE_NAME VALUES (?,?,?)";
try {
queryRunner.batch(getConnection(), sql, params);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Ⅱ 把list集合里的數據存到資料庫里 的一張表裡 求具體代碼!
Result rs = conn.executeQuery();
while(rs.next()){
list.add(rs.getString("columnName"));
}
-- 定義源函數
create function func()
returns @tbl table (c_au_id varchar(20), c_au_lname varchar(50))
begin
insert into @tbl select au_id, au_lname from dbo.authors
return ;
end
-- 查詢語句
select * from func()