关于数据库插入多条Mysql

Candylibin 2012-07-05 10:26:07
小弟想在数据库插入多条数据,不能写死,要动态的,因为我也不知道插入多少条,求格式
...全文
200 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
beiouwolf 2012-07-05
  • 打赏
  • 举报
回复
在连接语句中加入 allowMultiQueries=true
然后可以把多条SQL拼起来一起传递给mysql,每条语句用 ; 分割
Candylibin 2012-07-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你的数据是怎么放过来的啊 ?
[/Quote]

public class AddShopOperation extends Operation{
public Result execute() throws OpertionException {

Object orderid = this.getParameter("orderid");
Object shopname = this.getParameter("shopname");
Object totalprice = this.getParameter("totalprice");
Object deliverprice = this.getParameter("deliverprice");
Object finalprice = this.getParameter("finalprice");

String sql = "insert into shop(orderid,shopname,totalprice,deliverprice,finalprice,addtime) values(?,?,?,?,?,now())";

Object [] a = {orderid,shopname,totalprice,deliverprice,finalprice};
int c = this.getDataBaseProvider().executeUpdate(sql, a);

if(c>0){
return new AddShopSuccessful();
}else{
return new AddShopFail();
}
}
}
这个是数据操作。

这个是测试

public class AddShopTest extends BreadthTestCase{
public void testAddShopSuccessful(){

Result rs = null;

OperationWorker worker = new OperationWorker(AddShopOperation.class);
worker.addParameter("orderid", "21256");
worker.addParameter("shopname", "nihao");
worker.addParameter("totalprice", "1000");
worker.addParameter("deliverprice", "100");
worker.addParameter("finalprice", "80000");
worker.addParameter("isdel", "0");

try{
rs = worker.execute();
}catch(OpertionException e){
e.printStackTrace();
}
assertTrue(rs instanceof AddShopSuccessful);
}
}
五哥 2012-07-05
  • 打赏
  • 举报
回复
你的数据是怎么放过来的啊 ?
Candylibin 2012-07-05
  • 打赏
  • 举报
回复
已自己解决谢谢大家
aaaa8215 2012-07-05
  • 打赏
  • 举报
回复
 
List<OperationWorker> workers ;
for(int i = 0 ; i < workers.size();i++){
.....
PreparedStatement#addBatch();
}
PreparedStatement#excuteBatch();

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧