📄 bookbean.java
字号:
package czm;
import java.sql.*;
import java.sql.Connection;
public class BookBean {
private String bookISBN = null; //图书编号
private String bookName = null; //书名
private String bookAuthor = null; //作者
private String publisher = null; //出版社
private String introduce = null; //简介
private String price = null; //价格
private static String strDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static String strDBUrl = "jdbc:odbc:bookstore";
private Connection conn =null;
private ResultSet rs = null;
public BookBean(){
//加载驱动
try {
Class.forName(strDBDriver );
}
catch(java.lang.ClassNotFoundException e){
System.err.println("BookBean ():" + e.getMessage());
}
}
//取当前书库中全部图书信息
public ResultSet getBookList(){
String strSql = null;
try{
//建立与数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
strSql = "Select bookISBN,bookName,bookAuthor,publisher,price from bookInfo ";
rs = stmt.executeQuery(strSql);
}
//捕获异常
catch(SQLException e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
return rs ;
}
//根据图书的编号给图书的其他信息赋值
private void getBookInfo(String ISBN){
String strSql = null;
bookName = null;
bookAuthor = null;
publisher = null;
introduce = null;
price = null;
try{
//建立和数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
strSql = "Select * from bookInfo where bookISBN = '" + ISBN + "'";
rs = stmt.executeQuery(strSql);
while (rs.next()){
bookName = rs.getString("bookName");
bookAuthor = rs.getString("bookAuthor");
publisher = rs.getString("publisher");
introduce = rs.getString("introduce");
price = rs.getString("price");
}
}
//捕获异常
catch(SQLException e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
}
public ResultSet getBookListFindbyName(){
String strSql1 = null;
try{
//建立与数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
strSql1 = "Select bookISBN,bookName,bookAuthor,publisher,price from bookInfo where bookName=";
rs = stmt.executeQuery(strSql1);
}
//捕获异常
catch(SQLException e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
return rs ;
}
//给图书的编号赋值,同时调用函数给图书的其他信息赋值
public void setBookISBN (String ISBN){
this.bookISBN = ISBN;
getBookInfo(bookISBN);
}
//取图书编号
public String getBookISBN (){
return bookISBN ;
}
//取书名
// public void setBookName(String name){
// this.bookName=name;
// }
public String getBookName(){
return bookName ;
}
//取作者信息
public String getBookAuthor(){
return bookAuthor;
}
//取出版社信息
public String getPublisher(){
return publisher;
}
//取图书简介
public String getIntroduce(){
return introduce ;
}
//取图书价格
public String getPrice(){
return price;
}
private void getBookInfobyName(String name){
String strSql = null;
bookName = null;
bookAuthor = null;
publisher = null;
introduce = null;
price = null;
try{
//建立和数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
strSql = "Select * from bookInfo where bookISBN = '" + name + "'";
rs = stmt.executeQuery(strSql);
while (rs.next()){
bookName = rs.getString("bookName");
bookAuthor = rs.getString("bookAuthor");
publisher = rs.getString("publisher");
introduce = rs.getString("introduce");
price = rs.getString("price");
}
}
//捕获异常
catch(SQLException e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
}
public ResultSet getABookInfbyName(String name){
String strSql1 = null;
try{
//建立与数据库的连接
Class.forName(strDBDriver );
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
strSql1 = "Select * from bookInfo where bookName='"+name+"'";
rs = stmt.executeQuery(strSql1);
}
//捕获异常
catch(Exception e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
return rs ;
}
public ResultSet getMohu(String name){
String strSql1 = null;
try{
//建立与数据库的连接
Class.forName(strDBDriver );
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
strSql1 = "Select * from bookInfo where bookName like '%"+name+"%'";
rs=stmt.executeQuery(strSql1);
}
//捕获异常
catch(Exception e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
return rs ;
}
public ResultSet getBookListbyType(String typen){
String strSql = null;
try{
//建立与数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
strSql = "Select bookISBN,bookName,bookAuthor,publisher,price from bookInfo where book_type='"+typen+"'";
rs = stmt.executeQuery(strSql);
}
//捕获异常
catch(SQLException e){
System.err.println("BookBean.getBookList():" + e.getMessage());
}
return rs ;
}
//新增函数
//select专用
public ResultSet executeQuery(String Sql){
String strSql = null;
try{
//建立与数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(Sql);
}
//捕获异常
catch(SQLException e){
System.err.println( e.getMessage());
}
return rs ;
}
//insert updata delet
public int executeUpdate(String sql){
int n=0;
try{
//建立与数据库的连接
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
n= stmt.executeUpdate(sql);
}
//捕获异常
catch(SQLException e){
System.err.println( e.getMessage());
}
return n ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -