ArrayList循环插入表!求解一套数据插入主从表
SYS_ENGINEINFO是主表
SYS_ENGINEINFOCONDITION是从表
SYS_ENGINEINFO表中简单来说是三个字段
ENGINEINFOID 引擎info标识主键
ENGINEINFONAME 引擎info标识名称
ENGINEINFOTYPE 引擎info标识类型
SYS_ENGINEINFOCONDITION表中简单来说是三个字段
ENGINECONDITIONID 引擎info条件标识主键
ENGINEINFOID 引擎info标识主键
ENGINECONDITION 条件
我要新增一条信息记录的同时插入两张表
public void saveEngineInfo(EngineInfoForm form){
DatabaseAccess dba=new DatabaseAccess();
String insertSql1=" INSERT INTO SYS_ENGINEINFO " +
"(ENGINEINFOID , ENGINEINFONAME , ENGINEINFOTYPE , DESCRIBE)"+
" VALUES " +
" (SEQ_SERVICE.NEXTVAL, ?, ?, ?) " ;
ArrayList<String> paramList=new ArrayList<String>();
paramList.add(form.getEngineinfoName());
paramList.add(form.getEngineinfoType());
paramList.add(form.getDescribe());
int i=dba.execute(insertSql1,paramList);
String insertSql2=" INSERT INTO SYS_ENGINEINFOCONDITION " +
"(ENGINECONDITIONID , ENGINEINFOID , ENGINECONDITION , REMARK)" +
" VALUES " +
"(SEQ_SERVICE.NEXTVAL, ?, ?, ?)";
ArrayList<String> paramList2=new ArrayList<String>();
paramList2.add(String.valueOf(i));
paramList2.add(form.getEngineCondition());
paramList2.add(form.getRemark());
dba.execute(insertSql2,paramList2);
}
后来发现主从表一对多关系!!我增加从表需要循环增加!暨增加一条SYS_ENGINEINFO记录同时要增加多条SYS_ENGINEINFOCONDITION
代码怎么改?
[解决办法]
如果数据量少 你可以直接用循环 执行sql
for(){
String insertSql1=" INSERT INTO SYS_ENGINEINFO " +
"(ENGINEINFOID , ENGINEINFONAME , ENGINEINFOTYPE , DESCRIBE)"+
" VALUES " +
" (SEQ_SERVICE.NEXTVAL, ?, ?, ?) " ;
ArrayList<String> paramList=new ArrayList<String>();
paramList.add(form.getEngineinfoName());
paramList.add(form.getEngineinfoType());
paramList.add(form.getDescribe());
int i=dba.execute(insertSql1,paramList);
}
大概就是这个思路