课程设计作业
在网上代码的基础上做了一些改动(增加为两个菜单:分别为学生信息管理和学生成绩管理)
下载jdbc驱动:点击打开链接
jdk环境变量配置:点击打开链接
软件的安装就自己百度教程就好。
流程图:
首先在SQLServer中创建三个表:
1.login表:学生登录账号和密码管理
2.stu表:学生信息表
3.grade表:学生成绩表
登录界面:
package StudentManagementSystem;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class LLogin {
public static void main(String[] args) {
new loginFrame();
}
}
class loginFrame extends JFrame implements ActionListener{
Box box1,box2,baseBox;
JLabel userName,userPwd,tubiao;
JTextField nameField;
JPasswordField pwdField;
JButton button;
JTabbedPane choose;
JPanel panel1,panel2;
loginFrame(){
setBackground(Color.orange);
tubiao=new JLabel(new ImageIcon("图片路径")); //图片在原有基础上要调整大小
add(tubiao,BorderLayout.NORTH);
userName=new JLabel("账号",JLabel.CENTER);
userPwd=new JLabel("密码",JLabel.CENTER);
nameField=new JTextField(8);
pwdField=new JPasswordField(8);
panel1=new JPanel();
panel2=new JPanel();
choose=new JTabbedPane();
choose.add("登录界面",panel1);
panel1.setLayout(new GridLayout(2,2));
panel1.add(userName);panel1.add(nameField);
panel1.add(userPwd);panel1.add(pwdField);
add(choose,BorderLayout.CENTER);
button=new JButton("登陆");
add(button,BorderLayout.SOUTH);
button.addActionListener(this);
//小图标
ImageIcon tubiao=new ImageIcon("图片路径");
setIconImage(tubiao.getImage());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(400,150,550,400);
setTitle("陈可的学生信息管理系统");
validate();
}
public void actionPerformed(ActionEvent e){
String name,pwd;
name=nameField.getText();
pwd=pwdField.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
Statement sql;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库的密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
rs=sql.executeQuery("select * from login where name ='"+name+"' and pwd='"+pwd+"'");//对应自己数据库建的表填写
int q=0;
while(rs.next()){
q++;
}
if(q>0){
JOptionPane.showMessageDialog(this, "登陆成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
this.dispose();
new CommFrame();
}
else
JOptionPane.showMessageDialog(this, "账号或者密码错误!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
效果:
登陆成功后 点击确定 登陆界面会消失 出现学生信息管理系统管理界面 。
一个集合所有窗口的卡片式布:所调用的函数和界面的基本呈现。
你可以根据要求自己添加工具菜单栏。
package StudentManagementSystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CommFrame extends JFrame implements ActionListener {
JMenuBar bar;
JMenu menu,menu1;
JMenuItem scanItem,deleteItem,updateItem,insertItem,searchItem;
JMenuItem scanItem1,deleteItem1,updateItem1,insertItem1,searchItem1;
Scan_stu scan; //查看所有学生信息
Delete_stu delete;//删除学生信息
Update_stu update;//更新学生信息
Insert_stu insert;//插入学生信息
Search_stu search;//查找学生信息
Scan_grade scan_grade; //查看所有学生成绩
Delete_grade delete_grade;//删除学生成绩
Update_grade update_grade;//更新学生成绩
Insert_grade insert_grade;//插入学生成绩
Search_grade search_grade;//查找学生成绩
CardLayout card=null;
JPanel pCenter;
CommFrame(){
setLayout(new FlowLayout());
scanItem=new JMenuItem("浏览");
deleteItem=new JMenuItem("删除");
updateItem=new JMenuItem("修改");
insertItem=new JMenuItem("添加");
searchItem=new JMenuItem("查找");
scanItem1=new JMenuItem("浏览");
deleteItem1=new JMenuItem("删除");
updateItem1=new JMenuItem("修改");
insertItem1=new JMenuItem("添加");
searchItem1=new JMenuItem("查找");
bar=new JMenuBar();
menu=new JMenu("信息管理菜单");
menu1=new JMenu("成绩管理菜单");
menu.add(scanItem);
menu.add(deleteItem);
menu.add(updateItem);
menu.add(insertItem);
menu.add(searchItem);
menu1.add(scanItem1);
menu1.add(deleteItem1);
menu1.add(updateItem1);
menu1.add(insertItem1);
menu1.add(searchItem1);
bar.add(menu);
bar.add(menu1);
setJMenuBar(bar);
scanItem.addActionListener(this);
deleteItem.addActionListener(this);
updateItem.addActionListener(this);
insertItem.addActionListener(this);
searchItem.addActionListener(this);
scanItem1.addActionListener(this);
deleteItem1.addActionListener(this);
updateItem1.addActionListener(this);
insertItem1.addActionListener(this);
searchItem1.addActionListener(this);
scan=new Scan_stu();
update=new Update_stu();
delete=new Delete_stu();
insert=new Insert_stu();
search=new Search_stu();
card=new CardLayout();
scan_grade=new Scan_grade();
update_grade=new Update_grade();
delete_grade=new Delete_grade();
insert_grade=new Insert_grade();
search_grade=new Search_grade();
card=new CardLayout();
pCenter=new JPanel();
pCenter.setLayout(card);
pCenter.add("scanItem", scan);
pCenter.add("deleteItem",delete);
pCenter.add("updateItem",update);
pCenter.add("insertItem",insert);
pCenter.add("searchItem",search);
pCenter.add("scanItem1", scan_grade);
pCenter.add("deleteItem1",delete_grade);
pCenter.add("updateItem1",update_grade);
pCenter.add("insertItem1",insert_grade);
pCenter.add("searchItem1",search_grade);
add(pCenter,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(400,150,550,400);
setTitle("学生信息管理系统");
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==scanItem)
card.show(pCenter, "scanItem");
else if(e.getSource()==deleteItem)
card.show(pCenter, "deleteItem");
else if(e.getSource()==updateItem)
card.show(pCenter, "updateItem");
else if(e.getSource()==insertItem)
card.show(pCenter, "insertItem");
else if(e.getSource()==searchItem)
card.show(pCenter, "searchItem");
if(e.getSource()==scanItem1)
card.show(pCenter, "scanItem1");
else if(e.getSource()==deleteItem1)
card.show(pCenter, "deleteItem1");
else if(e.getSource()==updateItem1)
card.show(pCenter, "updateItem1");
else if(e.getSource()==insertItem1)
card.show(pCenter, "insertItem1");
else if(e.getSource()==searchItem1)
card.show(pCenter, "searchItem1");
}
}
效果:
浏览学所有学生信息:Scan_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Scan_stu extends JPanel implements ActionListener{
DefaultTableModel update_table;
JTable table;
Query query;
JButton button;
Object a[][];
String b[];
Scan_stu(){
setLayout(new FlowLayout());
setBackground(Color.orange);
query=new Query();
query.setTableName("stu");
a=query.getRecord();
b=query.getField();
update_table=new DefaultTableModel(a, b);
table=new JTable(update_table);
button=new JButton("更新");
button.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));//使表格出现滑动条
add(scrollPane);
add(button);
}
public void actionPerformed(ActionEvent e){
a=null;b=null;
query=new Query();
query.setTableName("stu");
a=query.getRecord();
b=query.getField();
update_table.setDataVector(a, b);
}
}
浏览所有学生成绩:Scan_grade(其实和上面删除学生信息一样只不过把类名和构造函数名改一下,后面的也一样)
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Scan_grade extends JPanel implements ActionListener{
DefaultTableModel update_table;
JTable table;
Query query;
JButton button;
Object a[][];
String b[];
Scan_grade(){
setLayout(new FlowLayout());
setBackground(Color.orange);
query=new Query();
query.setTableName("grade");
a=query.getRecord();
b=query.getField();
update_table=new DefaultTableModel(a, b);
table=new JTable(update_table);
button=new JButton("更新");
button.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));//使表格出现滑动条
add(scrollPane);
add(button);
}
public void actionPerformed(ActionEvent e){
a=null;b=null;
query=new Query();
query.setTableName("grade");
a=query.getRecord();
b=query.getField();
update_table.setDataVector(a, b);
}
}
Scan_stu和Scan_grade调用的Query查找函数
package StudentManagementSystem;
import java.sql.*;
public class Query {
Object a[][]=null;
String b[]=null;
String tableName="";
int 字段个数;
public Query(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException e){
System.out.println(e);
}
}
public Object[][] getRecord(){//求表格的内容
a=null;
b=null;
Connection con;
Statement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="chenke010919chan";
con=DriverManager.getConnection(url,userName,userPwd);
int 字段个数=getZiDuan();
int n=getAmount();
a=new Object[n][字段个数];
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from "+tableName);
int m=0;
while(rs.next()){
for(int k=1;k<=字段个数;k++){
a[m][k-1]=rs.getString(k);
}
System.out.println();
m++;
}
con.close();
}
catch(SQLException e){
System.out.println("请输入正确的表名"+e);
}
return a;
}
public int getAmount(){//求表内容有多少行
Connection con;
Statement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from "+tableName);
rs.last();
int rows=rs.getRow();
return rows;
}
catch(SQLException exp){
System.out.println(""+exp);
return 0;
}
}
public String[] getField(){//求字段名称
Connection con;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null, null, tableName, null);
int 字段个数=getZiDuan();
b=new String[字段个数];
int k=0;
while(rs1.next()){
b[k]=rs1.getString(4);
k++;
}
con.close();
}
catch(SQLException e){
System.out.println(e);
}
return b;
}
public void setTableName(String s){//初试表名
tableName=s.trim();
}
public int getZiDuan(){//求字段个数
Connection con;
PreparedStatement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null, null, tableName, null);
字段个数=0;
while(rs1.next())
字段个数++;
}
catch(SQLException e){
System.out.println(e);
}
return 字段个数;
}
}
删除学生信息:Delete_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Delete delete;
String tableName;
Delete_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("学号")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 单击删除"));
button=new JButton("删除");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
if(field[mark].getText().toString().equals("")==true)
JOptionPane.showMessageDialog(this, "带*号为必填内容","消息对话框",JOptionPane.WARNING_MESSAGE);
else
{
Delete delete=new Delete();
delete.setTableName("stu");
delete.setField(field);
delete.setA(a);
delete.Execute_Delete(mark);
}
}
}
删除学生成绩:Delete_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Delete delete;
String tableName;
Delete_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("学号")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 单击删除"));
button=new JButton("删除");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
if(field[mark].getText().toString().equals("")==true)
JOptionPane.showMessageDialog(this, "带*号为必填内容","消息对话框",JOptionPane.WARNING_MESSAGE);
else
{
Delete delete=new Delete();
delete.setTableName("grade");
delete.setField(field);
delete.setA(a);
delete.Execute_Delete(mark);
}
}
}
调用的删除函数:Delete
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Delete extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Delete(){
}
public void Execute_Delete(int n){
String SQL="";
Connection con;
Statement sql;
SQL="delete from "+tableName+" where "+a[n]+" ='"+field[n].getText().toString()+"'";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql.executeUpdate(SQL);
con.close();
JOptionPane.showMessageDialog(this, "删除成功","消息对话框",JOptionPane.WARNING_MESSAGE);
for(int i=0;i<field.length;i++)
field[i].setText(null);;
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
修改学生信息函数:Update_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Update_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Update update;
String tableName;
Update_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("学号")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 单击修改"));
button=new JButton("修改");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(a[i].equals("学号")==true&&field[i].getText().toString().equals("")==true){
mark=i;
JOptionPane.showMessageDialog(this, "带*必须填写!!!","消息对话框",JOptionPane.WARNING_MESSAGE);
break;
}
}
if(i==n){
int choose=JOptionPane.showConfirmDialog(this, "请确保你的学号是正确的,否则会更新失败!!!如果学号错误"
+ "请先删除再添加","消息对话框",JOptionPane.WARNING_MESSAGE);
if(choose==JOptionPane.YES_OPTION){
Update update=new Update();
update.setTableName("stu");
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "更新成功!!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
}
}
}
修改学生成绩:Update_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Update_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Update update;
String tableName;
Update_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("学号")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel("单击修改"));
button=new JButton("修改");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(a[i].equals("学号")==true&&field[i].getText().toString().equals("")==true){
mark=i;
JOptionPane.showMessageDialog(this, "带*必须填写!!!","消息对话框",JOptionPane.WARNING_MESSAGE);
break;
}
}
if(i==n){
int choose=JOptionPane.showConfirmDialog(this, "请确保你的学号是正确的,否则会更新失败!!!如果学号错误"
+ "请先删除再添加","消息对话框",JOptionPane.WARNING_MESSAGE);
if(choose==JOptionPane.YES_OPTION){
Update update=new Update();
update.setTableName("grade");
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "更新成功!!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
}
}
}
调用的修改函数:Update
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Update extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Update(){
}
public void Execute_Update(int mark){
String SQL[]=new String [a.length];
Connection con;
Statement sql;
for(int i=0;i<a.length;i++)
{
if(i!=mark)
SQL[i]="update "+tableName+" set "+a[i]+" ='"+field[i].getText().toString()+"' where "+a[mark]+" ='"+field[mark].getText().toString()+"'";
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
for(int i=0;i<field.length;i++)
if(i!=mark&&field[i].getText().toString().equals("")==false)
sql.executeUpdate(SQL[i]);
con.close();
for(int i=0;i<field.length;i++)
field[i].setText(null);
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
添加学生信息函数:Insert_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Insert_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Insert insert;
String tableName;
Insert_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("学号")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 单击添加"));
button=new JButton("添加");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true){
JOptionPane.showMessageDialog(this, "必须全部填写","消息对话框",JOptionPane.WARNING_MESSAGE);
break;
}
if(a[i].equals("学号")==true){
mark=i;
}
}
if(i==n){
Insert insert=new Insert();
insert.setTableName("stu");
insert.setField(field);
insert.setA(a);
insert.Execute_Insert(mark);
}
}
}
添加学生成绩函数:Insert_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Insert_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Insert insert1;
String tableName;
Insert_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("学号")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 单击添加"));
button=new JButton("添加");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true){
JOptionPane.showMessageDialog(this, "必须全部填写","消息对话框",JOptionPane.WARNING_MESSAGE);
break;
}
if(a[i].equals("学号")==true){
mark=i;
}
}
if(i==n){
Insert insert=new Insert();
insert.setTableName("grade");
insert.setField(field);
insert.setA(a);
insert.Execute_Insert(mark);
}
}
}
Insert_stu和Insert_grade调用的添加函数:Insert
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Insert extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
Update update;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Insert(){
}
public void Execute_Insert(int mark){
String SQL;
Connection con;
Statement sql;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
SQL="insert "+tableName+"("+a[mark]+") values ('"+field[mark].getText().toString()+"')";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql.execute(SQL);
con.close();
update =new Update();
update.setTableName(tableName);
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "添加成功!!!","消息对话框",JOptionPane.WARNING_MESSAGE);
for(int i=0;i<field.length;i++)
field[i].setText(null);;
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
查找学生信息函数:Search_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Search_stu extends JPanel implements ActionListener{
Box box[],baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark,m;
Search search;
String tableName;
DefaultTableModel search_table;
Object object[][];
String b[];
Search_stu(){
setLayout(new FlowLayout());
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
n=a.length;
box=new Box [n+1];
field =new JTextField[n];
for(int i=0;i<n;i++){
box[i]=Box.createHorizontalBox();
field[i]=new JTextField(10);
if(a[i].equals("学号")==true){
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
else{
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
}
object =new Object[0][0];
search_table=new DefaultTableModel(object, a);
JTable table=new JTable(search_table);
button=new JButton("查找");
button.addActionListener(this);
box[n-1].add(new JLabel(" 单击查找"));
box[n-1].add(Box.createHorizontalStrut(8));
box[n-1].add(button);
baseBox=Box.createVerticalBox();
for(int i=0;i<n;i++){
baseBox.add(box[i]);
baseBox.add(Box.createVerticalStrut(8));
}
JScrollPane scrollPane=new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));
baseBox.add(scrollPane);
add(baseBox);
table.revalidate();
}
public void actionPerformed(ActionEvent e){
int i,sum=0;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true)
sum++;
}
System.out.println(sum);
if(sum==n)
JOptionPane.showMessageDialog(this, "你未输入任何内容,请重新输入!","消息对话框",JOptionPane.WARNING_MESSAGE);
else{
Search search=new Search();
search.setTableName("stu");
search.setField(field);
search.setA(a);
search.Execute_Search();
object=null;b=null;
query=new Query();
query.setTableName("stu");
object=search.getRecord();
search_table.setDataVector(object, a);
}
}
}
查询学生成绩函数:Search_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Search_grade extends JPanel implements ActionListener{
Box box[],baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark,m;
Search search;
String tableName;
DefaultTableModel search_table;
Object object[][];
String b[];
Search_grade(){
setLayout(new FlowLayout());
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
n=a.length;
box=new Box [n+1];
field =new JTextField[n];
for(int i=0;i<n;i++){
box[i]=Box.createHorizontalBox();
field[i]=new JTextField(10);
if(a[i].equals("学号")==true){
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
else{
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
}
object =new Object[0][0];
search_table=new DefaultTableModel(object, a);
JTable table=new JTable(search_table);
button=new JButton("查找");
button.addActionListener(this);
box[n-1].add(new JLabel(" 单击查找"));
box[n-1].add(Box.createHorizontalStrut(8));
box[n-1].add(button);
baseBox=Box.createVerticalBox();
for(int i=0;i<n;i++){
baseBox.add(box[i]);
baseBox.add(Box.createVerticalStrut(8));
}
JScrollPane scrollPane=new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));
baseBox.add(scrollPane);
add(baseBox);
table.revalidate();
}
public void actionPerformed(ActionEvent e){
int i,sum=0;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true)
sum++;
}
System.out.println(sum);
if(sum==n)
JOptionPane.showMessageDialog(this, "你未输入任何内容,请重新输入!","消息对话框",JOptionPane.WARNING_MESSAGE);
else{
Search search=new Search();
search.setTableName("grade");
search.setField(field);
search.setA(a);
search.Execute_Search();
object=null;b=null;
query=new Query();
query.setTableName("grade");
object=search.getRecord();
search_table.setDataVector(object, a);
}
}
}
Search_stu和Search_grade调用的Search函数:
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Search extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
Object object[][];
String b[];
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Search(){
}
public void Execute_Search(){
String SQL="select * from "+tableName+" where ";
Connection con;
Statement sql;
ResultSet rs;
int sum=0;
for(int i=0;i<a.length;i++)
{
if(field[i].getText().toString().equals("")==false){
if(sum==0)
SQL=SQL+a[i]+" = '"+field[i].getText().toString()+"'";
else
SQL=SQL+" and "+a[i]+" = '"+field[i].getText().toString()+"'";
sum++;
}
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己数据库密码";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery(SQL);
rs.last();
object =new Object[rs.getRow()][a.length];
rs.beforeFirst();
int ncase=0;
while(rs.next()){
for(int i=1;i<=a.length;i++){
object[ncase][i-1]=rs.getString(i);
}
ncase++;
}
con.close();
for(int i=0;i<field.length;i++)
field[i].setText(null);
}
catch(SQLException ex){
System.out.println(ex);
}
}
public Object[][] getRecord(){
return object;
}
}