|
|
|
|
|
|
|
请看看我的代码
|
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
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月08日 17:52
|
|
|
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月09日 08:50
|
|
|
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月09日 09:28
|
|
|
|
将涉及数据库访问的全部纳入 这是java的通用写法,try语句你要常用
|
|
|
|
|
|
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条记录,但是结果却是空值。
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月09日 17:53
|
|
|
iterator办法试验下来,只适合prepareStatement 你用的是statement statement一结束,resulset就清空,所以iterator得null
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月09日 20:47
|
|
|
|
没有啊,我用的就是preparestament啊,请在看看,
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月09日 21:01
|
|
|
|
我在调试过程中发现是先执行了finall内的代码在返回,所以在返回前就关闭了preparestament,这是导致的到null的原因,现在有一个问题,如果用这个方法的话,那什么时候关闭连接呢?
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月10日 10:39
|
|
|
我仔细看了,你的代码不是PrepareStatment 的问题 是你的Law retval = law; 问题
在iterator里需要直接操作ResultSet.而你是操作自己的定义Law 当然你这样做可以,注意和ResultSet的衔接问题。
总体原则是 iterator实际是一个传递指针,所以你要把确切的指针给iterator用来传递
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月10日 17:04
|
|
|
|
在你提供的原代码中,不也是用了string吗,我只是把他改为自定义的Law而已啊,我想你所说的错误出处就是在传递指针上面,我对这段iterator代码不熟悉,可以说一下到底是怎样传指针的吗?
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月10日 17:17
|
|
|
你把law去掉,直接在iterator中操作Resulset
我那篇文章的代码你可以全部引用,你要做的就是把Mysql数据库操作封装在一个javabean中。不要在Jsp中 或一个方法中放置过多功能的代码,这是OO设计的一个基本原则。
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月11日 15:33
|
|
|
|
我的jsp只是得到iterator,所有的操作就是在javabean中的,请你说明白点好吗?我还是不知道怎么做,怎么删掉law,有怎么直接在iterator中操作resultset了
|
|
|
|
|
|
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
|
|
|
|
|
|
Re: 请看看我的代码
|
2002年09月11日 16:57
|
|
|
|
谢谢你,我的问题解决了,我知道哪错了,相信你也知道了
|
|
|
|
|
|
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()错误
|
|
|
|