pstmt = conn.prepareStatement(queryBuilder.toString());
pstmt.setString(count++, commonDTO.getGroupName());
pstmt.setString(count++, commonDTO.getSubGrpNameHindi());
pstmt.setString(count++, commonDTO.getGroupCode());
pstmt.setInt(count++, commonDTO.getIsActive());
pstmt.executeUpdate();
StringBuilder queryBuilder = new StringBuilder();
queryBuilder = queryBuilder.append(SLCMQueryConstant.Get_SubGrp_Id);
pstmt.setString (1, commonDTO.getGroupName());
rs = pstmt.executeQuery();
int x = 0;
while(rs.next())
{
x= rs.getInt("subject_Group_ID");
}
queryBuilder= queryBuilder.append(SLCMQueryConstant.MapSubject_to_SubGrp);
pstmt.setInt(count++, x);
pstmt.setInt(count++, commonDTO.getSubGrpID());
pstmt.setInt(count++, commonDTO.getSubjectMaxMark());
pstmt.setInt(count++, commonDTO.getSubjectMinMark());
pstmt.executeUpdate();
conn.commit();
我正在尝试在第一个表中插入一个新行。然后我想要新添加的id 行和插入第二个表但有一个例外。
答案 0 :(得分:1)
错误消息告诉您确切的错误 - executeQuery用于执行SELECT查询,而不是INSERTS / UPDATES。请改用executeUpdate
,例如描述here。
看到您已添加上述查询 - 要执行多个查询,请运行多个executeQuery / executeUpdate语句。只要您没有自动提交设置并提交第三个查询,所有三个查询都将作为单个提交执行(即不能被中断)。
不可能 使用单个executeQuery / executeUpdate运行多个查询,以分号分隔。这种语法很简单,许多客户端使用它来以文本方式分隔查询,但客户端也会分别发送这些查询以及多个请求。
我猜在单个交易中运行三个单独的查询将完全符合您的需求。但是,如果由于某种原因你绝对想用一次执行来做,你可以使用stored procedure,你提前做好准备。
答案 1 :(得分:0)
您必须从构建的查询中创建预准备语句:
pstmt = conn.prepareStatement(queryBuilder.toString());
pstmt.setString(count++, commonDTO.getGroupName());
pstmt.setString(count++, commonDTO.getSubGrpNameHindi());
pstmt.setString(count++, commonDTO.getGroupCode());
pstmt.setInt(count++, commonDTO.getIsActive());
pstmt.executeUpdate();
StringBuilder queryBuilder = new StringBuilder();
queryBuilder = queryBuilder.append(SLCMQueryConstant.Get_SubGrp_Id);
pstmt = conn.prepareStatement(queryBuilder.toString()); // <<<<<<-----
pstmt.setString(1, commonDTO.getGroupName());
Recordset rs = pstmt.executeQuery();
int x = 0;
while(rs.next())
{
x = rs.getInt("subject_Group_ID");
}
您必须重置预准备语句参数计数器:
queryBuilder= queryBuilder.append(SLCMQueryConstant.MapSubject_to_SubGrp);
pstmt = conn.prepareStatement(queryBuilder.toString()); // <<<<<<-----
count = 1; //<<<<<-----
pstmt.setInt(count++, x);
pstmt.setInt(count++, commonDTO.getSubGrpID());
pstmt.setInt(count++, commonDTO.getSubjectMaxMark());
pstmt.setInt(count++, commonDTO.getSubjectMinMark());
pstmt.executeUpdate();
conn.commit();