public int kouanID(String kouan)
    {
        
        try
        {
            int kouanid;
            PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng= '"+kouan+"'");
                                            
            ResultSet rs = ps.executeQuery();
            rs.next();
            kouanid = rs.getInt(1);
            
            return kouanid;
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
        
        
    }
    
    
    
    在这里调用了上面这个函数
        String kouan;
                kouan = request.getParameter("kouan");
                int kouanid;
                kouanid = this.kouanID(kouan);
                
                
为什么得不到kouanid 它总是返回一个0,kouan能得到,传到上面返回来的却是个0

解决方案 »

  1.   

    把 int kouanid; 移到 try 的上面,将 catch 前的 return 移到 return 0; 的地方,把 return 0; 删掉。
      

  2.   

    你这样写的结构不太好,PreparedStatement、ResultSet 都没关掉。
      

  3.   

    你最后还返回0做什么,你开始定义一个变量int kouanid=0,最后返回那个变量就是了
      

  4.   

    有可能数据库没有记录
     kouanid = rs.getInt(1);  kouanid 是内部变量 默认为0
      

  5.   

    晕,你DEBUG一下,看哪个地方出错就是了
      

  6.   

    单独执行下你的SQL语句
    应该是空值 单词对吗?
      

  7.   

    kouan = request.getParameter("kouan");这句能得到正确的值
    ("select kouanid from kouanbiao where kouanmingcheng= '"+kouan+"'");这句kouan换成对应的值 查询分析器里也能查的出来
    可就是一起执行就是不返回正确的值
      

  8.   

    DEBUG设置断点看了...真是的..
    也不是什么难题...
      

  9.   

    你把这个 PreparedStatement 换成Statement试试
      

  10.   

    你有2个return ,看走到哪里不就是了么??
      

  11.   

    public int kouanID(String kouan)
    {
    int kouanid=0;
    try
    {



    PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng = '"+kouan+"'");

    ResultSet rs = ps.executeQuery();
    while(rs.next())
    {
    kouanid = rs.getInt("kouanid");
    }




    }catch(Exception e)
    {
    e.printStackTrace();
    }
    return kouanid;


    }
    kouan = request.getParameter("kouan");
             kouanid = this.kouanID(kouan);
             pw.print(kouanid);
    已经改成这样了还是返回一个0啊,到底是什么问题啊
      

  12.   

    是不是没有报错,但pw.print(kouanid);
    却是0????
      

  13.   

    你在kouanid = rs.getInt("kouanid");后面把这个kouanid打印出来看看呀,你看看到底到哪一部就变零了啊,你又不说清楚一点
      

  14.   

    kouanid 的类型是什么样的?
      

  15.   

    kouanid = rs.getInt("kouanid");这段是在 JAVA方法里的
    怎么打印出来的?我忘了
      

  16.   

    public int kouanID(String kouan)
    {
    int kouanid=0;
    try
    {


    System.out.println("kouan :"+kouan);
    PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng = '"+kouan+"'");
    System.out.println("sql :"+sql);
    ResultSet rs = ps.executeQuery();
    while(rs.next())
    {
    kouanid = rs.getInt("kouanid");
                                         System.out.println("rs.getInt--kouanid :"+rs.getInt("kouanid"));
                                         System.out.println("kouanid :"+kouanid);
    }




    }catch(Exception e)
    {
    e.printStackTrace();
    }
    return kouanid;


    }
    kouan = request.getParameter("kouan");
             kouanid = this.kouanID(kouan);
             pw.print(kouanid);把这段代码执行换成你原来的代码,再看看在控制台里打印出来的值是否正确。
      

  17.   

    System.out.println(rs.getInt("kouanid"));
      

  18.   

    那也就是说while(rs.next())没有执行
    看看你的数据库的连接程序吧
      

  19.   

    也没问题,因为我在运行其它模块时是好使的
    而且 PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng = '"+kouan+"'");
    这个改成 PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng = '会宁'");时是返回正确的
    但是我又觉得 SQL语句没错
      

  20.   

    我提交JSP页面时,调用的这个方法,而调用完这个方法之后是直接到一个空白页面了
    所以我一直都是用PW。PRINT打印到空白页面的
    用SYSTEM。OUT。PRINT怎么打到空白页面啊
      

  21.   

    public int kouanID(String kouan)
        {
            int kouanid=0;
            try
            {
                
                PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng= '"+kouan+"'");
                                                
                ResultSet rs = ps.executeQuery();
                if (rs.next()){    
                    kouanid = rs.getInt(1);    
                }
                
            }catch(Exception e)
            {
                e.printStackTrace();
            }finally{
              rs.colse(); 
              ps.cose(); 
            }
           return kouanid; 
        }
      

  22.   

    SYSTEM。OUT。PRINT 这个是控制台打印,你也可以到控制台看结果的
      

  23.   

    kouan = request.getParameter("kouan");
             System.out.println(kouan ); //1、判断是否得到传递的值?
             kouanid = this.kouanID(kouan);
             pw.print(kouanid);   // 2、kouanid =0 DB中没有相应的纪录。
      

  24.   

    return 0放在那里不对。不然怎么调这方法。返回的都是0
      

  25.   

    kouanid = rs.getInt("kouanid");
      这是大错特错的低级错误。
      

  26.   

    你的数据库采用的是什么字符集在ResultSet rs = ps.executeQuery();后加
    System.out.println("记录数为: "+rs.getRow())看看控制台有没有显示  记录数为: 0
      

  27.   

    String kouan;
                    kouan = request.getParameter("kouan");
                    int kouanid;
                    kouanid = this.kouanID(kouan);
    试下将String kouan;改成 String kouan="";
      

  28.   

    kouan = request.getParameter("kouan");如果你的 kouan 是从地址栏传入的数据,你需要将 Tomcat 目录下的 conf/server.xml 中找到 Connector 元素,应该可以找到两个,在这两个元素中加上“URIEncoding”属性,值设为“GBK”,你再试试看。
      

  29.   

    public int kouanID(String kouan)
        {
            
            try
            {
                int kouanid;
                PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng= '"+kouan+"'");
                                                
                ResultSet rs = ps.executeQuery();
                rs.next();
                kouanid = rs.getInt(1);
                
              
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            
              return kouanid;
            
        }
    试试
      

  30.   

    return 不能放在try{ }里面的,即使在try里return kouanid;了,仍然会执行return 0操作
    所以return不是在所有的地方都好用的
      

  31.   

    你不要return 0;
    你把kouanid附个初始值0;
    直接return kouanid;
    我也是新手只是说了一下自己的意见
    哈哈
    我也学习下
      

  32.   

    可能是字符编码的问题,你在数据库中将kouanmingcheng字段的值中文的改一个成英文试一下,如果行了的话,就是中文字符编码的问题,中文问题有很多解决方案,可以上网查一下,好像csdn blog里有一文章讲得很详细。
      

  33.   

    要查找字符串是不是相等不要用“=”,要用equals()方法。
      

  34.   

    谢谢 xieboshi 还有各位好心人,解决了返回0的问题 但是必须得把数据库里的kouan字段里的值改成英文啊~
      

  35.   

    PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng= ?");
    ps.setString(1, kouan);
      

  36.   

    回复人:bao110908(bao)(bao)(讨厌蟑螂) ( 四级(中级)) 信誉:100  2007-7-12 17:45:53  得分:0kouan = request.getParameter("kouan");如果你的 kouan 是从地址栏传入的数据,你需要将 Tomcat 目录下的 conf/server.xml 中找到 Connector 元素,应该可以找到两个,在这两个元素中加上“URIEncoding”属性,值设为“GBK”,你再试试看。URIEncoding 应与你 JSP 页面中的字符集匹配。停掉 Tomcat 再改,改好后重启,应该可以解决中文问题。============================我贴的,你竟然没看!555555555~~~~