一个简单的学生成绩管理系统,大概花费了我3天多的时间,从刚开始的设计到最后的实验报告。在完成过程中有以下几个让人头疼的问题:
1、java链接access数据库过程中,sql语句的一些问题。在建数据表的时候一定不要把一些关键字作为表的字段,比如像name、java这样的关键字,这种情况下都会导致sql语句执行失败,而在查找原因的时候又往往找不到关键原因,让人很头疼
2、一些sql语句中,如果有字符串,则必须包含在单引号中,不然会出现错误,而其他类型,则不必
3、java的图形界面的掌握的知识还不够,而且对于java的一些链表还不能熟练使用,以后应该及时把这一部分类容补上
4、对于小项目,没有按照标准的步骤完成,以后一定要学者用软件工程的思想来做项目
现在将改项目的代码粘贴如下:
文件结构:
package manageGUI;
import sql.*;
import user.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;
public class LoginGUI extends JFrame implements ActionListener,ItemListener {
/*输入提示标签*/
JLabel lblName;
JLabel lblPwd;
JLabel lblSelect;
JLabel lblNote;
/*输入文本框*/
JTextField txtUserName;
JPasswordField txtUserPwd;
/*操作按钮*/
JButton jbLogin;
JRadioButton jrbTeacher;
JRadioButton jrbStudent;
JRadioButton jrbManage;
JRadioButton jrbMainTeacher;
ButtonGroup jbtType;
DBAccess db=new DBAccess();
String sql;
String tempUser,tempPwd;
String userType="teacher";
String userNum;
public LoginGUI(String s){
this.setTitle(s);
lblName=new JLabel("用户名");
lblPwd=new JLabel("密码");
lblSelect=new JLabel("请选择您的身份");
lblNote=new JLabel();
txtUserName=new JTextField(15);
txtUserPwd=new JPasswordField(15);
txtUserPwd.setEchoChar('*');
jbLogin=new JButton("登陆");
jrbTeacher=new JRadioButton("教师",true);
jrbStudent=new JRadioButton("学生",false);
jrbManage=new JRadioButton("管理员",false);
jrbMainTeacher=new JRadioButton("班主任",false);
jbLogin.addActionListener(this);
jrbTeacher.addItemListener(this);
jrbStudent.addItemListener(this);
jrbManage.addItemListener(this);
jrbMainTeacher.addItemListener(this);
jbtType=new ButtonGroup();
jbtType.add(jrbStudent);
jbtType.add(jrbMainTeacher);
jbtType.add(jrbManage);
jbtType.add(jrbTeacher);
/*为空间定义外观和显示*/
Container c=getContentPane();
c.setLayout(null);
/*添加标签到容器里*/
c.add(lblName);
c.add(lblPwd);
c.add(lblNote);
c.add(lblSelect);
c.add(txtUserName);
c.add(txtUserPwd);
c.add(jbLogin);
c.add(jrbMainTeacher);
c.add(jrbManage);
c.add(jrbStudent);
c.add(jrbTeacher);
lblName.setBounds(40,40,40,35);
txtUserName.setBounds(85,40,140,35);
lblPwd.setBounds(40,100,40,35);
txtUserPwd.setBounds(85,100,140,35);
lblSelect.setBounds(40,140,100,35);
jrbStudent.setBounds(40,180,60,30);
jrbTeacher.setBounds(110,180,60,30);
jrbManage.setBounds(180,180,90,30);
jrbMainTeacher.setBounds(280,180,90,30);
jbLogin.setBounds(120,250,100,30);
lblNote.setBounds(40,290,300,30);
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent ew){
dispose();
}
});
this.setBounds(300,50,500,450);
this.setVisible(true);
this.setResizable(false);
}
/*个动作按钮的响应*/
public void actionPerformed(java.awt.event.ActionEvent evt){
db.dbconn();
sql="SELECT * FROM "+userType;
db.dbSelect(sql);
try{
while(db.rs.next()){
if(txtUserName.getText().equals(db.rs.getString(2))&&
String.valueOf(txtUserPwd.getText()).equals(db.rs.getString(3))){
if(userType.equals("student")){
int tempIndex=Integer.parseInt(db.rs.getString(1));
lblNote.setText(userType+"登陆成功!"+tempIndex);
lblNote.setVisible(true);
new StudentGUI(tempIndex).setVisible(true);
dispose();
}else if(userType.equals("teacher")){
new TeacherGUI(txtUserName.getText());
dispose();
}else if(userType.equals("mainteacher")){
new MainTeacherGUI(txtUserName.getText());
dispose();
}else if(userType.equals("manage")){
new ManageGUI().setVisible(true);
dispose();
}
// lblNote.setText(userType+"登陆成功!");
// lblNote.setVisible(true);
db.dbclose();
break;
}else{
lblNote.setText(userType+"登陆失败!");
lblNote.setVisible(true);
}
}
}catch(SQLException e){
System.err.println(e.toString());
}
db.dbclose();
}
/*重载选项事件的方法*/
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable() instanceof JRadioButton){
JRadioButton ck=(JRadioButton)(e.getItemSelectable());
if(ck.isSelected()){
if(ck.getText().equals("教师"))
userType="teacher";
else if(ck.getText().equals("学生"))
userType="student";
else if(ck.getText().equals("管理员"))
userType="manage";
else
userType="mainteacher";
}else
userType="";
}
}
}
package manageGUI;
import sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;
public class MainTeacherGUI extends JFrame{
/*定义标签*/
JLabel lblName;
JLabel lblNumber;
JLabel lblSex;
JLabel lblPassword;
JLabel lblNote;
/*定义文本框*/
JTextField txtName;
JTextField txtNumber;
JTextField txtSex;
JTextField txtPassword;
/*定义显示文本域及滚动面板*/
JTextArea areaTxt;
JScrollPane scrollPane;
/*数据库语句定义*/
DBAccess db;
String sql;
String userName;
/*构造函数*/
public MainTeacherGUI(String username){
/*初始化用户*/
this.userName=username;
/*初始化标签*/
lblName=new JLabel("姓名:");
lblNumber=new JLabel("编号:");
lblSex=new JLabel("性别:");
lblPassword=new JLabel("密码:");
lblNote=new JLabel("您所管理班级的学生成绩如下:");
/*初始化文本框*/
txtName=new JTextField(30);
txtNumber=new JTextField(30);
txtSex=new JTextField(30);
txtPassword=new JTextField(30);
/*初始化文本域*/
areaTxt=new JTextArea(15,10);
scrollPane=new JScrollPane(areaTxt);
/*初始化容器*/
areaTxt.setEditable(false);
Container c=getContentPane();
c.setLayout(null);
/*添加组件*/
c.add(lblName);
c.add(lblNote);
c.add(lblNumber);
c.add(lblPassword);
c.add(lblSex);
c.add(txtName);
c.add(txtNumber);
c.add(txtPassword);
c.add(txtSex);
c.add(scrollPane);
/*布局组件*/
lblName.setBounds(40,20,60,30);
txtName.setBounds(100,20,100,30);
lblNumber.setBounds(200,20,60,30);
txtNumber.setBounds(260,20,100,30);
lblSex.setBounds(360,20,60,30);
txtSex.setBounds(420,20,100,30);
lblNote.setBounds(40,60,300,30);
scrollPane.setBounds(40,100,520,400);
/*主窗口初始化*/
this.setBounds(300,50,700,550);
this.setVisible(true);
this.setResizable(false);
/*添加关闭窗口事件*/
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ew){
dispose();
}
});
/*链接数据库,根据用户类型,读出相关信息*/
DBAccess db=new DBAccess();
db.dbconn();
sql="SELECT * FROM student";
db.dbSelect(sql);
try{
while(db.rs.next()){
areaTxt.append("\n 姓名:"+db.rs.getString(2)+"编号:"+db.rs.getString(4)+
"性别:"+db.rs.getString(5)+"英语:"+db.rs.getString(6)+"java:"+
db.rs.getString(7)+"math:"+db.rs.getString(8)+"C++:"+db.rs.getString(9));
}
}catch(SQLException e){
System.err.println(e.toString());
}
db.dbclose();
/*连接数据库,读取用户信息*/
db.dbconn();
sql="SELECT * FROM mainteacher WHERE username ='"+userName+"'";
db.dbSelect(sql);
try{
while(db.rs.next()){
txtName.setText(db.rs.getString(2));
txtName.setEditable(false);
txtNumber.setText(db.rs.getString(4));
txtNumber.setEditable(false);
txtSex.setText(db.rs.getString(5));
txtSex.setEditable(false);
}
}catch(SQLException e){
System.err.println(e.toString());
}
}
}
package manageGUI;
import sql.*;
import user.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
public class ManageGUI extends JFrame implements ActionListener,ItemListener{
/*定义数据库对象*/
DBAccess db;
String sql;
String userType="";
int findflag;
/*定义标签*/
JLabel lblName;
JLabel lblNumber;
JLabel lblSex;
JLabel lblEnglish;
JLabel lblJava;
JLabel lblC;
JLabel lblMath;
JLabel lblPassword;
/*定义文本框*/
JTextField txtName;
JTextField txtNumber;
JTextField txtSex;
JTextField txtEnglish;
JTextField txtJava;
JTextField txtC;
JTextField txtMath;
JTextField txtPassword;
JTextArea areaTxt;
/*定义按钮*/
JButton jbtnCreateUser;
JButton jbtnDeleteUser;
JButton jbtnUpdateUserPassword;
/*定义选择按钮*/
JRadioButton jrbStudent;
JRadioButton jrbTeacher;
JRadioButton jrbManage;
JRadioButton jrbMainTeacher;
/*定义按钮组*/
ButtonGroup btgUserType;
/*定义滚动面板*/
JScrollPane scrollPane;
/*构造函数*/
public ManageGUI(){
/*初始化窗口标题*/
this.setTitle("管理员操作界面");
/*初始化标签*/
lblName=new JLabel("姓名:");
lblNumber=new JLabel("编号:");
lblSex=new JLabel("性别:");
lblEnglish=new JLabel("英语:");
lblC=new JLabel("C:");
lblMath=new JLabel("数学:");
lblJava=new JLabel("java:");
lblPassword=new JLabel("密码:");
/*初始化文本框*/
txtName=new JTextField(30);
txtNumber=new JTextField(30);
txtSex=new JTextField(30);
txtEnglish=new JTextField(30);
txtJava=new JTextField(30);
txtC=new JTextField(30);
txtMath=new JTextField(30);
txtPassword=new JTextField(30);
/*初始化文本域*/
areaTxt=new JTextArea(10,10);
/*初始化按钮*/
jbtnCreateUser=new JButton("创建用户");
jbtnDeleteUser=new JButton("删除用户");
jbtnUpdateUserPassword=new JButton("更改密码");
/*注册按钮事件监听器*/
jbtnCreateUser.addActionListener(this);
jbtnDeleteUser.addActionListener(this);
jbtnUpdateUserPassword.addActionListener(this);
/*初始化选择按钮*/
jrbStudent=new JRadioButton("学生",false);
jrbTeacher=new JRadioButton("老师",false);
jrbManage=new JRadioButton("管理员",false);
jrbMainTeacher=new JRadioButton("班主任",false);
btgUserType=new ButtonGroup();
/*加入按钮组,选择按钮形成互斥*/
btgUserType.add(jrbMainTeacher);
btgUserType.add(jrbManage);
btgUserType.add(jrbTeacher);
btgUserType.add(jrbStudent);
/*注册选择俺就事件监听器*/
jrbStudent.addItemListener(this);
jrbTeacher.addItemListener(this);
jrbManage.addItemListener(this);
jrbMainTeacher.addItemListener(this);
/*初始化滚动面板*/
scrollPane=new JScrollPane(areaTxt);
/*初始化容器*/
Container c=getContentPane();
c.setLayout(null);
/*将组建加入容器*/
c.add(lblC);
c.add(lblEnglish);
c.add(lblJava);
c.add(lblMath);
c.add(lblName);
c.add(lblNumber);
c.add(lblSex);
c.add(lblPassword);
c.add(txtC);
c.add(txtEnglish);
c.add(txtJava);
c.add(txtMath);
c.add(txtName);
c.add(txtNumber);
c.add(txtPassword);
c.add(txtSex);
c.add(jbtnCreateUser);
c.add(jbtnDeleteUser);
c.add(jbtnUpdateUserPassword);
c.add(jrbMainTeacher);
c.add(jrbManage);
c.add(jrbStudent);
c.add(jrbTeacher);
c.add(scrollPane);
/*布局组件*/
lblName.setBounds(40,20,60,30);
txtName.setBounds(100,20,60,30);
lblNumber.setBounds(160,20,60,30);
txtNumber.setBounds(220,20,60,30);
lblSex.setBounds(280,20,60,30);
txtSex.setBounds(340,20,60,30);
lblPassword.setBounds(400,20,60,30);
txtPassword.setBounds(460,20,60,30);
lblEnglish.setBounds(40,60,60,30);
txtEnglish.setBounds(100,60,60,30);
lblC.setBounds(160,60,60,30);
txtC.setBounds(220,60,60,30);
lblMath.setBounds(280,60,60,30);
txtMath.setBounds(340,60,60,30);
lblJava.setBounds(400,60,60,30);
txtJava.setBounds(460,60,60,30);
jrbMainTeacher.setBounds(40,100,120,40);
jrbTeacher.setBounds(170,100,120,40);
jrbStudent.setBounds(300,100,120,40);
jrbManage.setBounds(430,100,120,40);
areaTxt.setEditable(false);
areaTxt.setVisible(true);
scrollPane.setBounds(40,150,500,300);
jbtnCreateUser.setBounds(550,150,120,30);
jbtnDeleteUser.setBounds(550,190,120,30);
jbtnUpdateUserPassword.setBounds(550,230,120,30);
/*设置窗口大小及位置*/
this.setBounds(300,50,700,550);
this.setVisible(true);
this.setResizable(false);
/*添加窗口关闭事件*/
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ew){
dispose();
}
});
db=new DBAccess();
}
/*重载动作事件,让各按钮具备其功能*/
public void actionPerformed(ActionEvent evt){
if(evt.getActionCommand().equals("创建用户")){//单击创建用户按钮时
if(userType.equals("teacher")){ //判断用户身份为teacher
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")||
txtSex.getText().trim().equals("")||txtNumber.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
areaTxt.append(db.notes);
sql="INSERT INTO "+userType+"(username,userpwd,usersex,usernum)" +"VALUES('"+
txtName.getText()+"','"+txtPassword.getText()+"','"+txtSex.getText()+"','"+
txtNumber.getText()+"')";
db.dbconn();
db.dbInsert(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 插入信息如下:");
areaTxt.append("\n "+sql);
areaTxt.append("\n 姓名:"+txtName.getText()+"性别:"+txtSex.getText()+"编号:"+txtNumber.getText()+
"密码:"+txtPassword.getText());
}else
areaTxt.append(db.notes);
}
}else if(userType.equals("student")){//判断用户身份为student
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")||
txtSex.getText().trim().equals("")||txtNumber.getText().trim().equals("")||
txtEnglish.getText().trim().equals("")||txtJava.getText().trim().equals("")||
txtC.getText().trim().equals("")||txtMath.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="INSERT INTO "+userType+"(username,userpwd,usernum,ssex,english,Java,Math,c)"+
"VALUES('"+txtName.getText()+"','"+txtPassword.getText()+"','"+txtNumber.getText()+
"','"+txtSex.getText()+"','"+txtEnglish.getText()+"','"+txtJava.getText()+"','"+
txtMath.getText()+"','"+txtC.getText()+"')";
db.dbconn();
db.dbInsert(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 插入信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+"性别:"+txtSex.getText()+"编号:"+txtNumber.getText()+
"密码:"+txtPassword.getText()+"英语:"+txtEnglish.getText()+"java:"+txtJava.getText()+
"数学:"+txtMath.getText()+"c++:"+txtC.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("mainteacher")){//判断用户身份为mainteacher
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")||
txtSex.getText().trim().equals("")||txtNumber.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="INSERT INTO "+userType+"(username,userpwd,usernum,usersex)"+"VALUES('"+txtName.getText()+
"','"+txtPassword.getText()+"','"+txtNumber.getText()+"','"+txtSex.getText()+"')";
db.dbconn();
db.dbInsert(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 插入信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+"性别:"+txtSex.getText()+"编号:"+txtNumber.getText()+
"密码:"+txtPassword.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("manage")){//判断用户身份为manage
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")||
txtSex.getText().trim().equals("")||txtNumber.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="INSERT INTO "+userType+"(username,userpwd,usernum,usersex)"+"VALUES('"+txtName.getText()+
"','"+txtPassword.getText()+"','"+txtNumber.getText()+"','"+txtSex.getText()+"')";
db.dbconn();
db.dbInsert(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 插入信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+"性别:"+txtSex.getText()+"编号:"+txtNumber.getText()+
"密码:"+txtPassword.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else{//如果没有选择用户身份,则提示
areaTxt.append("\n 请选择身!");
}
}else if(evt.getActionCommand().equals("删除用户")){//单击删除用户按钮时
if(userType.equals("teacher")){//判断当前用户身份为teacher
if(txtName.getText().trim().equals("")){
areaTxt.append("\n 用户名不能为空!");
}else{
sql="DELETE FROM teacher WHERE username='"+txtName.getText()+"'";
db.dbconn();
db.dbDelete(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("manage")){//判断当前用户身份为manage
if(txtName.getText().trim().equals("")){
areaTxt.append("\n 用户名不能为空!");
}else{
sql="DELETE FROM manage WHERE username='"+txtName.getText()+"'";
db.dbconn();
db.dbDelete(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("student")){//判断当前用户身份为student
if(txtName.getText().trim().equals("")){
areaTxt.append("\n 用户名不能为空!");
}else{
sql="DELETE FROM student WHERE username='"+txtName.getText()+"'";
db.dbconn();
db.dbDelete(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("mainteacher")){//判断当前用户身份为mainteacher
if(txtName.getText().trim().equals("")){
areaTxt.append("\n 用户名不能为空!");
}else{
sql="DELETE FROM mainteacher WHERE username='"+txtName.getText()+"'";
db.dbconn();
db.dbDelete(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else{//如果当前用户没有选择身份
areaTxt.append("\n 请选择您的当前身份!");
}
}else if(evt.getActionCommand().equals("更改密码")){//单击更改密码时
if(userType.equals("teacher")){//判断当前用户身份为teacher
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="UPDATE teacher SET userpwd='"+txtPassword.getText()+"' WHERE username='"+
txtName.getText()+"'";
db.dbconn();
db.dbUpdate(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 执行信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+" 更改后的密码:"+txtPassword.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("student")){//判断当前用户身份为student
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="UPDATE student SET userpwd='"+txtPassword.getText()+"' WHERE username='"+
txtName.getText()+"'";
db.dbconn();
db.dbUpdate(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 执行信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+" 更改后的密码:"+txtPassword.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("manage")){//判断当前用户身份为manage
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="UPDATE manage SET userpwd='"+txtPassword.getText()+"' WHERE username='"+
txtName.getText()+"'";
db.dbconn();
db.dbUpdate(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 执行信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+" 更改后的密码:"+txtPassword.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else if(userType.equals("mainteacher")){//判断当前用户身份为mainteacher
if(txtName.getText().trim().equals("")||txtPassword.getText().trim().equals("")){
areaTxt.append("\n 输入框不能为空!");
}else{
sql="UPDATE mainteacher SET userpwd='"+txtPassword.getText()+"' WHERE username='"+
txtName.getText()+"'";
db.dbconn();
db.dbUpdate(sql);
db.dbclose();
if(db.sqlflag==1){
areaTxt.append(db.notes);
areaTxt.append("\n 执行信息如下:");
areaTxt.append("\n 姓名:"+txtName.getText()+" 更改后的密码:"+txtPassword.getText());
}else{
areaTxt.append(db.notes);
areaTxt.append("\n"+sql);
}
}
}else{
areaTxt.append("\n 请选择您当前的身份!");
}
}else{
}
}
/*重载选择按钮事件*/
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable() instanceof JRadioButton){
JRadioButton ck=(JRadioButton)(e.getItemSelectable());
if(ck.isSelected()){
if(ck.getText().equals("老师")){
userType="teacher";
areaTxt.append("\n 您当前的身份是:"+userType);
reset();
txtEnglish.setEditable(false);
txtC.setEditable(false);
txtJava.setEditable(false);
txtMath.setEditable(false);
}
if(ck.getText().equals("学生")){
userType="student";
reset();
txtEnglish.setEditable(true);
txtC.setEditable(true);
txtJava.setEditable(true);
txtMath.setEditable(true);
areaTxt.append("\n 您当前的身份是:"+userType);
}
if(ck.getText().equals("管理员")){
userType="manage";
reset();
areaTxt.append("\n 您当前的身份是:"+userType);
txtEnglish.setEditable(false);
txtC.setEditable(false);
txtJava.setEditable(false);
txtMath.setEditable(false);
}
if(ck.getText().equals("班主任")){
userType="mainteacher";
reset();
areaTxt.append("\n 您当前的身份是:"+userType);
txtEnglish.setEditable(false);
txtC.setEditable(false);
txtJava.setEditable(false);
txtMath.setEditable(false);
}
}
}else
userType="";
}
/*清除文本框文本函数*/
public void reset(){
txtName.setText("");
txtNumber.setText("");
txtSex.setText("");
txtPassword.setText("");
txtC.setText("");
txtJava.setText("");
txtMath.setText("");
txtEnglish.setText("");
}
}
package manageGUI;
import sql.*;
import user.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;
public class StudentGUI extends JFrame{
/*定义标签*/
JLabel lblName;
JLabel lblNumber;
JLabel lblSex;
JLabel lblNote;
/*定义文本框*/
JTextField txtName;
JTextField txtNumber;
JTextField txtSex;
/*定义表格及滚动面板*/
JScrollPane scrollPane;
JTable jTable;
DefaultTableModel dModel;
int StuNum;
String[] st;
/*定义数据库对象*/
DBAccess db;
String sql;
int findflag;
/*构造函数*/
public StudentGUI(int StuNum){
String[] title={"姓名","学号","性别","english","java","c++","math"};
String[][] data0=new String[0][0];//表格数据源,初始化为0
dModel=new DefaultTableModel(data0,title);
jTable=new JTable(dModel);//用默认的数据模型,新建一张表对象
jTable.setAutoscrolls(true);//支持自动产生滚动条
jTable.setGridColor(Color.blue);//设置表格线条颜色
jTable.setRowHeight(25);
JScrollPane jstb=new JScrollPane(jTable);//将表格放到滚动面板中
/*链接数据库*/
this.StuNum=StuNum;
db=new DBAccess();
db.dbconn();
sql="SELECT * FROM student WHERE ID="+StuNum;
display(sql);
/*设置窗口标题*/
this.setTitle("学生登陆界面");
/*初始化标签*/
lblName=new JLabel("姓名:");
lblNumber=new JLabel("学号:");
lblSex=new JLabel("性别:");
lblNote=new JLabel("您的信息如下:");
/*初始化文本框*/
txtName=new JTextField(30);
txtNumber=new JTextField(30);
txtSex=new JTextField(30);
/*初始化容器*/
Container c=getContentPane();
c.setLayout(null);
/*向容器添加组件*/
c.add(lblName);
c.add(lblNumber);
c.add(lblSex);
c.add(txtName);
c.add(txtNumber);
c.add(txtSex);
c.add(lblNote);
c.add(jstb);
/*文本框不可编辑*/
txtName.setEditable(false);
txtNumber.setEditable(false);
txtSex.setEditable(false);
/*布局组件*/
lblName.setBounds(40,40,60,30);
txtName.setBounds(100,40,60,30);
lblNumber.setBounds(180,40,60,30);
txtNumber.setBounds(250,40,60,30);
lblSex.setBounds(320,40,60,30);
txtSex.setBounds(390,40,60,30);
lblNote.setBounds(40,100,300,30);
jstb.setBounds(40,140,390,300);
/*添加窗口关闭功能事件*/
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent ew){
dispose();
}
});
/*主窗口位置及大小设置*/
this.setBounds(300,50,650,550);
this.setVisible(true);
this.setResizable(false);
}
/*数据读取函数*/
public void display(String sql1){
db.dbconn();
db.dbSelect(sql1);
try{
while(db.rs.next()){
findflag=1;//标识着查到了记录
Student st=new Student();
st.setStuName(db.rs.getString(2));
st.setStuNum(db.rs.getString(3));
st.setStuSex(db.rs.getString(4));
Score sc=new Score();
sc.setC(db.rs.getInt(5));
sc.setEnglish(db.rs.getInt(6));
sc.setJava(db.rs.getInt(7));
sc.setMath(db.rs.getInt(8));
inputData(sc,st);
}
}catch(SQLException e){
System.err.println(e.toString());
}
db.dbclose();
}
/*将数据添加到数据源*/
public void inputData(Score sc,Student st){
//Score sc=new Score();
Vector data=new Vector();
data.addElement(st.getStuName());
data.addElement(st.getStuSex());
data.addElement(st.getStuNum());
data.addElement(Double.valueOf(sc.getC()).toString());
data.addElement(Double.valueOf(sc.getEnglish()).toString());
data.addElement(Double.valueOf(sc.getJava()).toString());
data.addElement(Double.valueOf(sc.getMath()).toString());
dModel.addRow(data);
}
}
package manageGUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;
import sql.*;
public class TeacherGUI extends JFrame implements ItemListener,ActionListener{
/*定义标签*/
JLabel lblName;
JLabel lblNumber;
JLabel lblSex;
JLabel lblPassword;
JLabel lblNote;
JLabel lblNote2;
/*定义文本框*/
JTextField txtName;
JTextField txtNumber;
JTextField txtSex;
JTextField txtPassword;
/*定义按钮*/
JButton btnSelect;
/*定义选择按钮*/
JRadioButton jrbEnglish;
JRadioButton jrbMath;
JRadioButton jrbC;
JRadioButton jrbJava;
ButtonGroup btgCorse;
/*定义文本域及滚动面板*/
JTextArea areaTxt;
JScrollPane scrollPane;
/*数据库对象定义*/
DBAccess db;
String sql;
String userName;
String selectType;
/*构造函数*/
public TeacherGUI(String username){
/*初始化窗口标题*/
this.userName=username;
/*初始化标签*/
lblName=new JLabel("姓名:");
lblNumber=new JLabel("编号:");
lblSex=new JLabel("性别:");
lblPassword=new JLabel("密码:");
lblNote=new JLabel("请选择要查看的科目:");
lblNote2=new JLabel("您要查找的信息如下:");
/*初始化文本框*/
txtName=new JTextField(60);
txtPassword=new JTextField(60);
txtSex=new JTextField(60);
txtNumber=new JTextField(60);
/*设置文本框编辑状态*/
txtName.setEditable(false);
txtPassword.setEditable(false);
txtSex.setEditable(false);
txtNumber.setEditable(false);
/*初始化按钮*/
btnSelect=new JButton("查询");
/*初始化选择按钮*/
jrbEnglish=new JRadioButton("英语",false);
jrbJava=new JRadioButton("JAVA",false);
jrbMath=new JRadioButton("数学",false);
jrbC=new JRadioButton("c++",false);
btgCorse=new ButtonGroup();
btgCorse.add(jrbC);
btgCorse.add(jrbEnglish);
btgCorse.add(jrbJava);
btgCorse.add(jrbMath);
/*初始化文本域*/
areaTxt=new JTextArea(10,10);
scrollPane=new JScrollPane(areaTxt);
/*选择按钮注册监听事件*/
jrbEnglish.addItemListener(this);
jrbC.addItemListener(this);
jrbJava.addItemListener(this);
jrbMath.addItemListener(this);
/*注册监听事件*/
btnSelect.addActionListener(this);
/*初始化容器*/
Container c=getContentPane();
c.setLayout(null);
/*将组建加入容器*/
c.add(lblNumber);
c.add(lblName);
c.add(lblSex);
c.add(lblNote);
c.add(lblNote2);
c.add(lblPassword);
c.add(txtName);
c.add(txtNumber);
c.add(txtPassword);
c.add(txtSex);
c.add(btnSelect);
c.add(jrbC);
c.add(jrbEnglish);
c.add(jrbJava);
c.add(jrbMath);
c.add(scrollPane);
/*布局组建*/
lblName.setBounds(40,20,50,30);
txtName.setBounds(90,20,100,30);
lblNumber.setBounds(190,20,50,30);
txtNumber.setBounds(240,20,100,30);
lblSex.setBounds(340,20,50,30);
txtSex.setBounds(390,20,100,30);
lblPassword.setBounds(490,20,50,30);
txtPassword.setBounds(540,20,100,30);
lblNote.setBounds(40,60,200,30);
jrbEnglish.setBounds(240,60,60,30);
jrbJava.setBounds(300,60,60,30);
jrbC.setBounds(360,60,60,30);
jrbMath.setBounds(420,60,60,30);
btnSelect.setBounds(480,60,60,30);
scrollPane.setBounds(40,100,600,300);
/*设置窗口大小及位置*/
this.setBounds(300,50,700,650);
this.setVisible(true);
this.setResizable(false);
/*添加窗口关闭功能事件*/
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ew){
dispose();
}
});
/*链接数据库*/
DBAccess db=new DBAccess();
db.dbconn();
sql="SELECT * FROM teacher WHERE username='"+userName+"'";
db.dbSelect(sql);
try{
while(db.rs.next()){
txtName.setText(db.rs.getString(2));
txtPassword.setText(db.rs.getString(3));
txtSex.setText(db.rs.getString(4));
txtNumber.setText(db.rs.getString(5));
}
}catch(SQLException e){
System.err.println(e.toString());
}
}
/*重载选择按钮事件*/
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable() instanceof JRadioButton){
JRadioButton ck=(JRadioButton)(e.getItemSelectable());
if(ck.isSelected()){
selectType=ck.getText();
}else{
selectType="";
}
}
}
/*定义动作事件*/
public void actionPerformed(ActionEvent e){
DBAccess db=new DBAccess();
db.dbconn();
int index=0;
if(selectType.equals("英语"))
index=6;
else if(selectType.equals("数学"))
index=8;
else if(selectType.equals("JAVA"))
index=7;
else if(selectType.equals("c++"))
index=9;
sql="SELECT * FROM student";
db.dbSelect(sql);
try{
while(db.rs.next()){
areaTxt.append("\n"+"姓名:"+db.rs.getString(2)+" 编号:"+db.rs.getString(4)+
" 性别:"+db.rs.getString(5)+" "+this.selectType+db.rs.getShort(index));
}
}catch(SQLException es){
System.err.println(es.toString());
}
db.dbclose();
}
}