package test;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
class Add extends Panel implements ActionListener{
Connection con;
Statement sql;
Button b1,b2;
TextField tf1,tf2,tf3,tf4,tf5,tf6;
Box baseBox,bv1,bv2;
Add(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
Panel p1=new Panel();
Panel p2=new Panel();
tf1=new TextField(16);
tf2=new TextField(16);
tf3=new TextField(16);
tf4=new TextField(16);
tf5=new TextField(16);
tf6=new TextField(16);
b1=new Button("录入");
b1.setBackground(Color.green);
b2=new Button("重置");
b2.setBackground(Color.green);
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
bv1=Box.createVerticalBox();
bv1.add(new Label("学号"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("专业"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("年级"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(tf1);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"South");
add(p2,"Center");
setSize(350,300);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
try{ insert();}
catch(SQLException ee){}
JOptionPane.showMessageDialog(this,"数据已入库!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource()==b2){
tf1.setText(" ");
tf2.setText(" ");
tf3.setText(" ");
tf4.setText(" ");
tf5.setText(" ");
tf6.setText(" ");
}
}
public void insert() throws SQLException{
String s1="'"+tf1.getText().trim()+"'";
String s2="'"+tf2.getText().trim()+"'";
String s3="'"+tf3.getText().trim()+"'";
String s4="'"+tf4.getText().trim()+"'";
String s5="'"+tf5.getText().trim()+"'";
String s6="'"+tf6.getText().trim()+"'";
String temp="INSERT INTO jesse VALUES ("+s1+","+s2+","+s3+","+s4+","+s5+","+s6+")";
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql.executeQuery(temp);
con.close();
}
}
class Query extends Panel implements ActionListener{
Connection con;
Statement sql;
TextField t1,t2,t3,t4,t5,t6;
Button b;
Box baseBox,bv1,bv2;
int flag=0;
Query(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
b=new Button("查询");
b.setBackground(Color.orange);
b.addActionListener(this);
t1=new TextField(8);
t2=new TextField(16);
t3=new TextField(16);
t4=new TextField(16);
t5=new TextField(16);
t6=new TextField(16);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
t6.setEditable(false);
Panel p1=new Panel(),p2=new Panel();
p1.add(new Label("输入要查询的学号"));
p1.add(t1);
p1.add(b);
bv1=Box.createVerticalBox();
bv1.add(new Label("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("专业"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("年级"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"North");
add(p2,"Center");
setSize(300,250);
setBackground(Color.red);
}
public void actionPerformed(ActionEvent e){
flag=0;
try{query();}
catch(SQLException ee){}
}
public void query() throws SQLException{
String num,name,sex,subject,grade,born;
con=DriverManager.getConnection("jdbc:odbc:data","","");
ResultSet rs=sql.executeQuery("SELECT * FROM jesse ");
while(rs.next()){
num=rs.getString("学号");
name=rs.getString("姓名");
sex=rs.getString("性别");
subject=rs.getString("专业");
grade=rs.getString("年级");
born=rs.getString("出生");
if(num.equals(t1.getText().trim())){
t2.setText(name);
t3.setText(sex);
t4.setText(subject);
t5.setText(grade);
t6.setText(born);
flag=1;
break;
}
}
con.close();
if(flag==0){t1.setText("没有该学生");}
}
}
class Update extends Panel implements ActionListener{
Connection con;
Statement sql;
Button b1,b2,b3;
Box baseBox,bv1,bv2;
TextField t1,t2,t3,t4,t5,t6;
Update(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
b1=new Button("开始修改");
b1.setBackground(Color.green);
b2=new Button("录入修改");
b2.setBackground(Color.yellow);
b3=new Button("重置");
b3.setBackground(Color.yellow);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
t1=new TextField(8);
t2=new TextField(16);
t3=new TextField(16);
t4=new TextField(16);
t5=new TextField(16);
t6=new TextField(16);
Panel p1=new Panel(),p2=new Panel(),p3=new Panel();
p1.add(new Label("输入要修改信息的学号"));
p1.add(t1);
p1.add(b1);
bv1=Box.createVerticalBox();
bv1.add(new Label("(新)姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)专业"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)年级"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
p3.add(b2);
p3.add(b3);
add(p1,"North");
add(p2,"Center");
add(p3,"South");
setSize(300,250);
setBackground(Color.cyan);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
try{
String num,name,sex,subject,grade,born;
con=DriverManager.getConnection("jdbc:odbc:data","","");
ResultSet rs=sql.executeQuery("SELECT * FROM jesse ");
while(rs.next()){
num=rs.getString("学号");
name=rs.getString("姓名");
sex=rs.getString("性别");
subject=rs.getString("专业");
grade=rs.getString("年级");
born=rs.getString("出生");
if(num.equals(t1.getText().trim())){
t2.setText(name);
t3.setText(sex);
t4.setText(subject);
t5.setText(grade);
t6.setText(born);
break;
}
}
con.close();
}
catch(SQLException ee){}
}
if(e.getSource()==b2){
try{update();}
catch(SQLException ee){}
}
if(e.getSource()==b3){
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
t6.setText(" ");
}
}
public void update() throws SQLException{
String s1="'"+t1.getText().trim()+"'";
String s2="'"+t2.getText().trim()+"'";
String s3="'"+t3.getText().trim()+"'";
String s4="'"+t4.getText().trim()+"'";
String s5="'"+t5.getText().trim()+"'";
String s6="'"+t6.getText().trim()+"'";
String temp="UPDATE jesse SET 姓名 ="+s2+", 性别="+s3+", 专业="+s4+", 年级="+s5+", 出生="+s6+" WHERE 学号="+s1;
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql.executeQuery(temp);
con.close();
}
}
class Delete extends Panel implements ActionListener{
Connection con;
Statement sql;
TextField t1,t2,t3,t4,t5,t6;
Button b;
Box baseBox,bv1,bv2;
Delete(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
b=new Button("删除");
b.setBackground(Color.cyan);
b.addActionListener(this);
t1=new TextField(8);
t1.addActionListener(this);
t2=new TextField(16);
t3=new TextField(16);
t4=new TextField(16);
t5=new TextField(16);
t6=new TextField(16);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
t6.setEditable(false);
Panel p1=new Panel(),p2=new Panel();
p1.add(new Label("输入要删除的学号"));
p1.add(t1);
p1.add(b);
bv1=Box.createVerticalBox();
bv1.add(new Label("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("专业"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("年级"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"North");
add(p2,"Center");
setSize(300,250);
setBackground(Color.green);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==t1){
try{delete();}
catch(SQLException ee){}
}
else if(e.getSource()==b){
int n=JOptionPane.showConfirmDialog(this,"确定要删除该学号及全部信息吗?","确定",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION){
try{
String s1="'"+t1.getText().trim()+"'";
String temp="DELETE FROM jesse WHERE 学号="+s1;
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql.executeUpdate(temp);
con.close();
}
catch(SQLException ee){}
}
else if(n==JOptionPane.NO_OPTION){}
}
}
public void delete() throws SQLException{
String num,name,sex,subject,grade,born;
con=DriverManager.getConnection("jdbc:odbc:data","","");
ResultSet rs=sql.executeQuery("SELECT * FROM jesse ");
while(rs.next()){
num=rs.getString("学号");
name=rs.getString("姓名");
sex=rs.getString("性别");
subject=rs.getString("专业");
grade=rs.getString("年级");
born=rs.getString("出生");
if(num.equals(t1.getText().trim())){
t2.setText(name);
t3.setText(sex);
t4.setText(subject);
t5.setText(grade);
t6.setText(born);
break;
}
}
con.close();
}
}
public class tyj extends Frame implements ActionListener{
MenuBar bar=null;
Menu menu1,menu2,menu3,menu4,menu5;
MenuItem item1,item2,item3,item4,item5;
Add zengjia;
Query chaxun;
Update gengxin;
Delete shanchu;
tyj(){
super("欢迎进入学生信息管理系统");
zengjia=new Add();
chaxun=new Query();
gengxin=new Update();
shanchu=new Delete();
bar=new MenuBar();
menu1=new Menu("信息录入");
menu2=new Menu("信息查询");
menu3=new Menu("信息更新");
menu4=new Menu("信息删除");
menu5=new Menu("退出系统");
item1=new MenuItem("录入");
item2=new MenuItem("查询");
item3=new MenuItem("更新");
item4=new MenuItem("删除");
item5=new MenuItem("退出");
menu1.add(item1);
menu2.add(item2);
menu3.add(item3);
menu4.add(item4);
menu5.add(item5);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
bar.add(menu4);
bar.add(menu5);
setMenuBar(bar);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
Label label=new Label("欢迎进入学生信息管理系统",Label.CENTER);
String s=" ";
Font f=new Font(s,Font.BOLD,22);
label.setFont(f);
label.setBackground(Color.GREEN);
label.setForeground(Color.BLUE);
add(label,"Center");
setVisible(true);
setSize(320,300);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==item1){
removeAll();
add(zengjia,"Center");
validate();
}
if(e.getSource()==item2){
removeAll();
add(chaxun,"Center");
validate();
}
if(e.getSource()==item3){
removeAll();
add(gengxin,"Center");
validate();
}
if(e.getSource()==item4){
removeAll();
add(shanchu,"Center");
validate();
}
if(e.getSource()==item5){
removeAll();
setBackground(Color.GREEN);
Label label=new Label("欢迎进入学生信息管理系统",Label.CENTER);
String s=" ";
Font f=new Font(s,Font.BOLD,22);
label.setFont(f);
label.setForeground(Color.BLUE);
add(label,"Center");
validate();
}
}
public static void main(String[] args)
{
tyj jesse=new tyj();
jesse.setVisible(true);
jesse.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
温馨提示:内容为网友见解,仅供参考