JiveJdon Community Forums
在线126人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » 综合杂类区(软件相关)
???en_US.forumThreadPrev.name??? 上一主题
google yahoo 365Key网摘 CSDN网摘 添加到百度搜藏 POCO网摘 新浪ViVi 天极网摘
???en_US.forumThreadNext.name??? 下一主题
1 2 Go 共有 16 回复 / 2
 发表新帖子   回复该主题贴
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
请看看我的代码 2002年09月08日 15:49 到本帖网址 加入本帖到收藏夹 回复该主题
标签
public Iterator getAllRow(String ip){
Connection con = null;

Statement st = null;
String sql = null;

try {
con = MysqlConn.getConnection(ip);
sql = "select stateid,name from State";
st = con.createStatement();

System.out.println("sql="+sql);
final ResultSet rs = st.executeQuery(sql);

return new Iterator() {
private State state;//自定义类
public boolean hasNext() {
if (state == null) {
if (! rs.next()) {//出错地方1,line54
return false;
}
state.StateId = rs.getShort("stateid");//出错地方2,line57
state.StateName = rs.getString("name");//出错地方3,line58
}
return true;
}

public Object next() {
if (! hasNext()) {
throw new NoSuchElementException();
}
State retval = state;
state = null;
return retval;
}

public void remove() {
throw new UnsupportedOperationException("no remove allowed");
}
};
}catch(Exception e){
e.printStackTrace();
}
finally {
try { st.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { con.close(); } catch(SQLException ne) { ne.printStackTrace(); }
}
}

编译不通过,错误提示
1、unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 54, column 24
2、unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 57, column 34
3、unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 58, column 36

banq

悄悄话
发表文章: 9613
注册时间: 2002年08月03日 17:08
Re: 请看看我的代码 2002年09月08日 17:52 到本帖网址 加入本帖到收藏夹 回复该主题
需要

try{

}catah{
}
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月09日 08:50 到本帖网址 加入本帖到收藏夹 回复该主题
是的,问题是在哪里扑抓异常,rs.next呢
banq

悄悄话
发表文章: 9613
注册时间: 2002年08月03日 17:08
Re: 请看看我的代码 2002年09月09日 09:28 到本帖网址 加入本帖到收藏夹 回复该主题
将涉及数据库访问的全部纳入 这是java的通用写法,try语句你要常用
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月09日 16:10 到本帖网址 加入本帖到收藏夹 回复该主题
谢谢,异常的问题我解决了,是我一时没想清楚。现在有新问题了。我用了你介绍的那个方法,iterator,不知道是否是我的程序的问题,得到的iterator是空的。下面是搜索的一个方法
public Iterator getSearchbyKind(short kindid,String ip){
Connection con = null;
PreparedStatement pst = null;
String sql = null;
try {
con = MysqlConn.getConnection(ip);//这里是正确的
sql = "select l.lawid,l.lawname,l.lawcontent,l.subkindid,l.kindid,k.kindname,l.bbtime,l.sstime,l.sxtime,l.unit,l.note from Law l,Kind k where l.kindid=k.kindname and l.kindid=?";//我会给一个常量2,应该有2个记录符合要求
pst = con.prepareStatement(sql);
pst.setShort(1,kindid);
System.out.println("sql=select l.lawid,l.lawname,l.lawcontent,l.subkindid,l.kindid,k.kindname,l.bbtime,l.sstime,l.sxtime,l.unit,l.note from Law l,Kind k where l.kindid=k.kindname and l.kindid="+kindid);

final ResultSet rs = pst.executeQuery();
searchrowcount=0;
return new Iterator() {
private Law law;
public boolean hasNext() {

if (law == null) {
try {
if (! rs.next()) {
return false;
}
searchrowcount++;//计算一下有多少个元素
law.LawId = rs.getShort("lawid");
law.LawName =rs.getString("lawname");
law.LawContent = rs.getString("lawcontent");
law.SubKindId =rs.getShort("subkindid");
law.KindId=rs.getShort("kindid");
law.KindName = rs.getString("kindname");
law.BbTime=rs.getDate("bbtime");
law.SsTime =rs.getDate("sstime");
law.Sxtime =rs.getDate("sxtime");
law.Unit = rs.getString("unit");
law.Note =rs.getString("note");
}catch(Exception e){e.printStackTrace();return false;}
}
return true;
}

public Object next() {
if (! hasNext()) {
throw new NoSuchElementException();
}
Law retval = law;
law = null;
return retval;
}

public void remove() {
throw new UnsupportedOperationException("no remove allowed");
}
};
}catch(Exception e){
e.printStackTrace();
return null;
}
finally {
try { pst.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { con.close(); } catch(SQLException ne) { ne.printStackTrace(); }
}
}



我在另一个java中调用上面的方法,这是调用上面方法的函数
public void search(){
LawConnBean LawConn = new LawConnBean();//这个构造函数可以调用上面的方法

Iterator i_law =LawConn.getSearchbyKind((short)2,ip);
try{
System.out.println(i_law.hasNext());//得到false
while (i_law.hasNext()){
System.out.println("Law="+((Law)i_law.next()).LawName);
}
System.out.println("count="+LawConn.getSearchrowcount());//得到count=0
}catch(Exception e){e.printStackTrace();}
}
我在getSearchbyKind((short)2,ip)里面给了一个固定的2,应该得到有2条记录,但是结果却是空值。



banq

悄悄话
发表文章: 9613
注册时间: 2002年08月03日 17:08
Re: 请看看我的代码 2002年09月09日 17:53 到本帖网址 加入本帖到收藏夹 回复该主题
iterator办法试验下来,只适合prepareStatement
你用的是statement
statement一结束,resulset就清空,所以iterator得null
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月09日 20:47 到本帖网址 加入本帖到收藏夹 回复该主题
没有啊,我用的就是preparestament啊,请在看看,
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月09日 21:01 到本帖网址 加入本帖到收藏夹 回复该主题
我在调试过程中发现是先执行了finall内的代码在返回,所以在返回前就关闭了preparestament,这是导致的到null的原因,现在有一个问题,如果用这个方法的话,那什么时候关闭连接呢?
banq

悄悄话
发表文章: 9613
注册时间: 2002年08月03日 17:08
Re: 请看看我的代码 2002年09月10日 10:39 到本帖网址 加入本帖到收藏夹 回复该主题
我仔细看了,你的代码不是PrepareStatment 的问题
是你的Law retval = law;
问题

在iterator里需要直接操作ResultSet.而你是操作自己的定义Law
当然你这样做可以,注意和ResultSet的衔接问题。

总体原则是 iterator实际是一个传递指针,所以你要把确切的指针给iterator用来传递
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月10日 17:04 到本帖网址 加入本帖到收藏夹 回复该主题
在你提供的原代码中,不也是用了string吗,我只是把他改为自定义的Law而已啊,我想你所说的错误出处就是在传递指针上面,我对这段iterator代码不熟悉,可以说一下到底是怎样传指针的吗?
banq

悄悄话
发表文章: 9613
注册时间: 2002年08月03日 17:08
Re: 请看看我的代码 2002年09月10日 17:17 到本帖网址 加入本帖到收藏夹 回复该主题
你把law去掉,直接在iterator中操作Resulset

我那篇文章的代码你可以全部引用,你要做的就是把Mysql数据库操作封装在一个javabean中。不要在Jsp中 或一个方法中放置过多功能的代码,这是OO设计的一个基本原则。
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月11日 15:33 到本帖网址 加入本帖到收藏夹 回复该主题
我的jsp只是得到iterator,所有的操作就是在javabean中的,请你说明白点好吗?我还是不知道怎么做,怎么删掉law,有怎么直接在iterator中操作resultset了
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月11日 16:53 到本帖网址 加入本帖到收藏夹 回复该主题
这个代码是在javabean中,我也是不赞成在jsp中用过多的功能,请你在看看吧。我发现在return里面第一个rs.getShort()就出错了,抛出空值错
public Iterator getSearchbyKind(short kindid,String ip){
Connection con = null;
PreparedStatement pst = null;
String sql = null;
try {
con = MysqlConn.getConnection(ip);
sql = "select l.lawid,l.lawname,l.lawcontent,l.subkindid,l.kindid,k.kindname,l.bbtime,l.sstime,l.sxtime,l.unit,l.note from Law l,Kind k where l.kindid=k.kindid and l.kindid="+kindid;
pst = con.prepareStatement(sql);
final ResultSet rs = pst.executeQuery();
/*我在这里调试用
while(rs!=null && rs.next()){
law.LawId = rs.getShort("lawid");
law.LawName =rs.getString("lawname");
law.LawContent = rs.getString("lawcontent");
law.SubKindId =rs.getShort("subkindid");
}
一切都很正常而且能够赋值,*/

System.out.println("sql="+sql);
return new Iterator() {
private Law law;
public boolean hasNext() {
if (law == null) {
try {
if (! rs.next()) {
return false;
}
searchrowcount++;
//但是这里就不行了
law.LawId = rs.getShort("lawid");//出错的地方
law.LawName =rs.getString("lawname");
law.LawContent = rs.getString("lawcontent");
law.SubKindId =rs.getShort("subkindid");
law.KindId=rs.getShort("kindid");
law.KindName = rs.getString("kindname");
law.BbTime=rs.getDate("bbtime");
law.SsTime =rs.getDate("sstime");
law.Sxtime =rs.getDate("sxtime");
law.Unit = rs.getString("unit");
law.Note =rs.getString("note");
}catch(Exception e){e.printStackTrace();return false;}
}
return true;
}

public Object next() {
if (! hasNext()) {
throw new NoSuchElementException();
}
Law retval = law;
law = null;
return retval;
}

public void remove() {
throw new UnsupportedOperationException("no remove allowed");
}
};
}catch(Exception e){
e.printStackTrace();
return null;
} finally {
try { pst.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { con.close(); } catch(SQLException ne) { ne.printStackTrace(); }
}
}

错误:java.lang.NullPointerException
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月11日 16:57 到本帖网址 加入本帖到收藏夹 回复该主题
谢谢你,我的问题解决了,我知道哪错了,相信你也知道了
jacquiyan

悄悄话
发表文章: 16
注册时间: 2002年09月03日 17:00
Re: 请看看我的代码 2002年09月13日 10:44 到本帖网址 加入本帖到收藏夹 回复该主题
return new Iterator() {
private Law rlaw;
public boolean hasNext() {
if (rlaw == null) {
try {
if (! rs.next()) {
return false;
}
searchrowcount++;
Law law = new Law();
law.LawId = rs.getShort("lawid");
law.LawName =rs.getString("lawname");
law.LawContent = rs.getString("lawcontent");
law.SubKindId =rs.getShort("subkindid");
law.KindId=rs.getShort("kindid");
law.KindName = rs.getString("kindname");
law.BbTime=rs.getDate("bbtime");
law.SsTime =rs.getDate("sstime");
law.Sxtime =rs.getDate("sxtime");
law.Unit = rs.getString("unit");
law.Note =rs.getString("note");
rlaw = law;
}catch(java.sql.SQLException e){e.printStackTrace();return false;}
}
return true;
}

public Object next() {
if (! hasNext()) {
throw new NoSuchElementException();
}
Law retval = rlaw;
rlaw = null;
return retval;
}

public void remove() {
throw new UnsupportedOperationException("no remove allowed");
}
};

这是我的jsp的代码,他现在可以用了,我这样调用他Iterator i_law ;
〈% String keyword = (String)request.getParameter("keyword");
keyword=CodeChange.iso8859_1toGB2312(keyword);
Iterator i_law ;
//得到查询用的关键字
i_law = LawConn.getSearchbyWord(keyword,ip);

%〉

〈%try{
while (i_law.hasNext()){
%〉

〈a href="../law/lawdetail.jsp?lawid=〈%=((Law)i_law.next()).LawId%〉"〉〈%=((Law)i_law.next()).LawName%〉〈/a〉
〈%}
}catch(Exception e){%〉
读取数据库出错//总是跟着也被打印出来,而且记录是少了一个的
〈%e.printStackTrace();}%〉

但是在jsp页面打印结果每次都是少了一个记录,或者在打印最后一个记录是抛出NoSuchElementException()错误
标签
共有 16 回复 / 2Go 1 2
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG:
正在读取,请等待...
查询本论坛内 回复超过的热门帖子
标题
 
粗体: [b]文本[/b] 斜体: [i]文本[/i] 下划线 [u]文本[/u] 插入图片 [img]http://wwww.xxxx.com/img.ext[/img] 插入代码 [code]程序代码[/code]  插入url链接 [url]http://url[/url] / [url=http://url]URL加下滑线[/url] 插入附件 插入word文档 Txt等文件
内容
  提交时自动拷贝以上内容到剪贴板 Ctrl-V可取出;提问题前先查询标签列表

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区 推荐Chrome快速浏览本站
OpenSource JIVEJDON v3.5 Powered by JdonFramework Code © 2002-09 jdon.com

anti spam