请假oracle jdbc中使用PreparedStatement setString的问题?

baoma2000 2007-08-29 06:15:58
我写了两个方法
===========能取出数据===========
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");
有人遇到过类似问题吗?
...全文
466 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
herobjfu 2007-08-31
  • 打赏
  • 举报
回复
我的问题解决了,是数据库里空格,建议你看看你的数据里的字段类型
herobjfu 2007-08-31
  • 打赏
  • 举报
回复
看看数据库里字段的类型,文档里这么说
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. 有可能是这个原因
herobjfu 2007-08-31
  • 打赏
  • 举报
回复
我也遇到了,但是不知道怎么解决
baoma2000 2007-08-30
  • 打赏
  • 举报
回复
我试了,也查不出来。
huang3407 2007-08-30
  • 打赏
  • 举报
回复
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=?
baoma2000 2007-08-30
  • 打赏
  • 举报
回复
这是完整的代码

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;
}
baoma2000 2007-08-30
  • 打赏
  • 举报
回复
查询不需要加上conn.commit()吧。
conn.prepareStatement("select * from t where a='?' and b='?'");肯定不行的。

没有报错,就是查不出数据,我怀疑是不是oracle jdbc或者数据库版本问题。
ian63 2007-08-29
  • 打赏
  • 举报
回复
错误代码~
shengli_liao 2007-08-29
  • 打赏
  • 举报
回复
什么错误,你打出来我们看看
awusoft 2007-08-29
  • 打赏
  • 举报
回复
conn.prepareStatement("select * from t where a=? and b=?");
st.setString(1, "a");
st.setString(2, "b");

ResultSet rs = st.executeQuery();这样也不行,偶想你是见鬼了啦


顶起来
huoyin 2007-08-29
  • 打赏
  • 举报
回复
conn.prepareStatement("select * from t where a='?' and b='?'");
zdjray 2007-08-29
  • 打赏
  • 举报
回复
加上conn.commit()
试试?

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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