只设计了一部分 全部的太多了。会慢慢更新增加。
学生信息管理包括添加,删除,修改,查询,显示全部等
具体结构如图
在SQL Server 2005数据库上实现数据操作。使用纯面向对象的java语言作为开发语言
在sql server 2005新建一个名为Student的数据库,在下面新建一个名为stu的表
再新建一个名为login的表 存贮账号 密码
当然 列名你可以随便写 当然 要有个学号啊。我的修改等等都是根据学号的。
这是登录界面入口
package 学生信息管理系统;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class 学生信息管理系统 {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.green);tubiao=new JLabel(new ImageIcon("image/4.png"));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);choose.add("学生登陆",panel2);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("image/3.png");setIconImage(tubiao.getImage());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);setBounds(400,150,300,250);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="aaascx";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();}elseJOptionPane.showMessageDialog(this, "账号或者密码错误!","消息对话框",JOptionPane.WARNING_MESSAGE);}catch(SQLException exp){System.out.println(exp);}}
}
这是登陆界面:
登陆成功后 点击确定 登陆界面会消失 出现学生信息界面 。我的初始密码是12345 12345
你也可以随便设置。
还有我只设置了教师登陆的界面,学生登陆的界面没有设置。
然后就是一个集合所有窗口的卡片式布局,称为CommFrame
package 学生信息管理系统;
import javax.swing.*;import java.awt.color.*;
import java.awt.*;
import java.awt.event.*;
public class CommFrame extends JFrame implements ActionListener{JMenuBar bar;JMenu menu;JMenuItem scanItem,deleteItem,updateItem,insertItem,searchItem;Scan_stu scan;//查看所有学生信息Delete_stu delete;//删除学生信息Update_stu update;//更新学生信息Insert_stu insert;//插入学生信息Search_stu search;//查找学生信息CardLayout card=null;JPanel pCenter;CommFrame(){setLayout(new FlowLayout());scanItem=new JMenuItem("浏览");deleteItem=new JMenuItem("删除");updateItem=new JMenuItem("修改");insertItem =new JMenuItem("添加");searchItem=new JMenuItem("查找");bar=new JMenuBar();menu=new JMenu("菜单");menu.add(scanItem);menu.add(deleteItem);menu.add(updateItem);menu.add(insertItem);menu.add(searchItem);bar.add(menu);setJMenuBar(bar);scanItem.addActionListener(this);deleteItem.addActionListener(this);updateItem.addActionListener(this);insertItem.addActionListener(this);searchItem.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();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);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");}}
这里是浏览所有学生信息:Scan_stu
package 学生信息管理系统;
import java.awt.*;import javax.swing.JFrame;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.green);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_stu调用的Query查找函数
package 学生信息管理系统;
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="aaascx";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="aaascx";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="aaascx";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="aaascx";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 学生信息管理系统;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.color.*;
import java.sql.*;
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.green);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]));elsebox1.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
package 学生信息管理系统;
import javax.swing.*;import java.sql.*;
import java.awt.*;
import java.awt.event.*;
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="aaascx";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 学生信息管理系统;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.color.*;
import java.sql.*;
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.green);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]));elsebox1.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
package 学生信息管理系统;
import javax.swing.*;import java.sql.*;
import java.awt.*;
import java.awt.event.*;
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="aaascx";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 学生信息管理系统;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.color.*;
import java.sql.*;
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.green);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]));elsebox1.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
package 学生信息管理系统;
import javax.swing.*;import java.sql.*;
import java.awt.*;
import java.awt.event.*;
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="aaascx";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 学生信息管理系统;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.table.DefaultTableModel;import java.awt.color.*;
import java.sql.*;
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.green);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);}}
}
这是查找界面
然后是查找函数:
package 学生信息管理系统;
import javax.swing.*;import java.sql.*;
import java.awt.*;
import java.awt.event.*;
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()+"'";elseSQL=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="aaascx";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;}
}
写的时候没发现 现在看看挺多的。。其实代码不多,我每个删除 更新 添加 查看 查找 之所以又写了一个删除 更新 添加 查找函数 因为我还会往下写的。。在后面更新 会 查找课程信息 成绩信息 什么的直接调用函数就行了。所以不要嫌多。。而且这些函数的内容都是差不多的。
比如写完一个更新操作 把里面的sql语句改改 不就是删除操作吗。。内容看着多 其实都一样。一起加油吧!