JDBC问题

BillLamian 2014-06-17 09:33:37
为什么执行之后查到的数据行数是0,

Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");
//获取发送对象
ment=conn.createStatement();
//String sql="select * from t_user where "+"u_name='"+name+"' and u_pwd='" + pwd+"'";
String sql="select * from t_user";
re=ment.executeQuery(sql);
int count=0;
//获取数据行数
while(re.next()){

count++;
}
System.out.println(count);


用工具可以查到数据
U_NO U_NAME U_PWD U_LEVEL
---------- -------------------------------------------------- -------------------------------------------------- ----------
1001 张三 123 0
1002 admin 123 1
1003 guest 123 0
...全文
349 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lliiqiang 2014-06-23
  • 打赏
  • 举报
回复
数据库中没有数据.
悍M 2014-06-20
  • 打赏
  • 举报
回复
楼上正解,代码看着好像真没错
铁匠梁老师 2014-06-20
  • 打赏
  • 举报
回复
直接 count 函数,效率还高点 代码逻辑没问题,问题方向是不是多实例,连错实例了
朗晴 2014-06-20
  • 打赏
  • 举报
回复
代码貌似没问题的~
Mr-稻帅 2014-06-20
  • 打赏
  • 举报
回复
代码木有问题呀。。。
KeZhuang_ 2014-06-20
  • 打赏
  • 举报
回复
你确定数据库的连接没有问题?如果没有问题你在检查一下连的是哪个数据库?如果数据库连对了那在检查下表。。
mengz777 2014-06-20
  • 打赏
  • 举报
回复
连错数据库了吧,不是登录名
LinY_X 2014-06-18
  • 打赏
  • 举报
回复
代码没问题 估计是业务逻辑问题 看下数据库对不对 DEBUG看下循环进去没
懿凡 2014-06-18
  • 打赏
  • 举报
回复
难道你连错了数据库?.
S117 2014-06-18
  • 打赏
  • 举报
回复
在循环前设个断点,debug 跟踪
JPF1024 2014-06-18
  • 打赏
  • 举报
回复
放到循环里面去试试.


    Class.forName("oracle.jdbc.driver.OracleDriver");
            conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");
            //获取发送对象
            ment=conn.createStatement();
            //String sql="select * from t_user where "+"u_name='"+name+"' and u_pwd='" + pwd+"'";
            String sql="select * from t_user";
            re=ment.executeQuery(sql);
            int count=0;
            //获取数据行数
            while(re.next()){
                 
                    count++;
 System.out.println(count);
            }
看一下变化.
Gaken 2014-06-18
  • 打赏
  • 举报
回复
 Class.forName("oracle.jdbc.driver.OracleDriver");
            conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");
            //获取发送对象
            ment=conn.createStatement();
            //String sql="select * from t_user where "+"u_name='"+name+"' and u_pwd='" + pwd+"'";
            String sql="select * from t_user";
            PreparedStatement ps = conn.prepareStatement(sql);
            re=ps.executeQuery();
            int count=0;
            //获取数据行数
            while(re.next()){
                 
                    count++;
            }
            System.out.println(count);
aubergines 2014-06-18
  • 打赏
  • 举报
回复
代码完全没有问题,如果后台没有报错的话,唯一怀疑的就是你使用的表的问题了。
姜小白- 2014-06-18
  • 打赏
  • 举报
回复
代码没有问题,检查你的数据库名称,检查你的表名。
射鲸英雄传 2014-06-18
  • 打赏
  • 举报
回复
你连错数据库了。
-江沐风- 2014-06-17
  • 打赏
  • 举报
回复
count的结果不是0吧; 单看你程序代码,应该没问题;
姜小白- 2014-06-17
  • 打赏
  • 举报
回复
debug 看re是三行数据还是 ?

62,614

社区成员

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

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