日期:2014-05-20 浏览次数:20892 次
//用书名查询书籍信息
private void QueryByName()throws Exception
{
// TODO Auto-generated method stub
String name;
System.out.print("请输入要查找的书名:");
Scanner scanner = new Scanner(System.in);
name = scanner.next();
String querySql = "select book.BookID,book.BookName,book.BookPage,author.Author,author.Translater,type.TypeName"
+ " from book,author,type where book.BookName like \"%"
+ name + "%\" and author.BookID = book.BookID and "
+ "book.TypeID = type.TypeID;";
System.out.println(querySql);
System.in.read();
ResultSet rs = null;
DataBase database = null;
try
{
database = new DataBase();
database.InitDatabase("book.db");
rs = database.RunQuery(querySql);
if (!rs.next())
{
System.out.println("没有结果......");
System.in.read();
}
int n = 0;
while (rs.next())
{
System.out.println("书籍ID:" + rs.getInt("BookID"));
System.out.println("书籍名称:" + rs.getString("BookName"));
System.out.println("书籍页数:" + rs.getInt("BookPage"));
System.out.println("作者:" + rs.getString("Author"));
System.out.println("译者:" + rs.getString("Translater"));
System.out.println("书籍分类:" + rs.getString("TypeName"));
n++;
}
System.out.println("共有" + n + "项结果。");
System.out.println("查询结果输出完毕,按回车键继续......");
System.in.read();
System.in.read();
}
catch (Exception e)
{
System.out.println("查询书籍信息出错!详细信息:" + e.getMessage() +
"请按回车键继续......");
System.in.read();
System.in.read();
return;
}
finally
{
if (rs != null)
{
rs.close();
}
database.CloseDatabase();
}
}
public ResultSet RunQuery(String sql)throws Exception
{
ResultSet rs;
rs = state.executeQuery(sql);
return rs;
}
sqlite> .schema CREATE TABLE [author] ( [BookID] INTEGER PRIMARY KEY, [Translater] VARCHAR(100), [Author] VARCHAR(100) not null); CREATE TABLE [book] ( [BookID] INTEGER primary key autoincrement, [BookName] VARCHAR(100) not null, [BookPage] INTEGER not null, [TypeID] INTEGER not null); CREATE TABLE [type] ( [TypeID] INTEGER PRIMARY KEY, [TypeName] VARCHAR(100) not null); sqlite> select * from book; 2|sdf|34|342 3|sdf|4334|434 4|dsfds|344|545 32|sdfs|43|434 345|鍝堝搱|324|3428 2324|娴嬭瘯|3543|234235 3324|浣犲ソ|323|344323
请输入要查找的书名:sdf
select book.BookID,book.BookName,book.BookPage,author.Author,author.Translater,type.TypeName from book,author,type where book.BookName like "%sdf%" and author.BookID = book.BookID and book.TypeID = type.TypeID;
共有0项结果。
查询结果输出完毕,按回车键继续......