delete from t1 where id=?
insert into t1 values(?)都是需要传入参数的,难道创建多个PreparedStatement?
请教大家

解决方案 »

  1.   

    创建一个PreparedStatement一次只能执行一条SQL语句吧?
    要不然一会select一会delete,excuteQuery?还是excuteUpdate?
      

  2.   

    XXX为类型 parameterIndex为位置 x为传递参数
      

  3.   

    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又满足不了要求。
      

  4.   

    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{
     ...
    }
      

  5.   

    不是,可以执行多条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来处理
      

  6.   


    sql语句直接用赋值到string中就行了
    String sql = "delete from t1 where id="+id;
      

  7.   


    这样我还要用preparedStatemen干嘛?直接用Statement好了。
    不一定是id=?,或是name=?,或是id=? and name=?
    全是当参数传过去,不确定的。