public class student
{
public int id{getset}
public string name{getset}
}
List<student>st=new List<student>()
foreach(var item in st)
{
Model.student st=new Model.student()
st.id=item.id
st.name=itemt.name
this.Insert(st)
}
//根据实体添加数据库
public string Insert(Model.student student)
{
.... *** 作数据库
}
可以用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 = 0i <counti++)
// 将每条记录的数据插入数组
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()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)