首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

有人可以帮小弟我看段代码吗?郁闷中,

2012-01-11 
有人可以帮我看段代码吗?郁闷中,在线等~~~publicintkouanID(Stringkouan){try{intkouanidPreparedStateme

有人可以帮我看段代码吗?郁闷中,在线等~~~
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

[解决办法]
异常有没有,数据库中有没有对应的记录
[解决办法]
把 int kouanid; 移到 try 的上面,将 catch 前的 return 移到 return 0; 的地方,把 return 0; 删掉。
[解决办法]
你最后还返回0做什么,你开始定义一个变量int kouanid=0,最后返回那个变量就是了
------解决方案--------------------


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


[解决办法]
DEBUG设置断点看了...真是的..
也不是什么难题...
[解决办法]
打断
[解决办法]
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);


把这段代码执行换成你原来的代码,再看看在控制台里打印出来的值是否正确。
[解决办法]
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;
}
[解决办法]
return 0放在那里不对。不然怎么调这方法。返回的都是0

[解决办法]
hehe ,跟踪调试一下,就ok.
[解决办法]
String kouan;
kouan = request.getParameter( "kouan ");
int kouanid;
kouanid = this.kouanID(kouan);
试下将String kouan;改成 String kouan= " ";
[解决办法]
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;

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

[解决办法]
你不要return 0;
你把kouanid附个初始值0;
直接return kouanid;
我也是新手只是说了一下自己的意见
哈哈
我也学习下
[解决办法]
可能是字符编码的问题,你在数据库中将kouanmingcheng字段的值中文的改一个成英文试一下,如果行了的话,就是中文字符编码的问题,中文问题有很多解决方案,可以上网查一下,好像csdn blog里有一文章讲得很详细。
[解决办法]
要查找字符串是不是相等不要用“=”,要用equals()方法。
[解决办法]
PreparedStatement ps = DB.getPs( "select kouanid from kouanbiao where kouanmingcheng= ? ");
ps.setString(1, kouan);

热点排行