回覆列表
  • 1 # 匠工加工

    一個類似的例子,從資料庫裡取資料放table裡,請參考。

    package Libary;

    import java.awt.Cursor;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    import java.util.Vector;

    import javax.swing.*;

    import javax.swing.table.DefaultTableModel;

    public class BorrowBookLog extends JFrame{

    /**

    *

    */

    private static final long serialVersionUID = 5922888622610809963L;

    String BBookName, BBookId;

    JButton Borrow, Cancel;

    Statement SearchStmt;

    @SuppressWarnings({ "rawtypes", "unchecked" })

    BorrowBookLog(){

    ConDB CB3 = new ConDB();

    CB3.connectionDB();

    try {

    SearchStmt = CB3.dbConn.createStatement();

    } catch (SQLException e1) {

    // TODO 自動生成的 catch 塊

    e1.printStackTrace();

    }

    DefaultTableModel tableModel = new DefaultTableModel();

    String[] tableHeads= {"圖書編號","圖書名稱","借閱人", "借閱時間"};

    Vector cell;

    Vector row = new Vector();

    Vector tableHeadName = new Vector();

    for(int i = 0;i<tableHeads.length;i++){

    tableHeadName.add(tableHeads[i]);

    }

    try {

    ResultSet s =SearchStmt.executeQuery("select * from BorrowedBooks where StudentId ="+ """+ StudentMainFrame.Name+""");

    while(s.next()){

    cell = new Vector();

    cell.add(s.getString("BookId"));

    cell.add(s.getString("BookName"));

    cell.add(s.getString("StudentId"));

    cell.add(s.getString("BorrowedDate"));

    row.add(cell);

    BBookId = s.getString("BookId");

    BBookName = s.getString("BookName");

    }

    } catch (SQLException e1) {

    // TODO 自動生成的 catch 塊

    e1.printStackTrace();

    }

    setTitle("查詢結果");

    tableModel.setDataVector(row, tableHeadName);

    JTable table = new JTable(tableModel);

    table.setRowHeight(20);

    table.setCursor(new Cursor(12));

    getContentPane().setLayout(null);

    JScrollPane scrollPane = new JScrollPane(table);

    scrollPane.setBounds(10, 10, 420, 200);

    scrollPane.setCursor(new Cursor(12));

    this.getContentPane().add(scrollPane);

    setLocation(450,220);

    setSize(450,300);

    setVisible(true);

    }

    public static void mian(String args[]){

    }

    }

  • 中秋節和大豐收的關聯?
  • 主機板電源插槽怎麼檢查是否通電?