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
{
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
解决方案 »
- prefuse applet加到jsp上 InvocationTargetException
- 关于prepareStatement问题
- <s:i18n name="ApplicationResources"> 是什么意思
- java Mail中邮件服务器是指要发送的邮件服务器吗这么知道是多少啊
- Java的函数编:程利用闭包和高阶函数编写模块化的 Java 代码
- 关于JDBC连接MS SQL Server的问题
- 数据库对象关闭的问题
- 求教为什么Orcale数据库Long字段过长程序会报错?
- 压力测试tomcat假死,2分钟后自动恢复
- 为什么我感到这么慢?
- 100分求LDAP存储策略,附带相关代码
- 在struts练习中,调试时页面数据正确,运行时页面数据不更新,怎么回事?
kouanid = rs.getInt(1); kouanid 是内部变量 默认为0
应该是空值 单词对吗?
("select kouanid from kouanbiao where kouanmingcheng= '"+kouan+"'");这句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啊,到底是什么问题啊
却是0????
怎么打印出来的?我忘了
{
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);把这段代码执行换成你原来的代码,再看看在控制台里打印出来的值是否正确。
看看你的数据库的连接程序吧
而且 PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng = '"+kouan+"'");
这个改成 PreparedStatement ps = DB.getPs("select kouanid from kouanbiao where kouanmingcheng = '会宁'");时是返回正确的
但是我又觉得 SQL语句没错
所以我一直都是用PW。PRINT打印到空白页面的
用SYSTEM。OUT。PRINT怎么打到空白页面啊
{
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;
}
System.out.println(kouan ); //1、判断是否得到传递的值?
kouanid = this.kouanID(kouan);
pw.print(kouanid); // 2、kouanid =0 DB中没有相应的纪录。
这是大错特错的低级错误。
System.out.println("记录数为: "+rs.getRow())看看控制台有没有显示 记录数为: 0
kouan = request.getParameter("kouan");
int kouanid;
kouanid = this.kouanID(kouan);
试下将String kouan;改成 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不是在所有的地方都好用的
你把kouanid附个初始值0;
直接return kouanid;
我也是新手只是说了一下自己的意见
哈哈
我也学习下
ps.setString(1, kouan);