📄 例12-9.txt
字号:
Example12_9.java (效果如图12.12(a))
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Example12_9{
public static void main(String args[ ]){
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
System.out.println(""+e);
}
DatabaseWin win=new DatabaseWin();
}
}
class DatabaseWin extends JFrame implements ActionListener{//主窗口
JMenuBar menubar;
JMenu menu;
JMenuItem itemShow,itemUpdate,itemInsert;
ShowRecord showRecord;
ModifyRecord modifyRecord;
InsertRecord insertRecord;
DatabaseWin(){
menubar=new JMenuBar();
menu=new JMenu("操作数据库");
itemShow=new JMenuItem("显示记录");
itemUpdate=new JMenuItem("更新记录");
itemInsert=new JMenuItem("插入记录");
itemShow.addActionListener(this);
itemUpdate.addActionListener(this);
itemInsert.addActionListener(this);
menu.add(itemShow);
menu.add(itemUpdate);
menu.add(itemInsert);
menubar.add(menu);
showRecord=new ShowRecord("显示记录对话框");
modifyRecord=new ModifyRecord("修改记录对话框");
insertRecord=new InsertRecord("插入记录对话框");
setJMenuBar(menubar);
setBounds(100,100,370,250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==itemShow)
showRecord.setVisible(true);
else if(e.getSource()==itemUpdate)
modifyRecord.setVisible(true);
else if(e.getSource()==itemInsert)
insertRecord.setVisible(true);
}
}
ShowRecord.java (效果如图12.12(b))
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ShowRecord extends JDialog implements ActionListener{//负责显示记录的类
JTable table;
Object a[][];
Object name[]={"学号","姓名","出生日期","身高"};
JButton showRecord;
Connection con;
Statement sql;
ResultSet rs;
ShowRecord(String title)
{ setTitle(title);
showRecord=new JButton("显示记录");
showRecord.addActionListener(this);
add(showRecord,BorderLayout.NORTH);
setBounds(200,60,400,250);
}
public void actionPerformed(ActionEvent e){
try{ con=DriverManager.getConnection("jdbc:odbc:hello","","");
sql=con.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM message");
rs.last();
int lastNumber=rs.getRow();
a=new Object[lastNumber][4];
int k=0;
rs.beforeFirst();
while(rs.next()){
a[k][0]=rs.getString(1);
a[k][1]=rs.getString(2);
a[k][2]=rs.getDate(3);
a[k][3]=rs.getString(4);
k++;
}
con.close();
}
catch(SQLException ee){
System.out.println(ee);
}
table=new JTable(a,name);
getContentPane().removeAll();
add(showRecord,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
validate();
}
}
ModifyRecord.java (效果如图12.12(c))
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.border.*;
public class ModifyRecord extends JDialog implements ActionListener{ //负责更新记录的类
JLabel hintLabel;
JTextField inputNumber;
Object name[]={"姓名","出生日期","身高"};
Object a[][]=new Object[1][3];
JTable table;
JButton enterModify;
Connection con;
Statement sql;
ResultSet rs;
String num;
ModifyRecord(String s){
setTitle(s);
hintLabel=new JLabel("输入学号(回车确认):");
inputNumber=new JTextField(20);
table=new JTable(a,name);
enterModify=new JButton("更新记录");
setLayout(null);
Box baseBox=Box.createHorizontalBox();
baseBox.add(hintLabel);
baseBox.add(inputNumber);
baseBox.add(new JScrollPane(table));
baseBox.add(enterModify);
add(baseBox);
baseBox.setBounds(10,40,600,38);
inputNumber.addActionListener(this);
enterModify.addActionListener(this);
setBounds(20,60,700,200);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==inputNumber)
try{ num=inputNumber.getText().trim();
con=DriverManager.getConnection("jdbc:odbc:hello","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM message WHERE number='"+num+"'");
boolean boo=rs.next();
if(boo==false){
JOptionPane.showMessageDialog
(this,"学号不存在","提示",JOptionPane.WARNING_MESSAGE);
}
else{
a[0][0]=rs.getString(2);
a[0][1]=rs.getDate(3).toString(); a[0][2]=rs.getString(4);
table.repaint();
}
con.close();
}
catch(SQLException ee){
System.out.println(ee);
}
if(e.getSource()==enterModify){
try{ con=DriverManager.getConnection("jdbc:odbc:hello","","");
sql=con.createStatement();
sql.executeUpdate
("UPDATE message SET name='"+a[0][0]+
"',birthday='"+a[0][1]+
"',height='"+a[0][2]+"'WHERE number='"+num+"'");
JOptionPane.showMessageDialog
(this,"更新成功","成功",JOptionPane.PLAIN_MESSAGE);
con.close();
}
catch(SQLException ee){
JOptionPane.showMessageDialog
(this,"更新失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);
}
}
}
}
InsertRecord.java (效果如图12.12(c))
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.border.*;
public class InsertRecord extends JDialog implements ActionListener{ //负责插入记录的类
JLabel hintLabel;
Object name[]={"学号","姓名","出生日期","身高"};
Object a[][]=new Object[1][4];
JTable table;
JButton enterInsert;
Connection con;
Statement sql;
ResultSet rs;
String num;
InsertRecord(String s){
setTitle(s);
hintLabel=new JLabel("输入新记录:");
table=new JTable(a,name);
enterInsert=new JButton("插入新记录");
setLayout(null);
Box baseBox=Box.createHorizontalBox();
baseBox.add(hintLabel);
baseBox.add(new JScrollPane(table));
baseBox.add(enterInsert);
add(baseBox);
baseBox.setBounds(10,40,600,38);
enterInsert.addActionListener(this);
setBounds(120,160,700,200);
}
public void actionPerformed(ActionEvent e){
try{ con=DriverManager.getConnection("jdbc:odbc:hello","","");
sql=con.createStatement();
int k=sql.executeUpdate
("INSERT INTO message VALUES('"+
a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')");
if(k==1)
JOptionPane.showMessageDialog
(this,"插入记录成功","成功",JOptionPane.PLAIN_MESSAGE);
con.close();
}
catch(SQLException ee){
JOptionPane.showMessageDialog
(this,"插入记录失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -