📄 jctx.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.sql.*;
//名称:学生信息管理系统
/**
*@author 骆俊武
*@version 1.1
*/
//需要建立一个数据库(DBMS不限)
/*属性为id(学号) sex(性别) name(姓名)
nation(民族) deparment(专业)
birthday(生日) grade(分数)(都为字符型)
*/
//然后建立数据源
//数据源名称为jctx
//本人编译器J2SDK1.4.2
public class JCTX
{
public static void main(String []args)
{
new Frm_Main();
}
}
class Frm_Main implements ActionListener
{
//throws IOException {
public JFrame frame;
public Container c;
public JMenuBar menuBar;
public JMenu mainMenu1;
public JMenu mainMenu2;
public JMenu mainMenu3;
public JMenuItem subMenu1[] = new JMenuItem[5];
public JMenuItem subMenu2[] = new JMenuItem[7];
public JMenuItem subMenu3[] = new JMenuItem[2];
JButton toolBarButton[] = new JButton[8];
public JToolBar toolBar;
String strTip[] = {"查询您要找的学生记录...",
"添加学生记录...",
"删除已有的学生记录...",
"修改学生记录...",
"使您修改的学生记录生效...",
"使您添加的学生记录生效...",
"确认删除当前记录...",
"退出本系统..."};
String id = new String();
String name = new String();
String sex = new String();
String nation = new String();
String birthday = new String();
String department = new String();
String grade = new String();
Connection con;
ResultSet rs;
Statement st;
// }
public JLabel idL = new JLabel("学号:");//学号标签
public JLabel nameL = new JLabel("姓名:"); //姓名标签
public JLabel sexL = new JLabel("性别:");//性别标签
public JComboBox comboBoxsexL=new JComboBox();
//public comboBoxsexL.addItem("男");
//public comboBoxsexL.addItem("女");
public JLabel nationL = new JLabel("民族:");//民族标签
public JLabel departmentL = new JLabel("专业:");//专业标签
public JLabel birthdayL = new JLabel("生日:"); //生日标签
public JLabel gradeL = new JLabel("成绩:");//成绩标签
public JTextField idT = new JTextField();
public JTextField nameT = new JTextField();
public JTextField sexT = new JTextField();
public JTextField nationT = new JTextField();
public JTextField departmentT = new JTextField();
public JTextField birthdayT = new JTextField();
public JTextField gradeT = new JTextField();
public Frm_Main()
{
frame = new JFrame("学生信息管理系统");
c = frame.getContentPane();
c.setLayout(null);
menuBar = new JMenuBar();
toolBar = new JToolBar();
toolBar.setFloatable(false);
frame.setJMenuBar(menuBar);
frame.setResizable(false);
mainMenu1 = new JMenu("管理");
String str1[] = {"添加用户",
"删除用户",
"查询用户",
" ",
"退出"};
for(int i=0;i<5;i++)
{
if(i==3)
mainMenu1.addSeparator();
else
{
subMenu1[i] = new JMenuItem(str1[i]);
subMenu1[i].addActionListener(this);
mainMenu1.add(subMenu1[i]);
}
}
menuBar.add(mainMenu1);
mainMenu2 = new JMenu("维护学生信息");
String str2[] = {"查询记录",
"添加记录",
"删除记录",
"修改记录",
"提交修改",
"提交添加",
"确认删除"};
for(int i=0;i<7;i++)
{
subMenu2[i] = new JMenuItem(str2[i]);
subMenu2[i].addActionListener(this);
mainMenu2.add(subMenu2[i]);
}
menuBar.add(mainMenu2);
mainMenu3 = new JMenu("帮助");
String str3[] = {"帮助...","关于..."};
for(int i=0;i<2;i++)
{
subMenu3[i] = new JMenuItem(str3[i]);
subMenu3[i].addActionListener(this);
mainMenu3.add(subMenu3[i]);
}
menuBar.add(mainMenu3);
String strToolBar[] ={"查询",
"添加",
"删除",
"修改",
"提交修改",
"提交添加",
"确认删除",
"关于"};
for(int i=0;i<8;i++)
{
toolBarButton[i] = new JButton(strToolBar[i]);
toolBarButton[i].setToolTipText(strTip[i]);
toolBarButton[i].addActionListener(this);
toolBar.add(toolBarButton[i]);
}
toolBar.setLocation(0,0);
toolBar.setSize(400,30);
c.add(toolBar);
idL.setLocation(35,40);
idL.setSize(40,20);
idL.setFont(new Font("宋体",Font.BOLD,12));
c.add(idL);
idT.setLocation(90,40);
idT.setSize(200,20);
idT.setEnabled(false);
c.add(idT);
nameL.setLocation(35,70);
nameL.setSize(40,20);
c.add(nameL);
nameT.setLocation(90,70);
nameT.setSize(200,20);
c.add(nameT);
sexL.setLocation(35,100);
sexL.setSize(40,20);
c.add(sexL);
sexT.setLocation(90,100);
sexT.setSize(200,20);
c.add(sexT);
nationL.setLocation(35,130);
nationL.setSize(40,20);
c.add(nationL);
nationT.setLocation(90,130);
nationT.setSize(200,20);
c.add(nationT);
departmentL.setLocation(35,160);
departmentL.setSize(40,20);
c.add(departmentL);
departmentT.setLocation(90,160);
departmentT.setSize(200,20);
c.add(departmentT);
birthdayL.setLocation(35,190);
birthdayL.setSize(40,20);
c.add(birthdayL);
birthdayT.setLocation(90,190);
birthdayT.setSize(200,20);
c.add(birthdayT);
gradeT.setLocation(35,220);
gradeT.setSize(200,20);
c.add(gradeT);
JLabel information1 = new JLabel("BUAA");
information1.setFont(new Font("宋体",Font.BOLD,80));
information1.setSize(200,100);
information1.setLocation(30,230);
c.add(information1);
JLabel information3 = new JLabel("—");
information3.setFont(new Font("宋体",Font.BOLD,40));
information3.setSize(200,110);
information3.setLocation(200,220);
c.add(information3);
JLabel information2 = new JLabel("6系");
information2.setFont(new Font("宋体",Font.BOLD,70));
information2.setSize(200,100);
information2.setLocation(250,230);
c.add(information2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400); frame.setLocation(200,200);
frame.setVisible(true); conDB();
}
public void conDB()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"您数据库加载失败了!");
}
try
{
con = DriverManager.getConnection("jdbc:odbc:jctx");
st = con.createStatement();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"提示您数据库连接失败了!");
}
}
public void closeDB()
{
try {
st.close();
con.close();
}
catch(SQLException e )
{
JOptionPane.showMessageDialog(null,"提示您数据库关闭失败了!"); } }
public void actionPerformed(ActionEvent e){
if(e.getSource()==subMenu3[1] ||e.getSource()==toolBarButton[7])
{
JOptionPane.showMessageDialog(null,"本程序由骆俊武编写!");
}
if(e.getSource()==subMenu1[0] || e.getSource()==subMenu1[1] || e.getSource()==subMenu1[2] )
{
JOptionPane.showMessageDialog(null,"本功能不打算实现了,您可以自己编写!");
}
if(e.getSource()==subMenu2[0]||e.getSource()==toolBarButton[0])
{
String idid = JOptionPane.showInputDialog("请输入要查找的学生学号");
if(idid.trim()!="")
{
String strSQL = "select * from Coolboy where id ='" + idid + "'";
try {
rs = st.executeQuery("select * from Coolboy where id ='2004010123'");
rs = st.executeQuery(strSQL);
int count = 0;
while(rs.next())
{
id = rs.getString("id");
name = rs.getString("name");
department = rs.getString("department");
sex = rs.getString("sex");
birthday = rs.getString("birthday");
nation = rs.getString("nation");
grade= rs.getString("grade");
++count;
}
if(count==0)
{
JOptionPane.showMessageDialog(null,"对不起,没有您要查找的学生!");
}
else
{
idT.setText(id);
nameT.setText(name);
sexT.setText(sex);
birthdayT.setText(birthday);
nationT.setText(nation);
departmentT.setText(department);
gradeT.setText(grade);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"抱歉,程序出现异常!");
}
}
}
if(e.getSource()==subMenu1[4])
{
closeDB();
System.exit(0);
}
if(e.getSource()==subMenu2[1] ||
e.getSource()==toolBarButton[1])
{
JOptionPane.showMessageDialog(null,"请输入要添加的学生信息");
idT.setEnabled(true);
idT.setText("");
nameT.setText("");
sexT.setText("");
birthdayT.setText("");
nationT.setText("");
departmentT.setText("");
gradeT.setText("");
}
if(e.getSource()==toolBarButton[5] || e.getSource()==subMenu2[5])
{
if((idT.getText().trim()).equals("") ||
(nameT.getText().trim()).equals("") ||
(sexT.getText().trim()).equals("") ||
(birthdayT.getText().trim()).equals("") ||
(nationT.getText().trim()).equals("") ||
(departmentT.getText().trim()).equals("")||
(gradeT.getText().trim()).equals(""))
{
JOptionPane.showMessageDialog(null,"请输入信息再点击提交添加!");
}
else
{
id = idT.getText();
name = nameT.getText();
sex = sexT.getText();
birthday = birthdayT.getText();
nation = nationT.getText();
department = departmentT.getText();
grade = gradeT.getText();
String strSQL ="insert into Coolboy(id,name,sex,birthday,nation,department) " +
"values('" + id + "','" + name + "','" + sex + "','" + birthday + "','" +
nation + "','" + department + "','" + grade + "')";
try
{
st.executeUpdate(strSQL);
}
catch(Exception exx)
{
JOptionPane.showMessageDialog(null,"数据库中已经存在您要添加的学生的学号!");
idT.setText("");
nameT.setText("");
sexT.setText("");
birthdayT.setText("");
nationT.setText("");
departmentT.setText("");
gradeT.setText("");
return;
}
JOptionPane.showMessageDialog(null,"恭喜您,添加成功了!");
}
}
if(e.getSource()==subMenu2[4] || e.getSource()==toolBarButton[4])
{
if((idT.getText().trim()).equals("") ||
(nameT.getText().trim()).equals("") ||
(sexT.getText().trim()).equals("") ||
(birthdayT.getText().trim()).equals("") ||
(nationT.getText().trim()).equals("") ||
(departmentT.getText().trim()).equals("")||
(gradeT.getText().trim()).equals(""))
{
JOptionPane.showMessageDialog(null,"请输入信息再点击修改!");
return;
}
else
{
id = idT.getText();
name = nameT.getText();
sex = sexT.getText();
birthday = birthdayT.getText();
nation = nationT.getText();
department = departmentT.getText();
grade = gradeT.getText();
String strSQL ="update Coolboy set name='"+name+"'," +
"sex='"+sex+"',birthday='"+birthday+"'," +
"nation='"+nation+"'," +
"department='"+department+
"',"+"grade='"+grade+"where id='"+id+"'";
try
{
st.executeUpdate(strSQL);
}
catch(Exception exx)
{
JOptionPane.showMessageDialog(null,"数据库中已经存在您要修改的学生记录!");
idT.setText("");
nameT.setText("");
sexT.setText("");
birthdayT.setText("");
nationT.setText("");
departmentT.setText("");
gradeT.setText("");
return;
}
JOptionPane.showMessageDialog(null,"恭喜您,修改成功了!");
}
}
if(e.getSource()==subMenu2[3]||
e.getSource()==toolBarButton[3])
{
String idid = JOptionPane.showInputDialog("请输入要修改的学生学号");
if(idid.trim()!="")
{
String strSQL = "select * from Coolboy where id ='" + idid + "'";
try {
rs = st.executeQuery("select * from Coolboy where id ='2004010123'");
rs = st.executeQuery(strSQL);
int count = 0;
while(rs.next())
{
id = rs.getString("id");
name = rs.getString("name");
department = rs.getString("department");
sex = rs.getString("sex");
birthday = rs.getString("birthday");
nation = rs.getString("nation");
grade = rs.getString("grade");
++count;
}
if(count==0)
{
JOptionPane.showMessageDialog(null,"对不起,没有您要修改的学生信息!");
}
else {
idT.setText(id);
nameT.setText(name);
sexT.setText(sex);
birthdayT.setText(birthday);
nationT.setText(nation);
departmentT.setText(department);
gradeT.setText(grade);
idT.setEnabled(false);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"抱歉,程序出现异常!");
}
}
}
if(e.getSource()==subMenu2[2]||e.getSource()==toolBarButton[2])
{
String idDel = JOptionPane.showInputDialog("请输入要删除的学生学号");
if(idDel.trim()!="")
{
String strSQL = "select * from Coolboy where id ='" + idDel + "'";
try {
rs = st.executeQuery(strSQL);
int count = 0;
while(rs.next())
{
id = rs.getString("id");
name = rs.getString("name");
department = rs.getString("department");
sex = rs.getString("sex");
birthday = rs.getString("birthday");
nation = rs.getString("nation");
grade = rs.getString("grade");
++count;
}
if(count==0)
{
JOptionPane.showMessageDialog(null,"对不起,没有您要删除的学生信息!");
}
else
{
idT.setText(id);
nameT.setText(name);
sexT.setText(sex);
birthdayT.setText(birthday);
nationT.setText(nation);
departmentT.setText(department);
gradeT.setText(grade);
idT.setEnabled(false);
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"抱歉,程序出现异常!");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -