📄 manage_student.java
字号:
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.PreparedStatement;
import javax.swing.*;
public class Manage_student extends JFrame implements ActionListener
{
private static final int DEFAULT_WIDTH = 500;
private static final int DEFAULT_HEIFHT = 400;
ResultSet rs;
Connection con;
PreparedStatement ps;
String student_no,student_name,student_sex,student_phone,student_address,student_class,student_age;
int age;
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
JPanel toppanel;
JTextField shuru;
JLabel xuehaowei;
JButton queding;
JPanel butpanel;
JLabel xuehao,xingming,xingbie,nianling,dianhua,dizhi,banhao;
JLabel la1,la2,la3,la4,la5,la6,la7,la8,la9,la0;
JTextField tf_xuehao,tf_xingming,tf_xingbie,tf_nianling,tf_dianhua,tf_dizhi,tf_banhao;
JButton tianjia;
JButton xiugai;
JButton shanchu;
JButton qingkong;
JButton exit;
public Manage_student()
{
super.setLocation(300,80);
super.setSize(DEFAULT_WIDTH,DEFAULT_HEIFHT);
super.setTitle("学生基本信息管理!");
toppanel=new JPanel(); //////////////////////////顶面板....
toppanel.setBackground(Color.WHITE);
xuehaowei=new JLabel("学号为:");
shuru=new JTextField(11);
queding=new JButton("查询");
queding.addActionListener(this);
butpanel=new JPanel(); ///////////////////////////底面板.........
butpanel.setBackground(Color.WHITE);
xuehao=new JLabel("学号");
xingming=new JLabel("姓名");
//mima=new JLabel("密码");
xingbie=new JLabel("性别");
nianling=new JLabel("年龄");
dianhua=new JLabel("电话");
dizhi=new JLabel("地址");
banhao=new JLabel("班号");
la1=new JLabel(" ");
la2=new JLabel(" ");
la3=new JLabel(" ");
la4=new JLabel(" ");
la5=new JLabel(" ");
la6=new JLabel(" ");
la7=new JLabel(" ");
la8=new JLabel(" ");
la9=new JLabel(" ");
la0=new JLabel(" ");
tf_xuehao=new JTextField(7);
tf_xingming=new JTextField(7);
tf_xingbie=new JTextField(7);
tf_nianling=new JTextField(7);
tf_dianhua=new JTextField(7);
tf_dizhi=new JTextField(7);
tf_banhao=new JTextField(7);
tianjia=new JButton("添加");
tianjia.addActionListener(this);
xiugai=new JButton("修改");
xiugai.addActionListener(this);
shanchu=new JButton("删除");
shanchu.addActionListener(this);
qingkong=new JButton("清空");
qingkong.addActionListener(this);
exit=new JButton("返回");
exit.addActionListener(this);
set(0,0,1,1,butpanel,xuehao);set(1,0,1,1,butpanel,tf_xuehao);set(2,0,1,1,butpanel,la1);set(3,0,1,1,butpanel,nianling);set(4,0,1,1,butpanel,tf_nianling);
set(0,1,1,1,butpanel,xingming);set(1,1,1,1,butpanel,tf_xingming);set(2,1,1,1,butpanel,la2);set(3,1,1,1,butpanel,dianhua);set(4,1,1,1,butpanel,tf_dianhua);
set(2,2,1,1,butpanel,la3);set(0,2,1,1,butpanel,dizhi);set(1,2,1,1,butpanel,tf_dizhi);
set(0,3,1,1,butpanel,xingbie);set(1,3,1,1,butpanel,tf_xingbie);set(2,3,1,1,butpanel,la4);set(3,2,1,1,butpanel,banhao);set(4,2,1,1,butpanel,tf_banhao);
set(0,4,1,1,butpanel,la5);set(1,4,1,1,butpanel,la6);set(2,4,1,1,butpanel,la7);set(3,4,1,1,butpanel,la8);set(5,4,1,1,butpanel,la9);
set(0,5,1,1,butpanel,tianjia);set(1,5,1,1,butpanel,xiugai);set(2,5,1,1,butpanel,la0);;set(3,5,1,1,butpanel,shanchu);set(4,5,1,1,butpanel,qingkong);
set(0,0,1,1,toppanel,xuehaowei);
set(1,0,1,1,toppanel,shuru);
set(2,0,1,1,toppanel,queding);
set(3,0,1,1,toppanel,exit);
toppanel.add(butpanel);
add(toppanel,BorderLayout.NORTH);
butpanel.setLayout(gbl);
setLayout(new GridLayout());
String url="jdbc:odbc:StudentScore";
String username="sa";
String password="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,username,password);
System.err.println( "连接成功!");
}
catch(ClassNotFoundException cnfex)
{
System.err.println( "装载JDBC/ODBC驱动程序失败。");
//cnfex.printStackTrace();
System.exit(1);
}
catch(SQLException sqlex)
{
System.err.println("无法连接数据库");
//sqlex.printStackTrace();
System.exit(1);
}
}
public void set(int x,int y,int w,int h,JPanel panel,Component com)
{
gbc.anchor=GridBagConstraints.NORTH;
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=w;
gbc.gridheight=h;
gbl.setConstraints(com, gbc);
panel.add(com);
}
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();
if(obj==queding)
{
try
{
String no=shuru.getText().trim();
if(no.length()==0)
{
JOptionPane.showMessageDialog((Component)null,"请输入学号!","提示信息",JOptionPane.INFORMATION_MESSAGE);
}
else
{
ps=con.prepareStatement("select * from student where student_no=?");
ps.setString(1,no);
rs=ps.executeQuery();
rs.next();
student_no=rs.getString(1).trim();
student_name=rs.getString(2).trim();
student_sex=rs.getString(3).trim();
student_age=""+rs.getInt(4);
student_phone=rs.getString(5).trim();
student_class=rs.getString(6).trim();
student_address=rs.getString(7).trim();
tf_xuehao.setText(student_no);
tf_xingming.setText(student_name);
tf_xingbie.setText(student_sex);
tf_nianling.setText(student_age);
tf_dianhua.setText(student_phone);
tf_banhao.setText(student_class);
tf_dizhi.setText(student_address);
}
}
catch(SQLException sql)
{
sql.getMessage();
//sql.printStackTrace();
JOptionPane.showMessageDialog((Component)null,"该学生不存在!","提示信息",JOptionPane.QUESTION_MESSAGE);
shuru.requestFocus();
}
}
if(obj==tianjia)
{
student_no=tf_xuehao.getText().trim();
student_name=tf_xingming.getText().trim();
student_sex=tf_xingbie.getText().trim();
age=Integer.parseInt(tf_nianling.getText().trim());
student_phone=tf_dianhua.getText().trim();
student_class=tf_banhao.getText().trim();
student_address=tf_dizhi.getText().trim();
if(student_no.length()==0||student_name.length()==0||student_sex.length()==0||age==0||student_no.length()==0||student_phone.length()==0||student_address.length()==0||student_class.length()==0)
{
JOptionPane.showMessageDialog((Component)null,"请添加完整信息!","提示信息",JOptionPane.QUESTION_MESSAGE);
}
else
{
try
{
ps=con.prepareStatement("insert into student values(?,?,?,?,?,?,?)");
ps.setString(1, student_no);
ps.setString(2, student_name);
ps.setString(3, student_sex);
ps.setInt(4, age);
ps.setString(5, student_phone);
ps.setString(6, student_address);
ps.setString(7, student_class);
JOptionPane.showMessageDialog((Component)null,"确定添加吗?","提示信息",JOptionPane.QUESTION_MESSAGE);
tf_xuehao.setText("");
tf_xingming.setText("");
tf_xingbie.setText("");
tf_nianling.setText("");
tf_dianhua.setText("");
tf_dizhi.setText("");
tf_banhao.setText("");
rs=ps.executeQuery();
}
catch(SQLException sql)
{
//sql.printStackTrace();
JOptionPane.showMessageDialog((Component)null,"添加成功!","提示信息",JOptionPane.QUESTION_MESSAGE);
}
}
}
if(obj==xiugai)
{
String no=shuru.getText().trim();
student_no=tf_xuehao.getText().trim();
student_name=tf_xingming.getText().trim();
student_sex=tf_xingbie.getText().trim();
age=Integer.parseInt(tf_nianling.getText().trim());
student_phone=tf_dianhua.getText().trim();
student_address=tf_dizhi.getText().trim();
student_class=tf_banhao.getText().trim();
try
{
ps=con.prepareStatement("update student set student_no=?,student_name=?,student_sex=?,student_age=?,student_phone=?,student_address=?,student_class=? where student_no=?");
ps.setString(1, student_no);
ps.setString(2, student_name);
ps.setString(3,student_sex);
ps.setInt(4,age);
ps.setString(5,student_phone);
ps.setString(6,student_address);
ps.setString(7,student_class);
ps.setString(8,no);
JOptionPane.showMessageDialog((Component)null,"确定修改吗?","提示信息",JOptionPane.QUESTION_MESSAGE);
tf_xuehao.setText("");
tf_xingming.setText("");
tf_xingbie.setText("");
tf_nianling.setText("");
tf_dianhua.setText("");
tf_dizhi.setText("");
tf_banhao.setText("");
JOptionPane.showMessageDialog((Component)null,"修改成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
rs=ps.executeQuery();
rs.next();
}
catch(SQLException sql)
{
//sql.printStackTrace();
//JOptionPane.showMessageDialog((Component)null,sql.getMessage(),"提示信息",JOptionPane.QUESTION_MESSAGE);
}
}
if(obj==shanchu)
{
try
{
String no=shuru.getText().trim();
ps=con.prepareStatement("delete from student where student_no=?");
ps.setString(1,no);
JOptionPane.showMessageDialog((Component)null,"确定删除吗?","提示信息",JOptionPane.QUESTION_MESSAGE);
tf_xuehao.setText("");
tf_nianling.setText("");
tf_xingming.setText("");
tf_dianhua.setText("");
tf_dizhi.setText("");
tf_xingbie.setText("");
tf_banhao.setText("");
rs=ps.executeQuery();
JOptionPane.showMessageDialog((Component)null,"删除成功!","提示信息",JOptionPane.INFORMATION_MESSAGE);
}
catch(SQLException sqlex)
{
sqlex.getMessage();
}
}
if(obj==qingkong)
{
shuru.setText("");
tf_xuehao.setText("");
tf_nianling.setText("");
tf_xingming.setText("");
tf_dianhua.setText("");
tf_dizhi.setText("");
tf_xingbie.setText("");
tf_banhao.setText("");
}
if(obj==exit)
{
this.dispose();
Admin_manage admin=new Admin_manage();
admin.setVisible(true);
}
}
public static void main(String[] args)
{
Manage_student stu=new Manage_student();
stu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stu.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -