banq
2002-09-10 17:17

你把law去掉,直接在iterator中操作Resulset

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

jacquiyan
2002-09-11 15:33

我的jsp只是得到iterator,所有的操作就是在javabean中的,请你说明白点好吗?我还是不知道怎么做,怎么删掉law,有怎么直接在iterator中操作resultset了

jacquiyan
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
2002-09-11 16:57

谢谢你,我的问题解决了,我知道哪错了,相信你也知道了

jacquiyan
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()错误