
数据库课程设计报告
题 目: 学生信息管理系统
院系名称: 计算机学院
专业名称:
班 级:
学生姓名:
学号(8位):
指导教师:
设计起止时间:2011年12月19日~2011年12月30日
一. 设计目的
1、掌握DBMS的基本工作原理
2、培养数据库应用系统设计的基本思路和方法
3、培养分析、解决问题的能力
二. 设计内容
利用数据库实现对学生信息的管理
所用数据库:sqlserver 2008
开发语言:java、
开发工具:eclipse
三.概要设计
通过访问数据库实现以下功能:
1、不同用户权限登陆系统
2、用户密码修改
3、学生信息的添加、查询、修改、浏览
1.功能模块图
2.各个模块详细的功能描述。
1、系统用户管理模块
管理员和普通用户通过这个模块登陆系统,不同的用户用不同的用户名登陆,通过选择登陆模式(管理员或者普通用户)登陆系统,完成不同的操作。两种用户都可以对自己的登陆密码进行修改。新用户可进行注册操作,注册后登陆,并进行信息的完善。
2、学生信息管理模块
管理员和普通用户对学生信息由不同的操作。管理员可以对信息进行增加,删除,修改和列表查看,普通用户对自己的信息可进行查询,和修改。
四.详细设计
1.功能函数的调用关系图
查询信息操作
添加信息操作
删除信息操作
总体浏览操作
修改密码操作
查询个人信息
修改个人信息
修改个人密码
2.各功能函数的数据流程图
成功
成功
3.重点设计及编码、
--学生信息表--
create table studentinfo
(
Sid int primary key identity(1,1),
Sname varchar(30) not null,
Ssex varchar(10) check(Ssex='男' or Ssex='女'),
Snumber varchar(8) check(Snumber like '0409[0-9][0-9][0-9][0-9]') not null,
Sprofession varchar(30),
Sclass varchar(20),
Sdate datetime default getDate()
);
--普通用户登陆信息表--
create table userinfo
(
id int primary key identity(1,1),
name varchar(30) not null,
mima varchar(20) not null
);
--管理员登陆信息表--
create table userinfo
(
id int primary key identity(1,1),
name varchar(30) not null,
mima varchar(20) not null
);
--连接数据库—
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.print("加载驱动程序失败");
}
String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS";
try{
Connection con=DriverManager.getConnection(conURL,"sa","majie");
Statement st=con.createStatement();、
}catch(SQLException g)
{
System.out.println("错误代码:"+g.getErrorCode());
System.out.println("错误内容:"+g.getMessage());
}
五.测试数据及运行结果
1.正常测试数据和运行结果
登录
信息查询
修改信息
2.异常测试数据及运行结果
六.调试情况,设计技巧及体会
1.改进方案
本次课程设计实现了系统的基本功能,做的不是很复杂,跟预期想的结果有些差距,数据库表的设计有点简单,下来还要增加表的复杂度,以及触发器的使用。
2.体会
连接数据库还是比较顺利的,因为之前有简单的使用过数据库。总体来说系统算是完成了,基本功能也都实现了,但是在难度上还是不符合要求,还得增加一些更富有实际价值的设计。
七.参考文献
《数据库系统原理与应用》 ——孟彩霞、乔平安、张荣 编著
八.附录:
源代码
package com.student.MS;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Add implements ActionListener
{
JFrame f1;
Container ct;
JPanel jp,jp1,jp2,jp3;
JButton btn1,btn2;
JLabel label;
JTextField tf1,tf2,tf3,tf4,tf5,tf6;
Add(){
f1=new JFrame();
ct=f1.getContentPane();//初始化面板
jp=new JPanel();
jp1=new JPanel(new GridLayout(6,1));
jp2=new JPanel(new GridLayout(6,1));
jp3=new JPanel();
btn1=new JButton("确定");
btn2=new JButton("取消");
label=new JLabel("添加学生信息",SwingConstants.CENTER);
label.setForeground(Color.gray);
tf1=new JTextField(20);
tf2=new JTextField(20);
tf3=new JTextField(20);
tf4=new JTextField(20);
tf5=new JTextField(20);
tf6=new JTextField(20);
tf6.setText("例:2000-02-02");
//文本框加入提示语
tf6.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
tf6.setText("");
}
public void focusLost(FocusEvent e) {}
});
jp.add(label);
ct.add(jp,"North");
jp1.add(new JLabel("姓名",SwingConstants.CENTER));
jp2.add(tf1);
jp1.add(new JLabel("性别",SwingConstants.CENTER));
jp2.add(tf2);
jp1.add(new JLabel("学号",SwingConstants.CENTER));
jp2.add(tf3);
jp1.add(new JLabel("专业",SwingConstants.CENTER));
jp2.add(tf4);
jp1.add(new JLabel("班级",SwingConstants.CENTER));
jp2.add(tf5);
jp1.add(new JLabel("入学时间",SwingConstants.CENTER));
jp2.add(tf6);
jp3.add(btn1);
jp3.add(btn2);
ct.add(jp1,"West");
ct.add(jp2,"East");
ct.add(jp3,"South");
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width;
int y=screen.height;
f1.setSize(350,330);
int xcenter=(x-350)/2;
int ycenter=(y-330)/2;
f1.setLocation(xcenter, ycenter);
f1.setVisible(true);
//f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void insert(){
if(tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals("")||
tf4.getText().equals("")||tf5.getText().equals("")||tf6.getText().equals("")){
JOptionPane.showMessageDialog(f1,"请填写完整信息");
return;
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.print("加载驱动程序失败");
}
String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS";
try{
Connection con=DriverManager.getConnection(conURL,"sa
Statement st=con.createStatement();
String s="insert into studentinfo values('"+tf1.getText()+"','"+tf2.getText()+"','"+
tf3.getText()+"','"+tf4.getText()+"','"+tf5.getText()+"','"+tf6.getText()+"');";
String query="select * from studentinfo where Snumber='"+tf3.getText().trim()+"'";
ResultSet res=st.executeQuery(query);
if(res.next()){
JOptionPane.showMessageDialog(f1,"学号信息已经存在!");
con.close();
tf3.setText("");
}else if(tf6.getText().length()!=10){
JOptionPane.showMessageDialog(f1,"入学时间格式有误!");
con.close();
tf6.setText("");
}
int insert=st.executeUpdate(s);
if(insert==1){
JOptionPane.showMessageDialog(f1,"录入信息成功!");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
}
}catch(SQLException e){
System.out.println("错误代码:"+e.getErrorCode());
System.out.println("错误信息:"+e.getMessage());
}
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
if(cmd.equals("确定")){
JOptionPane.showMessageDialog(null,"与用户表冲突");
insert();
}
else if(cmd.equals("取消")){
f1.hide();
}
}
public Container getCt()
{
return ct;
}
public void setCt(Container ct)
{
this.ct = ct;
}
}
package com.student.MS;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
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.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Delete implements ActionListener
{
JFrame f2;
Container ct;
JPanel jp1,jp2,jp3,jp4;
JLabel label;
JTextField text;
JTable table;//接收数据库中返回的信息
JButton btn1,btn2,btn3;
Object columnName[]={"姓名性别学号专业班级入学时间"};
Object ar[][]=new Object[80][6];
Delete(){
f2=new JFrame();
ct=f2.getContentPane();
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
label=new JLabel();
text=new JTextField();
btn1=new JButton("查询");
btn2=new JButton("删除");
btn3=new JButton("取消");
label=new JLabel("请输入要删除的学生姓名:",SwingConstants.CENTER);
label.setBackground(Color.blue);
table=new JTable(ar,columnName);
JScrollPane scroll=new JScrollPane(table);
text=new JTextField(20);
jp2.add(btn1);
jp2.add(btn2);
jp2.add(btn3);
jp1.add(label);
jp1.add(text);
ct.add(jp1,"North");
jp3.setLayout(new BorderLayout());
jp3.add(new JLabel("学生信息如下"));
jp3.add(scroll);
ct.add(jp2,"South");
ct.add(jp3,"Center");
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width;
int y=screen.height;
f2.setSize(450, 400);
int xcenter=(x-450)/2;
int ycenter=(y-400)/2;
f2.setLocation(xcenter, ycenter);
f2.setVisible(true);
//f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
}
int i=0;
public void show(String s){
while(i>=0)
{
ar[i][0]="";
ar[i][1]="";
ar[i][2]="";
ar[i][3]="";
ar[i][4]="";
ar[i][5]="";
i--;
}
i=0;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.print("加载驱动程序失败");
}
String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS";
try{
Connection con=DriverManager.getConnection(conURL,"sa
Statement st=con.createStatement();
String sql="select * from studentinfo where Sname='"+s+"'";
ResultSet res=st.executeQuery(sql);
/*if(!(res.next())){
JOptionPane.showMessageDialog(f,"所要查询的信息不存在");
} */
while(res.next()){
String sName=res.getString(1);
String sSex=res.getString(2);
String sNumber=res.getString(3);
String sProfession=res.getString(4);
String sClass=res.getString(5);
String sDate=res.getString(6);
ar[i][0]=sName;
ar[i][1]=sSex;
ar[i][2]=sNumber;
ar[i][3]=sProfession;
ar[i][4]=sClass;
ar[i][5]=sDate;
i++;
}
f2.repaint();
con.close();
}catch(SQLException e){
System.out.println("错误代码:"+e.getErrorCode());
System.out.println("错误信息:"+e.getMessage());
}
}
public void delete(int line){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.print("加载驱动程序失败");
}
String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS";
try{
Connection con=DriverManager.getConnection(conURL,"sa
Statement st=con.createStatement();
String name=(String)(ar[line][2]);
String sql="delete from studentinfo where Snumber ='"+name+"'";
int del=st.executeUpdate(sql);
if(del==1){
ar[line][0]="";
ar[line][1]="";
ar[line][2]="";
ar[line][3]="";
ar[line][4]="";
ar[line][5]="";
JOptionPane.showMessageDialog(null,"删除成功!
"信息", JOptionPane.YES_NO_OPTION);
}
con.close();
f2.repaint();
}catch(SQLException e){
System.out.println("错误代码:"+e.getErrorCode());
System.out.println("错误信息:"+e.getMessage());
}
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
if(cmd.equals("查询")){
String qu=text.getText().trim();
show(qu);
}
if(cmd.equals("删除")){
int de=table.getSelectedRow();
if(de==-1){
JOptionPane.showMessageDialog(null,"请选定要删除的行
"错误信息", JOptionPane.YES_NO_OPTION);
}
else{
delete(de);
}
}
if(cmd.equals("取消")){
f2.hide();
}
}
}
package com.student.MS;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
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.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
public class Liulan implements ActionListener
{
JFrame f4;
Container ct;
JPanel jp1,jp2,jp3,jp4;
JLabel label;
JButton btn1,btn2;
JTable table;
Object[][] ar=new Object[80][6];
Object[] columnName={"姓名性别学号专业班级入学时间"};
Liulan(){
f4=new JFrame();
ct=f4.getContentPane();
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
btn1=new JButton("点击浏览");
btn2=new JButton(" 取消 ");
label=new JLabel("学生信息浏览",SwingConstants.CENTER); label.setBackground(Color.blue); table=new JTable(ar, columnName); JScrollPane scroll=new JScrollPane(table); jp1.add(label); jp2.add(btn1); jp2.add(btn2); jp3.add(scroll); ct.add(jp1,"North"); ct.add(jp3,"Center"); ct.add(jp2,"South"); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; int y=screen.height; f4.setSize(500,530); int xcen=(x-500)/2; int ycen=(y-530)/2; f4.setLocation(xcen,ycen); f4.setVisible(true); btn1.addActionListener(this); btn2.addActionListener(this); } int i=0; public void select(){ while(i>=0){ ar[i][0]=""; ar[i][1]=""; ar[i][2]=""; ar[i][3]=""; ar[i][4]=""; ar[i][5]=""; i--; } i=0; try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e){ System.out.print("加载驱动程序失败"); } String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS"; try{ Connection con=DriverManager.getConnection(conURL,"sa Statement st=con.createStatement(); String s="select * from studentinfo"; ResultSet res=st.executeQuery(s); while(res.next()){ String sName=res.getString(1); String sSex=res.getString(2); String sNumber=res.getString(3); String sProfession=res.getString(4); String sClass=res.getString(5); String sDate=res.getString(6); ar[i][0]=sName; ar[i][1]=sSex; ar[i][2]=sNumber; ar[i][3]=sProfession; ar[i][4]=sClass; ar[i][5]=sDate; i++; } f4.repaint(); con.close(); }catch(SQLException e){ System.out.println("错误代码:"+e.getErrorCode()); System.out.println("错误信息:"+e.getMessage()); } } @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent e) { String cmd=e.getActionCommand(); if(cmd.equals("点击浏览")){ select(); }else if(cmd.equals(" 取消 ")){ f4.hide(); } } } package com.student.MS; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.SwingConstants; @SuppressWarnings("serial") class Login extends JFrame implements ActionListener { Container cp=null; JFrame f=null; JButton button1,button2,button3; JTextField t1; JPasswordField t2; JLabel jlable1,jlable2; JRadioButton rabtn1,rabtn2; Color c; JPanel jp1,jp2,jp3; String table=null; Login(){ f=new JFrame("学生信息管理系统"); button1=new JButton("确定"); button2=new JButton("取消"); button3=new JButton("注册"); //button3.setOpaque(false); button3.setBorder(null); button3.setBackground(Color.getColor("#292421")); rabtn1=new JRadioButton("普通用户"); rabtn1.setBackground(Color.getColor("#292421")); rabtn2=new JRadioButton("管理员"); rabtn2.setBackground(Color.getColor("#292421")); ButtonGroup group=new ButtonGroup(); group.add(rabtn1); group.add(rabtn2); cp=f.getContentPane(); jlable1=new JLabel("输入用户名"); jlable2=new JLabel("输入密码"); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); t1=new JTextField(20); t2=new JPasswordField(20); //jp3.add(group); jp1.add(jlable1); jp1.add(t1); jp1.add(jlable2); jp1.add(t2); jp1.add(rabtn1); jp1.add(rabtn2); jp1.add(button3); JLabel JL=new JLabel("欢迎登陆" + " cp.add(JL,"North"); jp2.add(button1); jp2.add(button2); cp.add(jp1,"Center"); cp.add("South",jp2); cp.setBackground(new Color(127, 255, 212 )); jp2.setBackground(new Color(127, 255, 212 )); jp1.setBackground(new Color(127, 255, 212 )); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; int y=screen.height; f.setSize(300,300); int xcenter=(x-300)/2; int ycenter=(y-300)/2; f.setLocation(xcenter,ycenter);/*显示在窗口*/ f.setVisible(true); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //------------------------------------------------------------// /*监听事件*/ button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); rabtn1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { JRadioButton jop=(JRadioButton) e.getSource(); if (jop.isSelected()) table="userinfo"; } }); rabtn2.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { JRadioButton jop=(JRadioButton) e.getSource(); if (jop.isSelected()) table="manager"; } }); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } @SuppressWarnings("deprecation") public void confirm(String tableName){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e){ System.out.print("加载驱动程序失败"); } String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS"; try{ Connection con=DriverManager.getConnection(conURL,"sa Statement st=con.createStatement(); String userName=t1.getText().trim(); String Mima=t2.getText().trim(); String queryMima="select * from "+tableName+" where name='"+userName+"' and mima='"+Mima+"'"; ResultSet res=st.executeQuery(queryMima); if(res.next()){ if(tableName.equals("manager")) new Mstudent(userName); else if(tableName.equals("userinfo")) new Ustudent(userName); f.hide(); con.close(); }else{ JOptionPane.showMessageDialog(null,"该用户不存在提示!", JOptionPane.YES_NO_OPTION); } t1.setText(""); t2.setText(""); }catch(SQLException g) { System.out.println("错误代码:"+g.getErrorCode()); System.out.println("错误内容:"+g.getMessage()); } } ItemListener listen=new ItemListener(){ public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub } }; public void actionPerformed(ActionEvent e) { String cmd=e.getActionCommand(); if(cmd.equals("确定")){ if(table==null){ JOptionPane.showMessageDialog(f,"请选择用户类型"); } confirm(table); } else if(cmd.equals("取消")){ f.dispose(); }else if(cmd.equals("注册")){ new regist(); } } public static void main(String []arg){ new Login(); } } package com.student.MS; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingConstants; import javax.swing.border.TitledBorder; @SuppressWarnings("serial") public class Mstudent extends JFrame implements ActionListener { JButton Query = new JButton("查询信息"); JButton add = new JButton("添加信息"); JButton delete = new JButton("删除信息"); JButton xiugai = new JButton("浏览信息"); JButton liulan = new JButton("修改密码"); JMenuBar mb = new JMenuBar();// 菜单栏 JPanel jp = new JPanel(); Container cp = getContentPane(); String userName; Mstudent() { } Mstudent(String userName) { this.userName = userName; mb.add(Query); mb.add(add); mb.add(delete); mb.add(xiugai); mb.add(liulan); JPanel j=new JPanel(); j.add(mb,"Center"); cp.add(j, "North"); // 设置边框 jp.setBorder(BorderFactory.createTitledBorder(BorderFactory .createLineBorder(Color.blue, 2), null, TitledBorder.CENTER, TitledBorder.TOP)); jp.setLayout(new BorderLayout()); JLabel label1 = new JLabel("欢迎使用学生信息管理系统",SwingConstants.CENTER); jp.add(label1); JScrollPane scrollpane = new JScrollPane(jp); cp.add(scrollpane,"Center"); setTitle("欢迎登陆" + "--" + "你好:" + userName); Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screen = kit.getScreenSize(); int x = screen.width; /* 取得显示器窗口的宽度 */ int y = screen.height; /* 取得显示器窗口的高度 */ setSize(600, 600); int xcenter = (x - 600) / 2; int ycenter = (y - 600) / 2; setLocation(xcenter, ycenter);/* 显示在窗口 */ setVisible(true); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 注册临听器 Query.addActionListener(this); add.addActionListener(this); delete.addActionListener(this); xiugai.addActionListener(this); liulan.addActionListener(this); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("查询信息")) { new QueryWays(); } if (cmd.equals("添加信息")) { new Add(); } if (cmd.equals("删除信息")) { new Delete(); } if (cmd.equals("浏览信息")) { new Liulan(); } if (cmd.equals("修改密码")) { new UpdateCode(); } } public static void main(String[] args){ new Mstudent(""); } } package com.student.MS; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; 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.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class Query implements ActionListener { String sql =null; int a; String info; JFrame f3; // Container cp; JPanel jp1,jp2,jp3,jp4,jp,jpwest; JButton btn1,btn2;//确定,取消 JLabel label,label2;//标签,请输入姓名(label) JTextField tf,tf1,tf2,tf3,tf4,tf5,tf6;//文本框 Query(int a){ this.a=a; f3=new JFrame(); Container cp=f3.getContentPane();//初始化面板,按钮,标签,文本框 jp=new JPanel(); jp1=new JPanel(); jp2=new JPanel(new GridLayout(6,1)); jp3=new JPanel(); jp4=new JPanel(new GridLayout(6,1)); jpwest=new JPanel(); btn1=new JButton("确定"); btn2=new JButton("取消"); label=new JLabel("请输入姓名:" + " label2=new JLabel("请输入学号:" + " label.setForeground(Color.gray); tf=new JTextField(20); //------------------------------------------------ tf1=new JTextField(20); tf2=new JTextField(20); tf3=new JTextField(20); tf4=new JTextField(20); tf5=new JTextField(20); tf6=new JTextField(20); //布局,添加控件 if(a==0){ jp.add(label); jp.add(tf); }else if(a==1){ jp.add(label2); jp.add(tf); } cp.add(jp,"North"); jp4.add(new JLabel("姓名",SwingConstants.CENTER)); jp2.add(tf1); jp4.add(new JLabel("性别",SwingConstants.CENTER)); jp2.add(tf2); jp4.add(new JLabel("学号",SwingConstants.CENTER)); jp2.add(tf3); jp4.add(new JLabel("专业",SwingConstants.CENTER)); jp2.add(tf4); jp4.add(new JLabel("班级",SwingConstants.CENTER)); jp2.add(tf5); jp4.add(new JLabel("入学时间",SwingConstants.CENTER)); jp2.add(tf6); jp3.add(btn1); jp3.add(btn2); cp.add(jp4,"West"); cp.add(jp2,"East"); cp.add(jp3,"South"); cp.add(jp1); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; //显示窗口宽度 int y=screen.height;//窗口高度 f3.setSize(350,330); int xcenter=(x-350)/2; int ycenter=(y-330)/2; f3.setLocation(xcenter,ycenter); f3.setVisible(true); //f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn1.addActionListener(this); btn2.addActionListener(this); /*f3.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } );*/ } public void showRecord(){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e){ System.out.print("加载驱动程序失败"); } String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS"; try{ Connection con=DriverManager.getConnection(conURL,"sa Statement st=con.createStatement(); info=tf.getText().trim(); if(a==0){ sql="select * from studentinfo where Sname ='"+info +"'"; }else if(a==1){ sql="select * from studentinfo where Snumber ='"+info +"'"; } ResultSet res=st.executeQuery(sql); if(res.next()){ String sName=res.getString(1); String sSex=res.getString(2); String sNumber=res.getString(3); String sProfession=res.getString(4); String sClass=res.getString(5); String sDate=res.getString(6); tf1.setText(sName); tf2.setText(sSex); tf3.setText(sNumber); tf4.setText(sProfession); tf5.setText(sClass); tf6.setText(sDate); } else {JOptionPane.showMessageDialog(null,"您输入的学生信息不存在,请重新输入 "输入错误", JOptionPane.YES_NO_OPTION); } con.close(); }catch(SQLException e){ System.out.println("错误代码:"+e.getErrorCode()); System.out.println("错误内容:"+e.getMessage()); } tf1.setEditable(false); tf2.setEditable(false); tf3.setEditable(false); tf4.setEditable(false); tf5.setEditable(false); tf6.setEditable(false); } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e){ String cmd=e.getActionCommand(); if(cmd.equals("确定")){ showRecord(); tf.setText(""); }else if(cmd.equals("取消")){ f3.hide(); } } } package com.student.MS; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class QueryWays implements ActionListener { int a; JFrame f=new JFrame(); Container ct=f.getContentPane(); QueryWays(){ JLabel label=new JLabel("选择查询条件",SwingConstants.CENTER); JButton j1=new JButton("按姓名查询"); JButton j2=new JButton("按学号查询"); JPanel p1=new JPanel(); JPanel p2=new JPanel(); p1.add(label); p2.add(j1); p2.add(j2); ct.add(p1,"North"); ct.add(p2,"Center"); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; int y=screen.height; f.setSize(200,160); int xcen=(x-200)/2; int ycen=(y-160)/2; f.setLocation(xcen,ycen); f.setVisible(true); j1.addActionListener(this); j2.addActionListener(this); } public void actionPerformed(ActionEvent e){ String cmd=e.getActionCommand(); if(cmd.equals("按姓名查询")){ a=0; new Query(a); }else if(cmd.equals("按学号查询")){ a=1; new Query(a); } } } package com.student.MS; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; 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.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class regist implements ActionListener { JFrame f6; Container ct; JPanel jp1,jp2,jp3,jp4; JButton btn1,btn2; JLabel label; JTextField tf1; JPasswordField tf2,tf3; regist(){ f6=new JFrame(); ct=f6.getContentPane(); jp1=new JPanel(new GridLayout(3,1)); jp2=new JPanel(new GridLayout(3,1)); jp3=new JPanel(); jp4=new JPanel(); btn1=new JButton("确定"); btn2=new JButton("取消"); label=new JLabel("注册",SwingConstants.CENTER); label.setForeground(Color.gray); tf1=new JTextField(20); tf2=new JPasswordField(20); tf3=new JPasswordField(20); jp3.add(label); ct.add(jp3,"North"); jp1.add(new JLabel("学号",SwingConstants.CENTER)); jp2.add(tf1); jp1.add(new JLabel("密码",SwingConstants.CENTER)); jp2.add(tf2); jp1.add(new JLabel("确认密码",SwingConstants.CENTER)); jp2.add(tf3); jp4.add(btn1); jp4.add(btn2); ct.add(jp1,"West"); ct.add(jp2,"Center"); ct.add(jp4,"South"); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; int y=screen.height; f6.setSize(300,220); int xcenter=(x-200)/2; int ycenter=(y-160)/2; f6.setLocation(xcenter, ycenter); f6.setVisible(true); //f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn1.addActionListener(this); btn2.addActionListener(this); } @SuppressWarnings("deprecation") public void insert(){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e){ System.out.print("加载驱动程序失败"); } String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS"; try{ Connection con=DriverManager.getConnection(conURL,"sa Statement st=con.createStatement(); String username=tf1.getText().trim(); String Mima=tf2.getText().trim(); String insert="insert into userinfo values('"+username+"','"+Mima+"');"; String query="select * from userinfo where name='"+tf1.getText().trim()+"'"; ResultSet res=st.executeQuery(query); if(res.next()){ JOptionPane.showMessageDialog(f6,"学号信息已经存在!"); con.close(); tf1.setText(""); } int re=st.executeUpdate(insert); if(re==1){ JOptionPane.showMessageDialog(f6,"注册成功!"); tf1.setText(""); tf2.setText(""); tf3.setText(""); } }catch(SQLException ee){ System.out.println("错误代码:"+ee.getErrorCode()); System.out.println("错误信息:"+ee.getMessage()); } } @SuppressWarnings({ "deprecation"}) public void actionPerformed(ActionEvent e){ String cmd=e.getActionCommand(); if(cmd.equals("确定")){ String num=tf1.getText().substring(0,4); if(tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals("")) { JOptionPane.showMessageDialog(null,"请填写用户的所有信息提示", JOptionPane.YES_NO_OPTION); return; } if(tf1.getText().length()<8||tf1.getText().length()>8){ JOptionPane.showMessageDialog(f6,"请输入8位学号"); }else if(!(num.equals("0409"))){ JOptionPane.showMessageDialog(f6,"输入学号格式错误"); tf1.setText(""); } if(tf2.getText().trim().equals(tf3.getText().trim())){ insert(); }else{ JOptionPane.showMessageDialog(f6,"确认密码与新密码不匹配"); } }else if(cmd.equals("取消")){ f6.hide(); } } } package com.student.MS; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; 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.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; @SuppressWarnings("serial") public class UpdateCode extends JFrame implements ActionListener { JFrame f4; Container ct; JPanel jp1,jp2,jp3,jp4; JLabel label,label2,label3; JTextField name; JPasswordField tf1,tf2,tf3; JButton btn1,btn2; String Sname; UpdateCode(){ f4=new JFrame(); ct=f4.getContentPane(); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(new GridLayout(6,1)); jp4=new JPanel(new GridLayout(6,1)); btn1=new JButton("确定"); btn2=new JButton("取消"); label=new JLabel("修改密码",SwingConstants.CENTER); label.setForeground(Color.blue); //label.setFont(new Font("BOLD",Font.BOLD,15)); name=new JTextField(20); tf1=new JPasswordField(20); tf2=new JPasswordField(20); tf3=new JPasswordField(20); jp1.add(btn1); jp1.add(btn2); jp2.add(label); ct.add(jp2,"North"); jp3.add(new JLabel("用户名",SwingConstants.RIGHT)); jp4.add(name); jp3.add(new JLabel("原密码",SwingConstants.RIGHT)); jp4.add(tf1); jp3.add(new JLabel("新密码",SwingConstants.RIGHT)); jp4.add(tf2); jp3.add(new JLabel("确认密码",SwingConstants.RIGHT)); jp4.add(tf3); jp4.add(new JLabel()); ct.add(jp3,"West"); ct.add(jp4,"East"); ct.add(jp1,"South"); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; int y=screen.height; f4.setSize(350,330); int xcen=(x-350)/2; int ycen=(y-330)/2; f4.setLocation(xcen, ycen); f4.setVisible(true); //f4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn1.addActionListener(this); btn2.addActionListener(this); } @SuppressWarnings("deprecation") public void Update(){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e){ System.out.print("加载驱动程序失败"); } String conURL="jdbc:sqlserver://localhost:1433; DatabaseName=SIMS"; try{ Connection con=DriverManager.getConnection(conURL,"sa Statement st=con.createStatement(); String username=name.getText().trim(); String Mima=tf1.getText().trim(); String query="select * from userinfo where name='"+username+"' and mima='"+Mima+"'"; ResultSet res=st.executeQuery(query); if(res.next()){ String newmima=tf2.getText().trim(); String s=null; s="update userinfo set mima='"+newmima+"' where name='"+username +"'"; int updatemima=st.executeUpdate(s); if(updatemima==1){ JOptionPane.showMessageDialog(f4, "修改密码成功"); } con.close(); f4.repaint(); }else{ JOptionPane.showMessageDialog(f4,"该用户不存在"); } name.setText(""); tf1.setText(""); tf2.setText(""); tf3.setText(""); }catch(SQLException e){ System.out.println("错误代码:"+e.getErrorCode()); System.out.println("错误信息:"+e.getMessage()); } } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e){ String cmd=e.getActionCommand(); if(cmd.equals("确定")){ if(name.getText().equals("")||tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals("")) { JOptionPane.showMessageDialog(null,"请填写用户的所有信息提示", JOptionPane.YES_NO_OPTION); return; } if(tf2.getText().trim().equals(tf3.getText().trim())){ Update(); }else{ JOptionPane.showMessageDialog(f4,"确认密码与新密码不匹配"); } }else if(cmd.equals("取消")){ f4.hide(); } } } package com.student.MS; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class UpdateInfo implements ActionListener { JFrame f5; Container ct; JPanel jp, jp1, jp2, jp3; JButton btn1, btn2; JLabel label; JTextField tf1, tf2, tf3, tf4, tf5; String userName; public UpdateInfo(String username) { this.userName = username; f5 = new JFrame(); ct = f5.getContentPane();// 初始化面板 jp = new JPanel(); jp1 = new JPanel(new GridLayout(6, 1)); jp2 = new JPanel(new GridLayout(6, 1)); jp3 = new JPanel(); btn1 = new JButton("确定"); btn2 = new JButton("取消"); label = new JLabel("修改(完善)信息 SwingConstants.CENTER); label.setForeground(Color.gray); tf1 = new JTextField(20); tf2 = new JTextField(20); tf3 = new JTextField(20); tf4 = new JTextField(20); tf5 = new JTextField(20); tf5.setText("例:2000-02-02"); // 文本框加入提示语 tf5.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { tf5.setText(""); } public void focusLost(FocusEvent e) { } }); jp.add(label); ct.add(jp, "North"); jp1.add(new JLabel("姓名", SwingConstants.CENTER)); jp2.add(tf1); jp1.add(new JLabel("性别", SwingConstants.CENTER)); jp2.add(tf2); jp1.add(new JLabel("专业", SwingConstants.CENTER)); jp2.add(tf3); jp1.add(new JLabel("班级", SwingConstants.CENTER)); jp2.add(tf4); jp1.add(new JLabel("入学时间", SwingConstants.CENTER)); jp2.add(tf5); jp3.add(btn1); jp3.add(btn2); ct.add(jp1, "West"); ct.add(jp2, "East"); ct.add(jp3, "South"); Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screen = kit.getScreenSize(); int x = screen.width; int y = screen.height; f5.setSize(350, 330); int xcenter = (x - 350) / 2; int ycenter = (y - 330) / 2; f5.setLocation(xcenter, ycenter); f5.setVisible(true); f5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn1.addActionListener(this); btn2.addActionListener(this); } public void update() { if (tf1.getText().equals("") || tf2.getText().equals("") || tf3.getText().equals("") || tf4.getText().equals("") || tf5.getText().equals("")) { JOptionPane.showMessageDialog(f5, "请填写完整信息"); return; } try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { System.out.print("加载驱动程序失败"); } String conURL = "jdbc:sqlserver://localhost:1433; DatabaseName=SIMS"; try { Connection con = DriverManager.getConnection(conURL, "sa", "majie"); Statement st = con.createStatement(); String query="select * from studentinfo where Snumber='"+userName+"'"; String up = "update studentinfo set Sname='"+tf1.getText()+"'," +"Ssex='" + tf2.getText()+ "',Sprofession='"+ tf3.getText() + "',"+ "Sclass='" + tf4.getText()+ "',Sdate='" + tf5.getText() + "' where Snumber='"+userName+"'"; ResultSet res=st.executeQuery(query); if (tf5.getText().length() != 10) { JOptionPane.showMessageDialog(f5, "修改信息成功"); con.close(); tf5.setText(""); }else if(res.next()){ int update = st.executeUpdate(up); if (update == 1) { JOptionPane.showMessageDialog(f5, "修改信息成功!"); tf1.setText(""); tf2.setText(""); tf3.setText(""); tf4.setText(""); tf5.setText(""); } }else{ String insert="insert into studentinfo values('"+tf1.getText()+"','"+tf2.getText()+"','"+userName+"','"+tf3.getText()+"','"+tf4.getText()+"','"+tf5.getText()+"');"; int ins=st.executeUpdate(insert); if(ins==1){ JOptionPane.showMessageDialog(f5, "完善信息成功"); tf1.setText(""); tf2.setText(""); tf3.setText(""); tf4.setText(""); tf5.setText(""); } } con.close(); } catch (SQLException e) { System.out.println("错误代码:" + e.getErrorCode()); System.out.println("错误信息:" + e.getMessage()); } } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equals("确定")) { update(); } else if(cmd.equals("取消")) { f5.hide(); } } public static void main(String[] args){ new UpdateInfo(""); } } package com.student.MS; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingConstants; import javax.swing.border.TitledBorder; @SuppressWarnings("serial") public class Ustudent extends JFrame implements ActionListener { Container ct; JPanel jp1,jp2,jp3; JMenuBar bar; JButton b1,b2,b3; String userName; Ustudent(){} Ustudent(String userName){ ct=getContentPane(); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); b1=new JButton("查询个人信息"); b2=new JButton("修改(完善)信息"); b3=new JButton("修改个人密码"); bar=new JMenuBar(); this.userName=userName; bar.add(b1); bar.add(b2); bar.add(b3); JPanel j=new JPanel(); j.add(bar,"Center"); ct.add(j,"North"); // 设置边框 jp1.setBorder(BorderFactory.createTitledBorder(BorderFactory .createLineBorder(Color.blue, 2), null, TitledBorder.CENTER, TitledBorder.TOP)); jp1.setLayout(new BorderLayout()); JLabel label1 = new JLabel("欢迎使用学生信息管理系统",SwingConstants.CENTER); jp1.add(label1); JScrollPane scrollpane = new JScrollPane(jp1); ct.add(scrollpane,"Center"); setTitle("欢迎登陆" + "--" + "你好: majie" + userName); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screen=kit.getScreenSize(); int x=screen.width; int y=screen.height; setSize(500,400); int xcen=(x-500)/2; int ycen=(y-400)/2; setLocation(xcen,ycen); setVisible(true); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent e){ String cmd=e.getActionCommand(); if(cmd.equals("查询个人信息")){ new Query(0); }else if(cmd.equals("修改(完善)信息")){ new UpdateInfo(userName); }else if(cmd.equals("修改个人密码")){ new UpdateCode(); } } public static void main(String[] args){ new Ustudent(""); } }
