Java 动态向 JTable 中添加数据

  1 import java.awt.Toolkit;
  2 import javax.swing.SwingUtilities;
  3 import javax.swing.UIManager;
  4 import java.awt.Dimension;
  5 public class JTableAppClass {
  6     boolean packFrame = false;
  7    
  8     public JTableAppClass() {
  9         JTableAppFrame frame = new JTableAppFrame();
 10         // Validate frames that have preset sizes
 11         // Pack frames that have useful preferred size info, e.g. from their layout
 12         if (packFrame) {
 13             frame.pack();
 14         } else {
 15             frame.validate();
 16         }
 17         // Center the window
 18         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 19         Dimension frameSize = frame.getSize();
 20         if (frameSize.height > screenSize.height) {
 21             frameSize.height = screenSize.height;
 22         }
 23         if (frameSize.width > screenSize.width) {
 24             frameSize.width = screenSize.width;
 25         }
 26         frame.setLocation((screenSize.width - frameSize.width) / 2,
 27                           (screenSize.height - frameSize.height) / 2);
 28         frame.setVisible(true);
 29     }
 30    
 31     public static void main(String[] args) {
 32         SwingUtilities.invokeLater(new Runnable() {
 33             public void run() {
 34                 try {
 35                     UIManager.setLookAndFeel(UIManager.
 36                                              getSystemLookAndFeelClassName());
 37                 } catch (Exception exception) {
 38                     exception.printStackTrace();
 39                 }
 40                 new JTableAppClass();
 41             }
 42         });
 43     }
 44 }
 45 操作类:JTableAppFrame.java
 46 package jtabletest;
 47 import java.awt.Dimension;
 48 import javax.swing.JFrame;
 49 import javax.swing.JPanel;
 50 import javax.swing.JTable;
 51 import java.awt.Rectangle;
 52 import javax.swing.JScrollPane;
 53 import java.util.Vector;
 54 import java.sql.*;
 55 import java.util.ArrayList;
 56 import javax.swing.JLabel;
 57 import javax.swing.JTextField;
 58 import java.awt.Font;
 59 import javax.swing.JButton;
 60 import java.awt.event.ActionEvent;
 61 import java.awt.event.ActionListener;
 62 import javax.swing.JOptionPane;
 63 public class JTableAppFrame extends JFrame {
 64     JPanel contentPane;
 65     Vector CellsVector = new Vector();
 66     Vector TitleVector = new Vector();
 67     JScrollPane scp = new JScrollPane();
 68     JTable tab = null;
 69     DBCon dbcon = new DBCon();
 70     JLabel lbl_name = new JLabel();
 71     JLabel lbl_age = new JLabel();
 72     JLabel lbl_address = new JLabel();
 73     JTextField txt_name = new JTextField();
 74     JTextField txt_age = new JTextField();
 75     JTextField txt_address = new JTextField();
 76     JButton btn_add = new JButton();
 77     JButton btn_del = new JButton();
 78     JButton btn_update = new JButton();
 79     JButton btn_reset = new JButton();
 80     public JTableAppFrame() {
 81         try {
 82             setDefaultCloseOperation(EXIT_ON_CLOSE);
 83             jbInit();
 84         } catch (Exception exception) {
 85             exception.printStackTrace();
 86         }
 87     }
 88    
 89     private void jbInit() throws Exception {
 90         contentPane = (JPanel) getContentPane();
 91         contentPane.setLayout(null);
 92         this.setResizable(false);
 93         setSize(new Dimension(400, 340));
 94         setTitle("JTable");
 95         scp.setBounds(new Rectangle(46, 32, 297, 157));
 96         lbl_name.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
 97         lbl_name.setText("姓名:");
 98         lbl_name.setBounds(new Rectangle(46, 205, 42, 15));
 99         lbl_age.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
100         lbl_age.setText("年龄:");
101         lbl_age.setBounds(new Rectangle(200, 205, 42, 15));
102         lbl_address.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
103         lbl_address.setText("地址:");
104         lbl_address.setBounds(new Rectangle(46, 232, 42, 15));
105         txt_name.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
106         txt_name.setBounds(new Rectangle(98, 205, 72, 21));
107         txt_age.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
108         txt_age.setBounds(new Rectangle(250, 205, 72, 21));
109         txt_address.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
110         txt_address.setBounds(new Rectangle(98, 232, 72, 21));
111         btn_add.setBounds(new Rectangle(46, 271, 83, 25));
112         btn_add.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
113         btn_add.setText("添 加");
114         btn_add.addActionListener(new JTableAppFrame_btn_add_actionAdapter(this));
115         btn_del.setBounds(new Rectangle(155, 271, 83, 25));
116         btn_del.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
117         btn_del.setText("删 除");
118         btn_del.addActionListener(new JTableAppFrame_btn_del_actionAdapter(this));
119         btn_update.setBounds(new Rectangle(260, 271, 83, 25));
120         btn_update.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
121         btn_update.setText("修 改");
122         btn_update.addActionListener(new
123                                      JTableAppFrame_btn_update_actionAdapter(this));
124         btn_reset.setBounds(new Rectangle(258, 232, 83, 25));
125         btn_reset.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
126         btn_reset.setText("重 置");
127         btn_reset.addActionListener(new JTableAppFrame_btn_reset_actionAdapter(this));
128         contentPane.add(scp);
129         contentPane.add(lbl_name);
130         contentPane.add(lbl_age);
131         contentPane.add(lbl_address);
132         contentPane.add(txt_name);
133         contentPane.add(txt_age);
134         contentPane.add(txt_address);
135         contentPane.add(btn_del);
136         contentPane.add(btn_update);
137         contentPane.add(btn_add);
138         contentPane.add(btn_reset);
139         dbcon.connectDB();
140         this.showTable();
141         tab = new JTable(CellsVector, TitleVector);
142         scp.getViewport().add(tab);
143     }
144    
145     public void showTable() {
146         this.TitleVector.add("姓名");
147         this.TitleVector.add("年龄");
148         this.TitleVector.add("地址");
149         dbcon.select();
150         ArrayList list = dbcon.select();
151         for (int i = 0; i < list.size(); i++) {
152             Student stu = (Student) list.get(i);
153             Vector v = new Vector();
154             v.add(stu.getName());
155             v.add(stu.getAge());
156             v.add(stu.getAddress());
157             CellsVector.add(v);
158         }
159     }
160 
161    
162     String name;
163     String age;
164     String address;
165     public int checkInformation() {
166         name = this.txt_name.getText();
167         age = this.txt_age.getText();
168         address = this.txt_address.getText();
169         if (name.equals("")) {
170             JOptionPane.showMessageDialog(this, "您好!请输入姓名!", "提示", 1);
171             this.txt_name.grabFocus();
172             return 0;
173         }
174         if (age.equals("")) {
175             JOptionPane.showMessageDialog(this, "您好!请输入年龄!", "提示", 1);
176             this.txt_age.grabFocus();
177             return 0;
178         }
179         char[] ans = age.toCharArray();
180         for (int i = 0; i < ans.length; i++) {
181             if (!Character.isDigit(ans[i])) {
182                 JOptionPane.showMessageDialog(this, "您好!年龄输入错误!", "提示", 1);
183                 this.txt_age.setText("");
184                 this.txt_age.grabFocus();
185                 return 0;
186             }
187         }
188         if (age.length() > 3) {
189             JOptionPane.showMessageDialog(this, "您好!年龄最高只能为 100 岁!", "提示", 1);
190             this.txt_age.setText("");
191             this.txt_age.grabFocus();
192             return 0;
193         }
194         if (address.equals("")) {
195             JOptionPane.showMessageDialog(this, "您好!请输入地址!", "提示", 1);
196             this.txt_address.grabFocus();
197             return 0;
198         }
199         ages = Integer.valueOf(age);
200         return 1;
201     }
202    
203     int ages;
204     public void btn_add_actionPerformed(ActionEvent e) {
205         if (this.checkInformation() == 0) {
206             return;
207         }
208         dbcon.insert(name, ages, address);
209         this.CellsVector.clear();
210         this.showTable();
211         this.tab.updateUI();
212         this.btn_reset_actionPerformed(e);
213     }
214    
215     ResultSet rs;
216     public void btn_del_actionPerformed(ActionEvent e) {
217         String name = this.txt_name.getText();
218         this.txt_age.setEditable(false);
219         this.txt_address.setEditable(false);
220         if (name.equals("")) {
221             JOptionPane.showMessageDialog(this, "您好!请输入姓名!", "提示", 1);
222             this.txt_name.grabFocus();
223             return;
224         }
225         try {
226             String sql = "select * from Student where name=?";
227             PreparedStatement ps = DBCon.con.prepareStatement(sql);
228             ps.setString(1, name);
229             rs = ps.executeQuery();
230             if (rs.next()) {
231                 ps.close();
232                 dbcon.delete(name);
233                 this.CellsVector.clear();
234                 this.showTable();
235                 this.tab.updateUI();
236                 this.btn_reset_actionPerformed(e);
237             } else {
238                 JOptionPane.showMessageDialog(this, "您好!该学员不存在!", "提示", 1);
239                 this.btn_reset_actionPerformed(e);
240                 return;
241             }
242         } catch (SQLException ex) {
243             System.out.println("核对学员信息发生异常" + ex.getMessage());
244         }
245     }
246    
247     public void btn_update_actionPerformed(ActionEvent e) {
248         if (this.checkInformation() == 0) {
249             return;
250         }
251         try {
252             String sql = "select * from Student where name=?";
253             PreparedStatement ps = DBCon.con.prepareStatement(sql);
254             ps.setString(1, name);
255             rs = ps.executeQuery();
256             if (rs.next()) {
257                 ps.close();
258                 dbcon.update(name, ages, address);
259                 this.CellsVector.clear();
260                 this.showTable();
261                 this.tab.updateUI();
262                 this.btn_reset_actionPerformed(e);
263             } else {
264                 JOptionPane.showMessageDialog(this, "您好!该学员不存在!", "提示", 1);
265                 this.btn_reset_actionPerformed(e);
266                 return;
267             }
268         } catch (SQLException ex) {
269             System.out.println("核对学员信息发生异常" + ex.getMessage());
270         }
271     }
272    
273     public void btn_reset_actionPerformed(ActionEvent e) {
274         this.txt_name.setText("");
275         this.txt_age.setText("");
276         this.txt_address.setText("");
277         this.txt_age.setEditable(true);
278         this.txt_address.setEditable(true);
279     }
280 }
281 
282 class JTableAppFrame_btn_del_actionAdapter implements ActionListener {
283     private JTableAppFrame adaptee;
284     JTableAppFrame_btn_del_actionAdapter(JTableAppFrame adaptee) {
285         this.adaptee = adaptee;
286     }
287     public void actionPerformed(ActionEvent e) {
288         adaptee.btn_del_actionPerformed(e);
289     }
290 }
291 
292 class JTableAppFrame_btn_update_actionAdapter implements ActionListener {
293     private JTableAppFrame adaptee;
294     JTableAppFrame_btn_update_actionAdapter(JTableAppFrame adaptee) {
295         this.adaptee = adaptee;
296     }
297     public void actionPerformed(ActionEvent e) {
298         adaptee.btn_update_actionPerformed(e);
299     }
300 }
301 
302 class JTableAppFrame_btn_reset_actionAdapter implements ActionListener {
303     private JTableAppFrame adaptee;
304     JTableAppFrame_btn_reset_actionAdapter(JTableAppFrame adaptee) {
305         this.adaptee = adaptee;
306     }
307     public void actionPerformed(ActionEvent e) {
308         adaptee.btn_reset_actionPerformed(e);
309     }
310 }
311 
312 class JTableAppFrame_btn_add_actionAdapter implements ActionListener {
313     private JTableAppFrame adaptee;
314     JTableAppFrame_btn_add_actionAdapter(JTableAppFrame adaptee) {
315         this.adaptee = adaptee;
316     }
317     public void actionPerformed(ActionEvent e) {
318         adaptee.btn_add_actionPerformed(e);
319     }
320 }
321 连接数据库类:DBCon.java
322 package jtabletest;
323 import java.sql.*;
324 import java.util.ArrayList;
325 import javax.swing.JOptionPane;
326 public class DBCon {
327     static Connection con;
328     private final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
329     private final String URL = "jdbc:odbc:test";
330     ResultSet rs = null;
331     Statement st = null;
332     public DBCon() {
333     }
334    
335     public void connectDB() {
336         try {
337             Class.forName(DRIVER);
338         } catch (ClassNotFoundException ex) {
339             System.out.println(ex.getMessage());
340         }
341         if (con == null) {
342             try {
343                 con = DriverManager.getConnection(URL);
344             } catch (SQLException ex) {
345                 System.out.println("创建连接发生异常:" + ex.getMessage());
346                 System.exit(0);
347             }
348         }
349     }
350    
351     public ArrayList select() {
352         ArrayList list = new ArrayList();
353         String sql = "select * from Student";
354         try {
355             st = con.createStatement();
356             rs = st.executeQuery(sql);
357             while (rs.next()) {
358                 Student stu = new Student();
359                 stu.setName(rs.getString(2));
360                 stu.setAge(rs.getInt(3));
361                 stu.setAddress(rs.getString(4));
362                 list.add(stu);
363             }
364         } catch (SQLException ex) {
365             System.out.println("查询数据发生异常:" + ex.getMessage());
366         } finally {
367             try {
368                 rs.close();
369             } catch (SQLException ex1) {
370                 System.out.println("查询数据关闭语句异常:" + ex1.getMessage());
371             }
372         }
373         return list;
374     }
375    
376     public void insert(String name, int age, String address) {
377         String sql = "insert into Student values(?,?,?)";
378         PreparedStatement ps = null;
379         try {
380             ps = con.prepareStatement(sql);
381             ps.setString(1, name);
382             ps.setInt(2, age);
383             ps.setString(3, address);
384             if (ps.executeUpdate() != 0) {
385                 JOptionPane.showMessageDialog(null, "恭喜!插入数据成功!", "消息", 1);
386             }
387         } catch (SQLException ex) {
388             System.out.println("插入数据发生异常:" + ex.getMessage());
389         } finally {
390             try {
391                 ps.close();
392             } catch (SQLException ex1) {
393                 System.out.println("插入数据关闭语句异常:" + ex1.getMessage());
394             }
395         }
396     }
397    
398     public void update(String name, int age, String address) {
399         String sql = "update Student set age = ?,address = ? where name =?";
400         PreparedStatement ps = null;
401         try {
402             ps = con.prepareStatement(sql);
403             ps.setInt(1, age);
404             ps.setString(2, address);
405             ps.setString(3, name);
406             if (ps.executeUpdate() != 0) {
407                 JOptionPane.showMessageDialog(null, "恭喜!更新数据成功!", "消息", 1);
408             }
409         } catch (SQLException ex) {
410             System.out.println("修改数据发生异常!");
411         } finally {
412             try {
413                 ps.close();
414             } catch (SQLException ex1) {
415                 System.out.println("修改数据关闭语句异常:" + ex1.getMessage());
416             }
417         }
418     }
419    
420     public void delete(String name) {
421         String sql = "delete from Student where name =?";
422         PreparedStatement ps = null;
423         int delNumber;
424         try {
425             ps = con.prepareStatement(sql);
426             ps.setString(1, name);
427             delNumber = ps.executeUpdate();
428             if (delNumber != 0) {
429                 JOptionPane.showMessageDialog(null, "恭喜!删除数据成功!", "消息", 1);
430             }
431         } catch (SQLException ex) {
432             System.out.println("删除数据发生异常:" + ex.getMessage());
433         } finally {
434             try {
435                 ps.close();
436             } catch (SQLException ex1) {
437                 System.out.println("删除数据关闭语句异常:" + ex1.getMessage());
438             }
439         }
440     }
441    
442     public void destoryConnection() {
443         if (con != null) {
444             try {
445                 con.close();
446             } catch (SQLException ex) {
447                 System.out.println("释放连接异常:" + ex.getMessage());
448             }
449         }
450     }
451 }
452 JavaBean 类:Student.java
453 package jtabletest;
454 public class Student {
455     private String name;
456     private int age;
457     private String address;
458     public Student() {
459     }
460     public void setName(String name) {
461         this.name = name;
462     }
463     public void setAge(int age) {
464         this.age = age;
465     }
466     public void setAddress(String address) {
467         this.address = address;
468     }
469     public String getName() {
470         return name;
471     }
472     public int getAge() {
473         return age;
474     }
475     public String getAddress() {
476         return address;
477     }
478 }

 

posted @ 2016-10-28 17:05  Boblim  阅读(18625)  评论(1编辑  收藏  举报