|
小弟要把 MS Access 的data display 出来在 JTable 里面,
试过很多方式 也不成功,求神人 救济救济小弟。- <font size="2">import javax.swing.*;
- import javax.swing.table.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- public class ScrollableJTable{
-
-
- Connection conn;
- ResultSet rs;
- PreparedStatement pstmtSelect,pstmtUpdate,pstmtDelete;
-
- private void initializeDB(){
- try{
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- conn = DriverManager.getConnection("jdbc:odbc:CustomerRecordDS");
-
- pstmtSelect = conn.prepareStatement("SELECT * FROM CUSTOMER",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- pstmtUpdate = conn.prepareStatement("UPDATE CUSTOMER SET custName=?,contactsNum=?,Email=?,address=?,state=?,postCode=?,City=? WHERE custID = ?;");
- pstmtDelete = conn.prepareStatement("DELETE FROM CUSTOMER WHERE custID = ?;");
-
- rs = pstmtSelect.executeQuery();
- if(rs.next()){ //set into the text field the database value database > textfield
-
- //how to write ?
-
- }
- }catch(Exception ex){
- JOptionPane.showMessageDialog(null,ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
- }
- }
-
- public static void main(String[] args) {
- new ScrollableJTable();
-
- }
- public ScrollableJTable(){
- JFrame frame = new JFrame("STimeS Customer Database");
- JPanel panel = new JPanel(new FlowLayout());
- JPanel titlePane = new JPanel(new GridLayout(1,3));
- JPanel btnPane = new JPanel(new GridLayout(2,8));
-
- JButton delete = new JButton("Delete");
- JButton edit = new JButton("Edit");
- JButton save = new JButton("Save");
- JButton print = new JButton("Print");
-
- titlePane.add(new JLabel(""));
- titlePane.add(new JLabel(" STimeS Customer Database"));
- titlePane.add(new JLabel(""));
- frame.add(titlePane,BorderLayout.NORTH);
-
- String data[][] = {{"","","","","","","","",""},
- {"","","","","","","","",""},
- {"","","","","","","","",""},
- {"","","","","","","","",""},
- {"","","","","","","","",""},
- {"","","","","","","","",""},
-
-
- };
- String col[] = {"Customer ID","Customer Name","Tel.No","E-mail","address.","state","postCode","City"};
- JTable table = new JTable(data,col);
- //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
- //JTableHeader header = table.getTableHeader();
- //header.setBackground(Color.yellow);
- JScrollPane pane = new JScrollPane(table);
- panel.add(pane);
- table.getColumnModel().getColumn(0).setPreferredWidth(50);
- table.getColumnModel().getColumn(1).setPreferredWidth(120);
- table.getColumnModel().getColumn(2).setPreferredWidth(180);
- table.getColumnModel().getColumn(3).setPreferredWidth(50);
- table.getColumnModel().getColumn(4).setPreferredWidth(70);
- table.getColumnModel().getColumn(5).setPreferredWidth(120);
- table.setPreferredScrollableViewportSize(new Dimension(800, 300));
- table.setRowHeight(20);
- table.setColumnSelectionAllowed(false);
- frame.add(panel,BorderLayout.CENTER);
-
- btnPane.add(new JLabel(""));
- btnPane.add(delete);
- btnPane.add(new JLabel(""));
- btnPane.add(edit);
- btnPane.add(new JLabel(""));
- btnPane.add(save);
- btnPane.add(new JLabel(""));
- btnPane.add(print);
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- btnPane.add(new JLabel(""));
- frame.add(btnPane,BorderLayout.SOUTH);
- edit.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
-
- }
- });
-
-
-
- frame.setSize(1000,500);
- //frame.setUndecorated(true);
- //frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }</font>
复制代码 |
|