使用JTable组件操作数据库中的表
使用JTable组件显示message表的记录、更新message表的记录、像message表插入记录。本例共有4个Java源文件,需分别保存编译,其中Example.java是主类,效果图如
Example.java代码
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.sql.*; 5 public class Example{ 6 public static void main(String args[ ]){ 7 try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 8 } 9 catch(ClassNotFoundException e){ 10 System.out.println(""+e); 11 } 12 DatabaseWin win=new DatabaseWin(); 13 } 14 } 15 class DatabaseWin extends JFrame implements ActionListener{//主窗口 16 JMenuBar menubar; 17 JMenu menu; 18 JMenuItem itemShow,itemUpdate,itemInsert; 19 ShowRecord showRecord; 20 ModifyRecord modifyRecord; 21 InsertRecord insertRecord; 22 DatabaseWin(){ 23 menubar=new JMenuBar(); 24 menu=new JMenu("操作数据库"); 25 itemShow=new JMenuItem("显示记录"); 26 itemUpdate=new JMenuItem("更新记录"); 27 itemInsert=new JMenuItem("插入记录"); 28 itemShow.addActionListener(this); 29 itemUpdate.addActionListener(this); 30 itemInsert.addActionListener(this); 31 menu.add(itemShow); 32 menu.add(itemUpdate); 33 menu.add(itemInsert); 34 menubar.add(menu); 35 showRecord=new ShowRecord("显示记录对话框"); 36 modifyRecord=new ModifyRecord("修改记录对话框"); 37 insertRecord=new InsertRecord("插入记录对话框"); 38 setJMenuBar(menubar); 39 setBounds(100,100,370,250); 40 setVisible(true); 41 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 42 } 43 public void actionPerformed(ActionEvent e){ 44 if(e.getSource()==itemShow) 45 showRecord.setVisible(true); 46 else if(e.getSource()==itemUpdate) 47 modifyRecord.setVisible(true); 48 else if(e.getSource()==itemInsert) 49 insertRecord.setVisible(true); 50 } 51 }
ShowRecord.java代码
1 import javax.swing.* ; 2 import java.awt.* ; 3 import java.awt.event.* ; 4 import java.sql.* ; 5 public class ShowRecord extends JDialog implements ActionListener //负责显示记录的类 6 { 7 JTable table ; 8 Object a[][] ; 9 Object name[]={"学号","姓名","出生日期","身高"} ; 10 JButton showRecord ; 11 Connection con ; 12 Statement sql ; 13 ResultSet rs ; 14 ShowRecord(String title){ 15 setTitle(title) ; 16 showRecord=new JButton("显示记录") ; 17 showRecord.addActionListener(this) ; 18 add(showRecord,BorderLayout.NORTH) ; 19 setBounds(200,60,400,250) ; 20 } 21 public void actionPerformed(ActionEvent e){ 22 try 23 { 24 con=DriverManager.getConnection("jdbc:odbc:hello","","") ; 25 sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY) ; 26 rs=sql.executeQuery("SELECT * FROM message") ; 27 rs.last() ; 28 int lastNumber=rs.getRow() ; 29 a=new Object[lastNumber][4] ; 30 int k=0 ; 31 rs.beforeFirst() ; 32 while(rs.next()){ 33 a[k][0]=rs.getString(1) ; 34 a[k][1]=rs.getString(2) ; 35 a[k][2]=rs.getDate(3) ; 36 a[k][3]=rs.getString(4) ; 37 k++ ; 38 } 39 con.close() ; 40 } 41 catch (SQLException ee) 42 { 43 System.out.println(ee) ; 44 } 45 table=new JTable(a,name) ; 46 getContentPane().removeAll() ; 47 add(showRecord,BorderLayout.NORTH) ; 48 add(new JScrollPane(table),BorderLayout.CENTER) ; 49 validate() ; 50 } 51 }
ModifyRecord.java代码
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.sql.*; 5 import javax.swing.border.*; 6 public class ModifyRecord extends JDialog implements ActionListener{ //负责更新记录的类 7 JLabel hintLabel; 8 JTextField inputNumber; 9 Object name[]={"姓名","出生日期","身高"}; 10 Object a[][]=new Object[1][3]; 11 JTable table; 12 JButton enterModify; 13 Connection con; 14 Statement sql; 15 ResultSet rs; 16 String num; 17 ModifyRecord(String s){ 18 setTitle(s); 19 hintLabel=new JLabel("输入学号(回车确认):"); 20 inputNumber=new JTextField(20); 21 table=new JTable(a,name); 22 enterModify=new JButton("更新记录"); 23 setLayout(null); 24 Box baseBox=Box.createHorizontalBox(); 25 baseBox.add(hintLabel); 26 baseBox.add(inputNumber); 27 baseBox.add(new JScrollPane(table)); 28 baseBox.add(enterModify); 29 add(baseBox); 30 baseBox.setBounds(10,40,600,38); 31 inputNumber.addActionListener(this); 32 enterModify.addActionListener(this); 33 setBounds(20,60,700,200); 34 } 35 public void actionPerformed(ActionEvent e){ 36 if(e.getSource()==inputNumber) 37 try{ num=inputNumber.getText().trim(); 38 con=DriverManager.getConnection("jdbc:odbc:hello","",""); 39 sql=con.createStatement(); 40 rs=sql.executeQuery("SELECT * FROM message WHERE number='"+num+"'"); 41 boolean boo=rs.next(); 42 if(boo==false){ 43 JOptionPane.showMessageDialog 44 (this,"学号不存在","提示",JOptionPane.WARNING_MESSAGE); 45 } 46 else{ 47 a[0][0]=rs.getString(2); 48 a[0][1]=rs.getDate(3).toString(); 49 a[0][2]=rs.getString(4); 50 table.repaint(); 51 } 52 con.close(); 53 } 54 catch(SQLException ee){ 55 System.out.println(ee); 56 } 57 if(e.getSource()==enterModify){ 58 try{ con=DriverManager.getConnection("jdbc:odbc:hello","",""); 59 sql=con.createStatement(); 60 sql.executeUpdate 61 ("UPDATE message SET name='"+a[0][0]+ 62 "',birthday='"+a[0][1]+ 63 "',height='"+a[0][2]+"'WHERE number='"+num+"'"); 64 JOptionPane.showMessageDialog 65 (this,"更新成功","成功",JOptionPane.PLAIN_MESSAGE); 66 con.close(); 67 } 68 catch(SQLException ee){ 69 JOptionPane.showMessageDialog 70 (this,"更新失败"+ee,"失败",JOptionPane.ERROR_MESSAGE); 71 } 72 } 73 } 74 }
InsertRecord.java代码
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.sql.*; 5 import javax.swing.border.*; 6 public class InsertRecord extends JDialog implements ActionListener{ //负责插入记录的类 7 JLabel hintLabel; 8 Object name[]={"学号","姓名","出生日期","身高"}; 9 Object a[][]=new Object[1][4]; 10 JTable table; 11 JButton enterInsert; 12 Connection con; 13 Statement sql; 14 ResultSet rs; 15 String num; 16 InsertRecord(String s){ 17 setTitle(s); 18 hintLabel=new JLabel("输入新记录:"); 19 table=new JTable(a,name); 20 enterInsert=new JButton("插入新记录"); 21 setLayout(null); 22 Box baseBox=Box.createHorizontalBox(); 23 baseBox.add(hintLabel); 24 baseBox.add(new JScrollPane(table)); 25 baseBox.add(enterInsert); 26 add(baseBox); 27 baseBox.setBounds(10,40,600,38); 28 enterInsert.addActionListener(this); 29 setBounds(120,160,700,200); 30 } 31 public void actionPerformed(ActionEvent e){ 32 try{ con=DriverManager.getConnection("jdbc:odbc:hello","",""); 33 sql=con.createStatement(); 34 int k=sql.executeUpdate 35 ("INSERT INTO message VALUES('"+ 36 a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')"); 37 if(k==1) 38 JOptionPane.showMessageDialog 39 (this,"插入记录成功","成功",JOptionPane.PLAIN_MESSAGE); 40 con.close(); 41 } 42 catch(SQLException ee){ 43 JOptionPane.showMessageDialog 44 (this,"插入记录失败"+ee,"失败",JOptionPane.ERROR_MESSAGE); 45 } 46 } 47 }