学习了一个月的javase后完成了一个小项目,教学管理系统。虽然是一个简单的小项目,但在完成期间,学到了许多内容。目前只用到了java基础部分的知识,后面的知识还没有学到,等学完后继续完善该项目。
首先该项目的完成需要连接到数据库,连接数据库就要用到三层架构。这里连接的数据库是sqlserver。
package com.teachsystem.dao; import java.sql.*; public class BaseDao { //创建驱动 private final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //连接url private final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=TeachingSystem"; //登录sqlserver用户名 private final String NAME="sa"; //登录sqlserver密码 private final String PASSWORD="123456"; //创建连接con方法 public Connection getCon(){ Connection con=null; try { //加载驱动 Class.forName(DRIVER); con=DriverManager.getConnection(URL, NAME, PASSWORD); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } //创建关闭方法,这里的关闭顺序con一定要最后关闭,不然在后面操作时会出现问题。 public void getClose(ResultSet rs,PreparedStatement ps,Connection con){ try { if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(con!=null){ con.close(); } }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 数据库连接后就可以进行登陆界面的设计,登陆界面的效果图为
该登陆界面登陆时有三种选择,当选择不同时进入到不同的界面,怎样实现这种效果呢?
这三个单选按钮的名称各不相同,分别为radioButton_1,rdbtnNewRadioButton和radioButton。每个按钮被选择的语法为xxx.isSelected();如radioButton_1.isSelected();进入到不同界面可用if(){}进行条件判断。当登陆成功时会有一个弹出框显示登陆成功。弹出框的语法为:
JOptionPane.showMessageDialog(DengLu.this,"登录成功!");登陆后实现页面的跳转,该登陆界面就会消失。实现此功能的语法为 Mima framemima = new Mima(); framemima.setVisible(true);//这里的true就是下一个窗口的显示 framedl.setVisible(false);//这里的false为该窗口的关闭,在此之前本窗口的申明要设置为全局变量。 若登陆时选择的是管理员的登陆,管理员的界面为该界面的完成,首先用到的是分隔窗口。splitPane
JSplitPane splitPane = new JSplitPane(); splitPane.setOneTouchExpandable(true); splitPane.setBounds(0, 0, 836, 578); contentPane.add(splitPane); //左右两边放不同的容器 splitPane.setRightComponent(panel); splitPane.setLeftComponent(panel_9); splitPane.setDividerLocation(200);//设置分隔条的起始位置 在左边窗口选择不同的按钮时,右边窗口会变化到相应的管理界面。除了在按钮上添加事件外,在右边窗口还用到了卡片布局管理器 CardLayout car=new CardLayout();//设为全局变量panel.setLayout(car);
car.show(panel, "s1");//panel为右边的窗口名,s1为右边窗口的子窗口名。
在右边窗口也有不同的选择,该处用到的是tabbedPane。
表格的数据显示,就要用到创建表格模型
//学生基本信息管理模型 public void getliststu(List<Student> list){ //创建一维数组存储表头 String thead[]={"编号","学号","密码","姓名","年龄","性别","籍贯","专业班级"}; //创建二位数组存储数据 String data[][]=new String[list.size()][8]; //循环将数据存入表中 for (int i = 0; i < data.length; i++) { data[i][0]=list.get(i).getID()+""; data[i][1]=list.get(i).getStudentID(); data[i][2]=list.get(i).getStuPassword(); data[i][3]=list.get(i).getStuName(); data[i][4]=list.get(i).getStuAge()+""; data[i][5]=list.get(i).getStuSex(); data[i][6]=list.get(i).getStuPlace(); data[i][7]=list.get(i).getStuClass(); } //创建一个表格模型对象 DefaultTableModel model1=new DefaultTableModel();//此处应设为全局变量 //将两个数组放入表格模型中 model1.setDataVector(data, thead); table = new JTable(model1);//将表格模型放入表格中 } 当点击表格时,对应的数据会显示在下方的显示信息栏中
public void mouseClicked(MouseEvent e) { //取得被点击的行数 int num=table.getSelectedRow(); //取得被点击行的每列的数据并放入对应的字段中 String ID=table.getValueAt(num, 0).toString(); String StudentID=table.getValueAt(num, 1).toString(); String StuPassword=table.getValueAt(num, 2).toString(); String StuName=table.getValueAt(num, 3).toString(); String StuAge=table.getValueAt(num, 4).toString(); String StuSex=table.getValueAt(num, 5).toString(); String StuPlace=table.getValueAt(num, 6).toString(); String StuClass=table.getValueAt(num, 7).toString(); lblNewLabel_1.setText(ID); textField_1.setText(StudentID); textField_2.setText(StuPassword); textField_3.setText(StuName); textField_4.setText(StuAge); textField_5.setText(StuSex); textField_6.setText(StuPlace); textField_7.setText(StuClass); } 进行分页效果,就要写出数据库的查找语句 String sql="select top 10 * from Student where ID not in(select top (10*(?-1)) ID from Student order by ID asc) order by ID asc"; 在查询一栏中可根据输入的条件进行查询,此时就用到了模糊查询
String sql="select * from Student where "+tj+" like '%"+value+"%'"+"order by ID asc"; 学生或老师登陆成功后会有相应的信息显示代码为 if(rdbtnNewRadioButton.isSelected()){ GongNeng2 gn2=new GongNeng2(); stu=gn2.getSelectstudl(name,password); pri=gn2.getSelectpri(name); gra=gn2.getSelectgra(name); if(stu.getStudentID()!=null&&stu.getStuPassword()!=null){ String StudentID=textField.getText(); String StuPassword=stu.getStuPassword(); String StuName=stu.getStuName(); String StuAge=stu.getStuAge()+""; String StuSex=stu.getStuSex(); String StuPlace=stu.getStuPlace(); String StuClass=stu.getStuClass(); String Prize=pri.getPrize(); String Course=gra.getCourse(); String Grade=gra.getGrade()+""; JOptionPane.showMessageDialog(DengLu.this,"登录成功!"); //下面代码的实现效果就是将信息显示在学生登陆成功后的界面 Student1 framestu1 = new Student1(); framestu1.setVisible(true); framestu1.lblNewLabel_1.setText(StudentID); framestu1.lblNewLabel_2.setText(StudentID); framestu1.label_8.setText(StuPassword); framestu1.label_9.setText(StuName); framestu1.label_10.setText(StuAge); framestu1.label_11.setText(StuSex); framestu1.lblNewLabel_3.setText(StuPlace); framestu1.label_12.setText(StuClass); framestu1.lblNewLabel_4.setText(Prize); framestu1.lblNewLabel_5.setText(Course); framestu1.lblNewLabel_6.setText(Grade); framedl.setVisible(false); }
