我写了两个方法
===========能取出数据=========== 
conn.prepareStatement("select * from t where a='a' and b='b'");
===========不能取出数据=========== 
conn.prepareStatement("select * from t where a=? and b=?");
 st.setString(1, "a");
 st.setString(2, "b");
 有人遇到过类似问题吗?

解决方案 »

  1.   

    conn.prepareStatement("select * from t where a='?' and b='?'");
      

  2.   

    查询不需要加上conn.commit()吧。
    conn.prepareStatement("select * from t where a='?' and b='?'");肯定不行的。没有报错,就是查不出数据,我怀疑是不是oracle jdbc或者数据库版本问题。
      

  3.   

    这是完整的代码public static void main(String[] args) {
    try {
    Class.forName("oracle.jdbc.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.2.3.26:1521:test", "test", "test");
    PreparedStatement st = null;
    int i = 0;

    st = conn.prepareStatement("select * from D90qx where f90yhbh=?");
    st.setString(1, "f90yhbh");
    i = getCount(st);

    st = conn.prepareStatement("select * from D90qx");
    i = getCount(st);

    st = conn.prepareStatement("select * from D90qx where f90yhbh='f90yhbh' and f90zcbh='f90zcbh3'");
    i = getCount(st);

    st = conn.prepareStatement("select * from D90qx where f90yhbh=? and f90zcbh=?");
    st.setString(1, "f90yhbh");
    st.setString(2, "f90zcbh3");
    //st.
    i = getCount(st);

    conn.close();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    private static int getCount(PreparedStatement st)
    {
    int i = 0;
    try {
    ResultSet rs = st.executeQuery();
    while (rs.next())
    {
    String str1 = rs.getString(1);
    String str2 = rs.getString(2);
    i++;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return i;
    }
      

  4.   

    select * from t where a=? and b=?  
     可以这样子吗?   select * from t where a=? and b=?   and c=?  and d=?  或者是select * from t where a=? && b=?  &&c=?  a&& d=?  
      

  5.   

    看看数据库里字段的类型,文档里这么说
    Sets the designated parameter to the given Java String value. The driver converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's size relative to the driver's limits on VARCHAR values) when it sends it to the database. 有可能是这个原因