我写了两个方法
===========能取出数据===========
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");
有人遇到过类似问题吗?
===========能取出数据===========
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");
有人遇到过类似问题吗?
conn.prepareStatement("select * from t where a='?' and b='?'");肯定不行的。没有报错,就是查不出数据,我怀疑是不是oracle jdbc或者数据库版本问题。
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;
}
可以这样子吗? select * from t where a=? and b=? and c=? and d=? 或者是select * from t where a=? && b=? &&c=? a&& d=?
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. 有可能是这个原因