佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1555|回复: 0

HELP******Java netBeans MS Access link 去 JTable******HELP

[复制链接]
发表于 6-12-2012 08:36 PM | 显示全部楼层 |阅读模式
小弟要把 MS Access 的data display 出来在 JTable 里面,
      试过很多方式 也不成功,求神人 救济救济小弟。
  1. <font size="2">import javax.swing.*;
  2. import javax.swing.table.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.sql.*;

  6. public class ScrollableJTable{
  7.         
  8.    
  9.         Connection conn;
  10.         ResultSet rs;
  11.         PreparedStatement pstmtSelect,pstmtUpdate,pstmtDelete;
  12.         
  13.         private void initializeDB(){
  14.             try{
  15.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  16.                 conn = DriverManager.getConnection("jdbc:odbc:CustomerRecordDS");
  17.                
  18.                 pstmtSelect = conn.prepareStatement("SELECT * FROM CUSTOMER",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  19.                 pstmtUpdate = conn.prepareStatement("UPDATE CUSTOMER SET custName=?,contactsNum=?,Email=?,address=?,state=?,postCode=?,City=? WHERE custID = ?;");
  20.                 pstmtDelete = conn.prepareStatement("DELETE FROM CUSTOMER WHERE custID = ?;");
  21.                
  22.                 rs = pstmtSelect.executeQuery();
  23.                 if(rs.next()){ //set into the text field the database value   database > textfield
  24.                     
  25.                     //how to write ?
  26.               
  27.                 }  
  28.             }catch(Exception ex){
  29.                  JOptionPane.showMessageDialog(null,ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
  30.             }
  31.         }
  32.    
  33.         public static void main(String[] args) {
  34.                 new ScrollableJTable();
  35.                
  36.         }
  37.         public ScrollableJTable(){
  38.                 JFrame frame = new JFrame("STimeS Customer Database");
  39.                 JPanel panel = new JPanel(new FlowLayout());
  40.                 JPanel titlePane = new JPanel(new GridLayout(1,3));
  41.                 JPanel btnPane = new JPanel(new GridLayout(2,8));
  42.                
  43.                 JButton delete = new JButton("Delete");
  44.                 JButton edit = new JButton("Edit");
  45.                 JButton save = new JButton("Save");
  46.                 JButton print = new JButton("Print");
  47.                
  48.                 titlePane.add(new JLabel(""));
  49.                 titlePane.add(new JLabel("                      STimeS Customer Database"));
  50.                 titlePane.add(new JLabel(""));
  51.                 frame.add(titlePane,BorderLayout.NORTH);
  52.                
  53.                 String data[][] = {{"","","","","","","","",""},
  54.                                    {"","","","","","","","",""},
  55.                                    {"","","","","","","","",""},
  56.                                    {"","","","","","","","",""},
  57.                                    {"","","","","","","","",""},
  58.                                    {"","","","","","","","",""},
  59.                                    
  60.                      
  61.                 };
  62.                 String col[] = {"Customer ID","Customer Name","Tel.No","E-mail","address.","state","postCode","City"};
  63.                 JTable table = new JTable(data,col);
  64.                 //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  65.                 //JTableHeader header = table.getTableHeader();
  66.                 //header.setBackground(Color.yellow);
  67.                 JScrollPane pane = new JScrollPane(table);       
  68.                 panel.add(pane);
  69.                 table.getColumnModel().getColumn(0).setPreferredWidth(50);
  70.                 table.getColumnModel().getColumn(1).setPreferredWidth(120);
  71.                 table.getColumnModel().getColumn(2).setPreferredWidth(180);
  72.                 table.getColumnModel().getColumn(3).setPreferredWidth(50);
  73.                 table.getColumnModel().getColumn(4).setPreferredWidth(70);
  74.                 table.getColumnModel().getColumn(5).setPreferredWidth(120);
  75.                 table.setPreferredScrollableViewportSize(new Dimension(800, 300));
  76.                 table.setRowHeight(20);
  77.                 table.setColumnSelectionAllowed(false);
  78.                 frame.add(panel,BorderLayout.CENTER);
  79.                
  80.                 btnPane.add(new JLabel(""));
  81.                 btnPane.add(delete);
  82.                 btnPane.add(new JLabel(""));
  83.                 btnPane.add(edit);
  84.                 btnPane.add(new JLabel(""));
  85.                 btnPane.add(save);
  86.                 btnPane.add(new JLabel(""));
  87.                 btnPane.add(print);
  88.                 btnPane.add(new JLabel(""));
  89.                 btnPane.add(new JLabel(""));
  90.                 btnPane.add(new JLabel(""));
  91.                 btnPane.add(new JLabel(""));
  92.                 btnPane.add(new JLabel(""));
  93.                 btnPane.add(new JLabel(""));
  94.                 btnPane.add(new JLabel(""));
  95.                 btnPane.add(new JLabel(""));
  96.                 btnPane.add(new JLabel(""));
  97.                 btnPane.add(new JLabel(""));
  98.                 frame.add(btnPane,BorderLayout.SOUTH);
  99.                 edit.addActionListener(new ActionListener(){
  100.                     public void actionPerformed(ActionEvent e){
  101.                         
  102.                     }
  103.                 });
  104.                
  105.                
  106.                
  107.                 frame.setSize(1000,500);
  108.                 //frame.setUndecorated(true);
  109.                 //frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  110.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  111.                 frame.setVisible(true);
  112.         }
  113. }</font>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 8-10-2025 05:31 AM , Processed in 0.112747 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表