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 有分
谢谢大家
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 有分
谢谢大家
============================只需要查询有多少本书?为什么这样呢?
直接"select count(*) from booke where title like '%" + title & "'%";岂不省事?
这个bean在tomcat中运行正常,to trumplet
你说的对,但是请再看一下我的问题
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;
}
这里面你确定有值了吗,放个断点看看。
可能是没有取到值......
我用的框架里是ConnectionAdapter con