不知道我是否理解正确你的意思PreparedStatement pstmt=con.preparedStatement("select * from a where c2 = ? and c3 = ?");
pstmt.setFloat(1,p1);
pstmt.setDate(2,p2);

解决方案 »

  1.   

    例如:其中conn是连接对象, unitsintstock和productid都是整型数据字段.
    ?代表参数PreparedStatement pst;
    pst = new conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");pst.setInt(1,1000); //设置第1个参数
    pst.setInt(2,101);  //设置第2个参数
    pst.executeUpdate();
      

  2.   

    请问 xtaotao 兄弟,
    new conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");conn是一个对象而不是一个类,new conn.prepareStatement()这个语法我有点胡涂,请问是你写错了还是我确实没见到过这种语法。谢谢你们的回答!!!!
      

  3.   

    Connection  conn;   //连接
    statement stmt;     
    Resultset data;sql = "select * from a where c2 = " + f1 + " and substring(c3,0,10) = " + d1;
    data = stmt.executeQuery(sql);
      

  4.   

    对不起,我写错了,应该是:
    conn连接对象的prepareStatement方法返回一个PreparedStatement的对象.pst = conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");
      

  5.   

    对不起,又错! 应该是:
    conn连接对象的prepareStatement方法返回一个PreparedStatement的对象.pst = conn.prepareStatement("update Products set UnitsInStock=UnitsInStock=? where ProductID=?");
      

  6.   

    keen1982,你的意思是让f1,d1都自动转行位String吗?
      

  7.   

    以上解都正确但:
    1,sql = "select * from a where c2 = " + f1 + " and substring(c3,0,10) = " + d1;
       此种问题下,如果f1,d1是字符串,且包含单引号等,会出错的;
    2,sql语句中定义变量,不推荐使用"?"。
       若,sql语句中定义的变量太多,会影响程序可读性。
      

  8.   

    keen1982的方法也是比较常用的方法,其实是没有用参数的,是自己拼接成一个SQL语句,我也经常用.使用参数的方法如同我和Ispy(晓剑) 说的一样,其实就是用?代价DELPHI中的:VAR, 然后再向参数赋值.