package mypack;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;
public class test
{
public static void main(String args[])
{
try
{int t=0;BookDB db=new BookDB();   //BookDB是访问数据库的类
Collection books=null;
    books= db.getBookDetailsByTitle("j");//返回包含书名中有j字母的所有书的collection集合
    Iterator i = books.iterator();
    while(i.hasNext())
    {
    t++;
    }
    
    System.out.println(t);//查看名字中包含j的书一共有多少本
}catch(Exception e) {System.out.print(e.getMessage());}
}
}BookDB:
public class BookDB {  private ArrayList books;
  private String dbUrl =  "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=BookDB";
  private String dbuser = "sa";
  private String dbpwd = "12342234";
  
    public BookDB () throws Exception{
     try{ 
         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
        }
        catch(ClassNotFoundException e)
             {System.out.println(e.getMessage());}
        
  }  public Connection getConnection()throws Exception{...}
  
  public void closeConnection(Connection con){...}  public void closePrepStmt(PreparedStatement prepStmt){...}  public Collection getBookDetailsByTitle(String title) throws Exception
  {
   Connection con=null;
    //PreparedStatement prepStmt=null;
    Statement stmt= null;//
    ResultSet rs =null;
    //String aa= "%"+title+"%";
     books = new ArrayList();
    try {
      con=getConnection();
      String selectStatement = "select * " + "from books where title like '%"+title+"%'";
     // prepStmt = con.prepareStatement(selectStatement);
     stmt= con.createStatement();//
     
     // prepStmt.setString(1, aa);
      //rs = prepStmt.executeQuery();
      rs= stmt.executeQuery(selectStatement);//      while(rs.next()) {
        BookDetails bd = new BookDetails(rs.getString(1), rs.getString(2), rs.getString(3),
          rs.getFloat(4), rs.getInt(5), rs.getString(6),rs.getInt(7));
        //stmt.close();
        books.add(bd);       // return bd;
      }
      else {
        return null;
      }
    }finally{
      closeResultSet(rs);
      //closePrepStmt(prepStmt);
      stmt.close();
      closeConnection(con);
     
    }
    Collections.sort(books);
    return books;
  }为何执行test后,控制台只有一个光标闪来闪去,而不打印t呢???就连press any key to contiune都没有????
高分!!!!!!!!!
up 有分
谢谢大家

解决方案 »

  1.   

    "select * " + "from books where title like '%"+title+"%'";
    ============================只需要查询有多少本书?为什么这样呢?
    直接"select count(*) from booke where title like '%" + title & "'%";岂不省事?
      

  2.   

    就算没找到记录也应该打印"0"呀,
    这个bean在tomcat中运行正常,to trumplet
    你说的对,但是请再看一下我的问题
      

  3.   

    嗯,说得对,如果只需要知道书的数量为什么返回Collection啊,直接返回数量就好了。while(rs.next()) {
            BookDetails bd = new BookDetails(rs.getString(1), rs.getString(2), rs.getString(3),
              rs.getFloat(4), rs.getInt(5), rs.getString(6),rs.getInt(7));
            //stmt.close();
            books.add(bd);       // return bd;
          }
    这里面你确定有值了吗,放个断点看看。
      

  4.   

    test只是一个测试代码,我的bean有别的用,to msnsnd:肯定有值,此bean在tomcat中正常
      

  5.   

    把SQL文打印出来,然后铐下来,先试试它好用不。
      

  6.   

    我感觉不是sql的问题,应该是new BookDB的时候出的问题,因为我把System.out.print放在new BookDB前就没问题,放在它后面就不打印了
      

  7.   

    在 Iterator i = books.iterator();后面设置断点看看.
    可能是没有取到值......
      

  8.   

    new BookDB()这里是不是要加个参数传过去?要不怎么证明建立连接?
    我用的框架里是ConnectionAdapter con