用java连接数据库,表中有数据,但查询不出来,请教一下

cyc123007512 2009-12-31 11:41:50
public Connection getConn() throws SQLException {
String sDBDriver = "oracle.jdbc.driver.OracleDriver";
String sConnStr = "jdbc:oracle:thin:@zhwlcool:1521:fyt";
Connection conn = null;
String user="scott";
String pass="ch111111";
try {
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr,user,pass);

} catch (ClassNotFoundException e) {
e.printStackTrace();

}
return conn;
}
public PreparedStatement getPreparedStatement(String sql)throws SQLException{
Connection conn=getConn();
PreparedStatement pst=conn.prepareStatement(sql);
return pst;
}

public static void main(String [] args) throws SQLException{
TestConn tconn = new TestConn();
try{
PreparedStatement pst=tconn.getPreparedStatement("select * from gtestsomething");
if(pst!=null){
ResultSet rs=pst.executeQuery();
if(rs.next()){//此处为什么为false
System.out.println(rs.getString(2));
}
rs.close();
}
pst.close();
}catch(Exception e){
e.printStackTrace();
}
}

用pl/sql执行“select * from gtestsomething”表gtestsomething中有两列数据,为什么rs.next()){一直为false呢
...全文
912 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyc123007512 2009-12-31
  • 打赏
  • 举报
回复
这个为true
[Quote=引用 1 楼 fatfoxz 的回复:]
把SQL改为:select  sysdate  from  dual试试,看看rs.next()是TRUE还是FALSE
[/Quote]
dawugui 2009-12-31
  • 打赏
  • 举报
回复
那直接通过toad,pl/sql能否查到数据?

如果能,就是JAVA代码问题了,不懂,帮顶.
fatfoxz 2009-12-31
  • 打赏
  • 举报
回复
把SQL改为:select sysdate from dual试试,看看rs.next()是TRUE还是FALSE
cyc123007512 2009-12-31
  • 打赏
  • 举报
回复
谢谢你,你的说法是正确的。
[Quote=引用 7 楼 shiyiwan 的回复:]
Insert之后commit回车,以为你的pl/sql session和java程序session是不同的,pl/sql对数据库做的修改如果没有提交,则无法反应到java那边。
[/Quote]
shiyiwan 2009-12-31
  • 打赏
  • 举报
回复
Insert之后commit回车,以为你的pl/sql session和java程序session是不同的,pl/sql对数据库做的修改如果没有提交,则无法反应到java那边。
cyc123007512 2009-12-31
  • 打赏
  • 举报
回复
直接在pl/sql里面运行
insert into sgtestsomething(nid,nname) values(3,'婉思')插入
[Quote=引用 5 楼 shiyiwan 的回复:]
这两条数据是什么插入进去的,有没有commit?

[/Quote]
shiyiwan 2009-12-31
  • 打赏
  • 举报
回复
这两条数据是什么插入进去的,有没有commit?
Denonly 2009-12-31
  • 打赏
  • 举报
回复
程序没有错

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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