PreparedStatement如何执行不同sql语句

blackthought 2010-09-15 02:50:10
delete from t1 where id=?
insert into t1 values(?)

都是需要传入参数的,难道创建多个PreparedStatement?
请教大家
...全文
995 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
24K純帥 2010-09-17
  • 打赏
  • 举报
回复
transaction
blackthought 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wypbt1032 的回复:]

引用 5 楼 blackthought 的回复:
sql="delete from t1 where id=?"
sql2="insert into t1 values(?)"
...

我要的是批处理执行sql,sql2...
ps=con.preparedStatement(sql)
ps.set...
...

ps2=con.preparedStatement(sql……
[/Quote]

这样我还要用preparedStatemen干嘛?直接用Statement好了。
不一定是id=?,或是name=?,或是id=? and name=?
全是当参数传过去,不确定的。
wypbt1032 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 blackthought 的回复:]
sql="delete from t1 where id=?"
sql2="insert into t1 values(?)"
...

我要的是批处理执行sql,sql2...
ps=con.preparedStatement(sql)
ps.set...
...

ps2=con.preparedStatement(sql2)
ps2.set...
...

然后一起……
[/Quote]

sql语句直接用赋值到string中就行了
String sql = "delete from t1 where id="+id;
wcnqrm 2010-09-16
  • 打赏
  • 举报
回复
addBatch()
thegodofwar 2010-09-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 flagiris 的回复:]
创建一个PreparedStatement一次只能执行一条SQL语句吧?
要不然一会select一会delete,excuteQuery?还是excuteUpdate?
[/Quote]
不是,可以执行多条sql语句e.g:


批量删除数据
PreparedStatement pstmt = null;
String sql = "update table set column1=XXX where column=?";
try {
pstmt = conn.prepareStatement(sql);
int count = 0;
for (int i = 0; i < values.length; i++) {
pstmt.setString(1, var1);
pstmt.addBatch();
count++;
if (count % 1000 == 0) {
pstmt.executeBatch();
pstmt.clearBatch();
count = 0;
}
}
if (count > 0) {
pstmt.executeBatch();
pstmt.clearBatch();
}
pstmt.close();
} catch (Exception e) {
throw e;
} finally {
if (pstmt != null) {
pstmt.close();
}
if (conn != null)
conn.close();
}


但是你说的这种情况只能是采用多个PreparedStatement来处理
wypbt1032 2010-09-15
  • 打赏
  • 举报
回复
try{
Connection con = .....;
con.setAutoCommit(false);

Statement statement = con.creatStatement();
sql="delete from t1 where id=?"
sql2="insert into t1 values(?)"
...
statement.addBatch(sql);
statement.addBatch(sql2);
...
statement.executeBatch();
con.commit();
}catch(Exception e){
con.rollback();
e.printStackTrace();
}finally{
...
}
blackthought 2010-09-15
  • 打赏
  • 举报
回复
sql="delete from t1 where id=?"
sql2="insert into t1 values(?)"
...

我要的是批处理执行sql,sql2...
ps=con.preparedStatement(sql)
ps.set...
...

ps2=con.preparedStatement(sql2)
ps2.set...
...

然后一起提交?这样的话语句100个sql语句,不要创建100个preparedStatement? 不是好方法。
请问应该怎样做?Statement又满足不了要求。
sound9world 2010-09-15
  • 打赏
  • 举报
回复
恩 创建多个
madFatso 2010-09-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 madfatso 的回复:]
ps.setXXX(parameterIndex, x)
[/Quote]
XXX为类型 parameterIndex为位置 x为传递参数



菖蒲老先生 2010-09-15
  • 打赏
  • 举报
回复
创建一个PreparedStatement一次只能执行一条SQL语句吧?
要不然一会select一会delete,excuteQuery?还是excuteUpdate?
madFatso 2010-09-15
  • 打赏
  • 举报
回复
ps.setXXX(parameterIndex, x)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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