文章目录
界面展示
登录界面
管理员界面
学生界面
教师界面
一、环境搭建
eclipse网址:https://www.eclipse.org/downloads/
(进入之后安装包直接安装就行)
MySql官方网址:http://www.mysql.com
这里我推荐用C语言中文网的下载方式,较为详细 MySql下载步骤详解
配置为 MySql安装配置教程
在这温馨提醒一下,一般配置路径不要有中文,因为可能会在编程过程中有许多奇奇怪怪的错误。
在我们下载完并搭建好路径之后,我推荐使用WindowBulider来进行UI界面设计。在学过Qt的家人们应该知道Qt有对应专门的UI界面设计组件。而在Eclipse中也是有的(我也不知道哪个鬼才想出来的,反正好用就对了)。它的下载方式有很多种,这里推荐一种最为简单的方法:
首先打开下载好的Eclipse,选择好工作环境之后进入界面
点击Help找到下滑栏中的Eclipse Market点击打开
在Find搜索框中搜索WindowBulider点击Install就会自动安装到Eclipse的目录中。
二、项目目录介绍
image是Source Folder类型的包,用于存储图片,就是你如果想要你的UI界面美观的话就可以放icon和background在里面
model用于存储一些模型;util用于放和MySql连接的模块;view则放置UI界面,也就是用WindowBuilder创建的JavaSwing界面。
三、WindowBuilder的使用
首先创建登录界面,在这里是LoginFrm.java。那么如何用WindowBuilder创建一个界面,操作如下
右键单击想加的包,选择New,选择Other,之后会弹出一个界面,向下滑动找到我们下载的WindowBuilder,点击打开,
选择Next会自动生成一个界面。想要可视化编程则点击界面下方的design按钮。如图:
一般我们都会采用Absolute Layout,这样可以拖拽组件到任意地方,如果有些许误差,我们还可以点击Source回到编程界面,在对应的组件名称创建的地方改变位置坐标。
四、MySql连接Eclipse
数据库连接方法:首先要有驱动文件,这里是下载地址 jdbc驱动下载地址 https://dev.mysql.com/downloads/connector/j/
下载完成后右键项目,选择Bulid Path—Configure Build Path
有的时候你会发现右边的按钮为灰色,这时只需点击一下Mudulepath就行。选择add External JARs,找到你下载的jdbc驱动文件点击添加即可。
五、数据库信息
CREATETABLE Student (
SID varchar(10)NOTNULL,
Sname nvarchar(10)NOTNULL,
Sex nvarchar(5)check(sex='男'or sex='女')NOTNULL,
SchoolAge intcheck(Schoolage >10and Schoolage <50)NOTNULL,
SchoolYear intNOTNULL,
Class nvarchar(20)NOTNULL,primarykey(SID))CREATETABLE Course (
CID varchar(7)NOTNULL,
Cname nvarchar(10)NOTNULL,
TID varchar(5)NOTNULL,
Credit intNOTNULL,
Grade varchar(30)NOTNULL,
CancelYear intNULL,primarykey(CID))CREATETABLE Teacher (
TID varchar(5)NOTNULL,
Tname nvarchar(10)NOTNULL,
Course nvarchar(10)NOTNULL,primarykey(TID))CREATETABLE Choose (
SID varchar(10)NOTNULL,
CID varchar(7)NOTNULL,
TID varchar(5)NOTNULL,
Score intNULL,
ChooseYear intNOTNULL,foreignkey(SID)references Student(SID),foreignkey(CID)references Course(CID),foreignkey(TID)references Teacher(TID))CREATETABLE Permission1 (
SID varchar(10)NOTNULL,
SPW varchar(10)NOTNULL,primarykey(SID))CREATETABLE Permission2 (
TID varchar(5)NOTNULL,
TPW varchar(10)NOTNULL,primarykey(TID))CREATETABLE Permission3 (
MID varchar(7)NOTNULL,
MPW varchar(10)NOTNULL,primarykey(MID))
六、部分代码展示
数据库连接代码(DBconnection.java)
packagezyz.util;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.ResultSet;importjava.sql.Statement;publicclassDBconnection{privatestaticfinalString jdbcName="com.mysql.cj.jdbc.Driver";privatestaticfinalString dbUrl="jdbc:mysql://localhost:3306/MDB?characterEncoding=utf8";privatestaticfinalString dbUserName="root";privatestaticfinalString dbPassword="462813";publicstaticConnection con=null;/**
* 获取数据库连接
* @return
* @throws Exception
*/publicConnectiongetCon(){try{Class.forName(jdbcName);}catch(ClassNotFoundException e){
e.printStackTrace();}try{
con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){
e.printStackTrace();}return con;}/**
* 关闭数据库连接
* @param con
* @throws Exception
*/publicvoidcloseCon(Connection con)throwsException{if(con!=null){
con.close();}}}
登录功能的实现
protectedvoidloginAct(ActionEvent ae){// TODO Auto-generated method stubString userName=userNameTextField.getText().toString();String password=passwordTextField.getText().toString();String jdbcName="com.mysql.cj.jdbc.Driver";String dbUrl="jdbc:mysql://localhost:3306/MDB?characterEncoding=utf8";String dbUserName="root";String dbPassword="462813";if(StringUtil.isEmpty(userName)){System.out.println("用户名不能为空");return;}if(adminRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql="select * from permission3";try{Statement stat = conn.createStatement();ResultSet rs = stat.executeQuery(sql);while(rs.next()){if(userName.equals(rs.getString(1))){if(password.equals(rs.getString(2))){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{
adminFrm.frame3 =newadminFrm();
adminFrm.frame3.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});
frame.dispose();}}}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(teacherRadioButton.isSelected()==true){DBconnection dbutil1=newDBconnection();try{
dbutil1.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conne =null;try{
conne =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql1="select * from permission2";try{Statement state = conne.createStatement();ResultSet rs1 = state.executeQuery(sql1);while(rs1.next()){String temp=rs1.getString(1);if(userName.equals(temp)){if(password.equals(rs1.getString(2))){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{
teacherFrm.frame8 =newteacherFrm();
teacherFrm.frame8.setextend(temp);
teacherFrm.frame8.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});
frame.dispose();}}}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(studentRadioButton.isSelected()==true){DBconnection dbutil2=newDBconnection();try{
dbutil2.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn2 =null;try{
conn2 =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql2="select * from permission1";try{Statement stat2 = conn2.createStatement();ResultSet rs2 = stat2.executeQuery(sql2);while(rs2.next()){String temp1=rs2.getString(1);String temp2=rs2.getString(2);if(userName.equals(temp1)){if(password.equals(temp2)){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{
studentFrm.frame7 =newstudentFrm();
studentFrm.frame7.setextend(temp1);
studentFrm.frame7.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});
frame.dispose();}}}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}}
数据库添加操作
String t1=idtextField.getText().toString();String t2=nametextField.getText().toString();String t3=sextextField.getText().toString();String t4=enterYearTextField.getText().toString();String t5=enterAgeTextField.getText().toString();String t6=classtextField.getText().toString();String jdbcName="com.mysql.cj.jdbc.Driver";String dbUrl="jdbc:mysql://localhost:3306/MDB?characterEncoding=utf8";String dbUserName="root";String dbPassword="462813";if(studentNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="insert into student(SID,Sname,Sex,SchoolAge,SchoolYear,Class) values ('"+t1+"','"+t2+"','"+t3+"','"+t4+"','"+t5+"','"+t6+"')";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"添加成功!","关于添加操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("添加成功!");}else{JOptionPane.showMessageDialog(null,"添加失败,请检查!","关于添加操作",JOptionPane. ERROR_MESSAGE);System.out.println("添加失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(teacherNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="insert into course(TID,Tname,Course) values ('"+t1+"','"+t2+"','"+t3+"')";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"添加成功!","关于添加操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("添加成功!");}else{JOptionPane.showMessageDialog(null,"添加失败,请检查!","关于添加操作",JOptionPane. ERROR_MESSAGE);System.out.println("添加失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(courseNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="insert into course(CID,Cname,TID,Credit,Grade,CancelYear) values ('"+t1+"','"+t2+"','"+t3+"','"+t4+"','"+t5+"','"+t6+"')";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"添加成功!","关于添加操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("添加成功!");}else{JOptionPane.showMessageDialog(null,"添加失败,请检查!","关于添加操作",JOptionPane. ERROR_MESSAGE);System.out.println("添加失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(chooseNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="insert into choose(SID,CID,TID,Score,ChooseYear) values ('"+t1+"','"+t2+"','"+t3+"','"+t4+"','"+t5+"')";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"添加成功!","关于添加操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("添加成功!");}else{JOptionPane.showMessageDialog(null,"添加失败,请检查!","关于添加操作",JOptionPane. ERROR_MESSAGE);System.out.println("添加失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}
数据库删除操作
String id1=ID_1textField.getText().toString();String id2=ID_2textField.getText().toString();String jdbcName="com.mysql.cj.jdbc.Driver";String dbUrl="jdbc:mysql://localhost:3306/MDB?characterEncoding=utf8";String dbUserName="root";String dbPassword="462813";if(deleteStudentRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="delete from student where SID='"+id1+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"删除成功!","关于删除操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("删除成功!");}else{JOptionPane.showMessageDialog(null,"删除失败,请检查!","关于删除操作",JOptionPane. ERROR_MESSAGE);System.out.println("删除失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(deleteTeacherRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="delete from teacher where TID='"+id1+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"删除成功!","关于删除操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("删除成功!");}else{JOptionPane.showMessageDialog(null,"删除失败,请检查!","关于删除操作",JOptionPane. ERROR_MESSAGE);System.out.println("删除失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(deleteCourseRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="delete from course where CID='"+id1+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"删除成功!","关于删除操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("删除成功!");}else{JOptionPane.showMessageDialog(null,"删除失败,请检查!","关于删除操作",JOptionPane. ERROR_MESSAGE);System.out.println("删除失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(deleteChooseRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}String sql ="delete from choose where SID='"+id1+"'"+" and CID='"+id2+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"删除成功!","关于删除操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("删除成功!");}else{JOptionPane.showMessageDialog(null,"删除失败,请检查!","关于删除操作",JOptionPane. ERROR_MESSAGE);System.out.println("删除失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}
数据库修改操作
String chooseColumn=chooseColumnTextField.getText().toString();String userID=userIDtextField.getText().toString();String alterValue=alterValueTextField.getText().toString();String jdbcName="com.mysql.cj.jdbc.Driver";String dbUrl="jdbc:mysql://localhost:3306/MDB?characterEncoding=utf8";String dbUserName="root";String dbPassword="462813";if(studentRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}if(chooseColumn.equals("2")){chooseColumn="Sname";}elseif(chooseColumn.equals("3")){chooseColumn="Sex";}elseif(chooseColumn.equals("4")){chooseColumn="SchoolAge";}elseif(chooseColumn.equals("5")){chooseColumn="SchoolYear";}elseif(chooseColumn.equals("6")){chooseColumn="Class";}String sql="update student set `"+chooseColumn+"`='"+alterValue+"' where `SID` = '"+userID+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"修改成功!","关于修改操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("修改成功!");}else{JOptionPane.showMessageDialog(null,"修改失败,请检查!","关于修改操作",JOptionPane. ERROR_MESSAGE);System.out.println("修改失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(teacherNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}if(chooseColumn.equals("2")){chooseColumn="Tname";}elseif(chooseColumn.equals("3")){chooseColumn="Course";}String sql="update teacher set `"+chooseColumn+"`='"+alterValue+"' where `TID` = '"+userID+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"修改成功!","关于修改操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("修改成功!");}else{JOptionPane.showMessageDialog(null,"修改失败,请检查!","关于修改操作",JOptionPane. ERROR_MESSAGE);System.out.println("修改失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(courseNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}if(chooseColumn.equals("2")){chooseColumn="Cname";}elseif(chooseColumn.equals("3")){chooseColumn="TID";}elseif(chooseColumn.equals("4")){chooseColumn="Credit";}elseif(chooseColumn.equals("5")){chooseColumn="Grade";}elseif(chooseColumn.equals("6")){chooseColumn="CancleYear";}String sql="update course set `"+chooseColumn+"`='"+alterValue+"' where `CID` = '"+userID+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"修改成功!","关于修改操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("修改成功!");}else{JOptionPane.showMessageDialog(null,"修改失败,请检查!","关于修改操作",JOptionPane. ERROR_MESSAGE);System.out.println("修改失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(courseNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}if(chooseColumn.equals("2")){chooseColumn="Cname";}elseif(chooseColumn.equals("3")){chooseColumn="TID";}elseif(chooseColumn.equals("4")){chooseColumn="Credit";}elseif(chooseColumn.equals("5")){chooseColumn="Grade";}elseif(chooseColumn.equals("6")){chooseColumn="CancleYear";}String sql="update course set `"+chooseColumn+"`='"+alterValue+"' where `CID` = '"+userID+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"修改成功!","关于修改操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("修改成功!");}else{JOptionPane.showMessageDialog(null,"修改失败,请检查!","关于修改操作",JOptionPane. ERROR_MESSAGE);System.out.println("修改失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(chooseNewRadioButton.isSelected()==true){DBconnection dbutil=newDBconnection();try{
dbutil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
e.printStackTrace();System.out.println("数据库连接失败");}Connection conn =null;try{
conn =DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}if(chooseColumn.equals("1")){chooseColumn="SID";}elseif(chooseColumn.equals("2")){chooseColumn="CID";}elseif(chooseColumn.equals("3")){chooseColumn="TID";}elseif(chooseColumn.equals("4")){chooseColumn="Score";}elseif(chooseColumn.equals("5")){chooseColumn="ChooseYear";}String sql="update choose set `"+chooseColumn+"`='"+alterValue+"' where `CID` = '"+userID+"'";try{Statement stat = conn.createStatement();int i = stat.executeUpdate(sql);if(i>0){JOptionPane.showMessageDialog(null,"修改成功!","关于修改操作",JOptionPane.INFORMATION_MESSAGE);System.out.println("修改成功!");}else{JOptionPane.showMessageDialog(null,"修改失败,请检查!","关于修改操作",JOptionPane. ERROR_MESSAGE);System.out.println("修改失败!");}}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}
七、小坑避免
在我写登录界面时,是用ResultSet对数据库数据进行访问,
Statement state = conne.createStatement();ResultSet rs1 = state.executeQuery(sql1);while(rs1.next()){String temp=rs1.getString(1);if(userName.equals(temp)){if(password.equals(rs1.getString(2))){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{
teacherFrm.frame8 =newteacherFrm();
teacherFrm.frame8.setextend(temp);
teacherFrm.frame8.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});
frame.dispose();}}}
这里如果不用temp对rs1.getString(1)进行替换而是直接是用的话就会报错。原因目前我也不大清楚,希望有大佬可以给予讲解。
在页面跳转中,我看了网上教程,都感觉有点云里雾里。我这里推荐一个比较简单的操作方式,在每个JavaSwing界面都加上一个public公有成员。以本项目为例,如果我们要实现LoginFrm(登录界面)和adminFrm(管理员界面)的跳转,我们只需要在LoginFrm中加入
public static LoginFrm frame;
公有成员以及
public static void LoginFrmClose()
公有函数
publicclassLoginFrmextendsJFrame{privateJPanel contentPane;privateJTextField userNameTextField;privateJTextField passwordTextField;privateJRadioButton adminRadioButton;privateJRadioButton teacherRadioButton;privateJRadioButton studentRadioButton;publicstaticLoginFrm frame;privatefinalAction action =newSwingAction();/**
* Launch the application.
*/publicstaticvoidLoginFrmClose(){
frame.dispose();}
而在adminFrm中加入
public static adminFrm frame3;
公有成员和
public static void adminFrmClose()
公有函数
publicclass adminFrm extendsJFrame{privateJPanel contentPane;privateJTextField nameTextField;privateJTextField idtextField;privateJTextPane textPane;publicstatic adminFrm frame3;publicstaticvoidadminFrmClose(){
frame3.dispose();}
在控制另一个界面打开时,这个界面就关闭。我们采取一下代码
\\关闭adminFrm,打开LoginFrm,实现adminFrm中退出功能
EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{LoginFrm.frame =newLoginFrm();LoginFrm.frame.setVisible(true);
frame3.adminFrmClose();}catch(Exception e){
e.printStackTrace();}}});
\\关闭LoginFrm,打开adminFrm,实现LoginFrm中管理员登录功能
EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{
adminFrm.frame3 =newadminFrm();
adminFrm.frame3.setVisible(true);
frame.dispose();}catch(Exception e){
e.printStackTrace();}}});
而此项目还未完成是因为无法处理较大量的数据,只适用于应付DDL,如果需要的话可以自行在achievementStatic.java的classScoreNewButton按钮监听时间中用String链表替换String。
测试数据
use MDB;altertable choose NOCHECK constraintall;altertable student NOCHECK constraintall;altertable course NOCHECK constraintall;altertable teacher NOCHECK constraintall;altertable choose DISABLEtriggerall;altertable student DISABLEtriggerall;altertable course DISABLEtriggerall;altertable teacher DISABLEtriggerall;insertinto Student values('1561401613',N'毛挺','男','18','2015',N'计联');insertinto Student values('1530561331',N'谈力','男','18','2015',N'计联');insertinto Student values('1530501085',N'晁阳','男','19','2015',N'计联');insertinto Student values('1530591376',N'韩梅梅','女','17','2015',N'计科');insertinto Course values('1008060',N'算法','10001',2,'2','2019');insertinto Course values('1203006',N'计组','10003',1,'2','2018');insertinto Course values('3009405',N'java','20010',2,'1','2020');insertinto Course values('7787564',N'经济学','35790',1,'1','2020');insertinto Teacher values('10001',N'颜老师',N'算法');insertinto Teacher values('10003',N'毛老师',N'计组');insertinto Teacher values('20010',N'张老师',N'java');insertinto Teacher values('35790',N'田老师',N'经济学');insertinto Choose values('1561401613','1008060','10001',95,null);insertinto Choose values('1561401613','1203006','10003',92,null);insertinto Choose values('1561401613','3009405','20010',94,null);insertinto Choose values('1530561331','1008060','10001',96,null);insertinto Choose values('1530561331','1203006','10003',88,null);insertinto Choose values('1530591376','1203006','10003',94,null);insertinto Choose values('1530591376','3009405','20010',92,null);insertinto Permission1 values('1561401613','111111');insertinto Permission1 values('1530561331','222222');insertinto Permission1 values('1530501085','333333');insertinto Permission1 values('1530591376','444444');insertinto Permission2 values('10001','555555');insertinto Permission2 values('10003','666666');insertinto Permission2 values('30405','777777');insertinto Permission2 values('35790','888888');insertinto Permission3 values('24680','100000');select*from choose;select*from Student;select*from Teacher;select*from Course;select*from Permission1;select*from Permission2;select*from Permission3;
第一次做有关项目可能结构不是很好,地址在这
https://download.csdn.net/download/qq_50062694/85322004
版权归原作者 浅川☡ 所有, 如有侵权,请联系我们删除。