Ⅰ 如何将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()