图书管理系统

来源:互联网 发布:人工智能的文献综述 编辑:程序博客网 时间:2024/05/24 05:53
package librarySystem;
import java.sql.*;
import java.util.*;
  
/*
 * 图书表模型
 * */
import javax.swing.table.*;
@SuppressWarnings("serial")
public class BookTableModel extends AbstractTableModel{
  //表的元素
  private Vector<Vector<String>> rowData;
  private Vector<String> colName;
  // 数据库
  private PreparedStatement stmt;
  private ResultSet result;
  public BookTableModel(String sql) throws SQLException{
    this.initData(sql);
  }
  public BookTableModel() throws SQLException{
    this.initData("SELECT * FROM book_info");
  }
  public void initData(String sql) throws SQLException{
    setRowData(new Vector<Vector<String>>());
    setColName(new Vector<String>());
    getColName().add("书号");
    getColName().add("书名");
    getColName().add("作者");
    getColName().add("出版社");
    getColName().add("出版时间");
    getColName().add("价格");
    /*
     * 数据库的导入
     * */
    try{
      Class.forName("com.mysql.jdbc.Driver");
    }catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    String url="jdbc:mysql://localhost:3306/device";
    String user="root";
    String password="zjq1314520";
    Connection con=DriverManager.getConnection(url,user,password);
    stmt = con.prepareStatement(sql);
    result=stmt.executeQuery();
    importSQL();
  }
  voidimportSQL() throwsSQLException{
    // TODO Auto-generated method stub
    @SuppressWarnings("unused")
    booleansignNull=true;
    while(result.next()){
      Vector<String> item=newVector<String>();
      for(inti=1;i<7;i++){
        item.add(result.getString(i));
      }
      getRowData().add(item);
      signNull=false;
    }
    result.close();
  }
  @Override
  publicint getColumnCount() {//得到列数
    // TODO Auto-generated method stub
    returnthis.colName.size();
  }
  
  @Override
  publicint getRowCount() {//得到行数
    // TODO Auto-generated method stub
    returnthis.rowData.size();
  }
  
  @Override
  publicObject getValueAt(introw, int col) {//得到某行某列的数据
    // TODO Auto-generated method stub
    return(this.rowData.get(row)).get(col);
  }
  
  @Override
  publicString getColumnName(intcolumn) {
    // TODO Auto-generated method stub
    returnthis.colName.get(column);
  }
    
  publicVector<Vector<String>> getRowData() {
    returnrowData;
  }
  publicvoid setRowData(Vector<Vector<String>> rowData) {
    this.rowData = rowData;
  }
  publicVector<String> getColName() {
    returncolName;
  }
  publicvoid setColName(Vector<String> colName) {
    this.colName = colName;
  }
  publicvoid addBook(String sql){
    try{
      stmt.executeUpdate(sql);
    }catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
//   initData("SELECT * FROM book_info");
  }
  publicvoid deleteBook(String sql){
    try{
      stmt.executeUpdate(sql);
    }catch (SQLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  }
}
0 0
原创粉丝点击