界面展示
登录界面
管理员界面
学生界面
教师界面
一、环境搭建
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驱动文件点击添加即可。
五、数据库信息
CREATE TABLE Student (
SID varchar(10) NOT NULL ,
Sname nvarchar(10) NOT NULL ,
Sex nvarchar(5) check(sex='男' or sex='女') NOT NULL,
SchoolAge int check(Schoolage > 10 and Schoolage < 50) NOT NULL ,
SchoolYear int NOT NULL ,
Class nvarchar(20) NOT NULL ,
primary key(SID)
)
CREATE TABLE Course (
CID varchar(7) NOT NULL ,
Cname nvarchar(10) NOT NULL ,
TID varchar(5) NOT NULL ,
Credit int NOT NULL ,
Grade varchar(30) NOT NULL ,
CancelYear int NULL ,
primary key(CID)
)
CREATE TABLE Teacher (
TID varchar(5) NOT NULL ,
Tname nvarchar(10) NOT NULL ,
Course nvarchar(10) NOT NULL ,
primary key(TID)
)
CREATE TABLE Choose (
SID varchar(10) NOT NULL,
CID varchar(7) NOT NULL,
TID varchar(5) NOT NULL ,
Score int NULL ,
ChooseYear int NOT NULL ,
foreign key(SID) references Student(SID),
foreign key(CID) references Course(CID),
foreign key(TID) references Teacher(TID)
)
CREATE TABLE Permission1 (
SID varchar(10) NOT NULL ,
SPW varchar(10) NOT NULL ,
primary key(SID)
)
CREATE TABLE Permission2 (
TID varchar(5) NOT NULL ,
TPW varchar(10) NOT NULL ,
primary key(TID)
)
CREATE TABLE Permission3 (
MID varchar(7) NOT NULL ,
MPW varchar(10) NOT NULL ,
primary key(MID)
)
六、部分代码展示
数据库连接代码(DBconnection.java)
package zyz.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBconnection {
private static final String jdbcName="com.mysql.cj.jdbc.Driver";
private static final String dbUrl="jdbc:mysql://localhost:3306/MDB?characterEncoding=utf8";
private static final String dbUserName="root";
private static final String dbPassword="462813";
public static Connection con=null;
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon() {
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
*/
public void closeCon(Connection con)throws Exception{
if(con!=null) {
con.close();
}
}
}
登录功能的实现
protected void loginAct(ActionEvent ae) {
// TODO Auto-generated method stub
String 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=new DBconnection();
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(new Runnable() {
public void run() {
try {
adminFrm.frame3 = new adminFrm();
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=new DBconnection();
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(new Runnable() {
public void run() {
try {
teacherFrm.frame8 = new teacherFrm();
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=new DBconnection();
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(new Runnable() {
public void run() {
try {
studentFrm.frame7 = new studentFrm();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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=new DBconnection();
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";}
else if(chooseColumn.equals("3")) {chooseColumn="Sex";}
else if(chooseColumn.equals("4")) {chooseColumn="SchoolAge";}
else if(chooseColumn.equals("5")) {chooseColumn="SchoolYear";}
else if(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=new DBconnection();
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";}
else if(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=new DBconnection();
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";}
else if(chooseColumn.equals("3")) {chooseColumn="TID";}
else if(chooseColumn.equals("4")) {chooseColumn="Credit";}
else if(chooseColumn.equals("5")) {chooseColumn="Grade";}
else if(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=new DBconnection();
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";}
else if(chooseColumn.equals("3")) {chooseColumn="TID";}
else if(chooseColumn.equals("4")) {chooseColumn="Credit";}
else if(chooseColumn.equals("5")) {chooseColumn="Grade";}
else if(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=new DBconnection();
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";}
else if(chooseColumn.equals("2")) {chooseColumn="CID";}
else if(chooseColumn.equals("3")) {chooseColumn="TID";}
else if(chooseColumn.equals("4")) {chooseColumn="Score";}
else if(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(new Runnable() {
public void run() {
try {
teacherFrm.frame8 = new teacherFrm();
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()
公有函数
public class LoginFrm extends JFrame {
private JPanel contentPane;
private JTextField userNameTextField;
private JTextField passwordTextField;
private JRadioButton adminRadioButton;
private JRadioButton teacherRadioButton;
private JRadioButton studentRadioButton;
public static LoginFrm frame;
private final Action action = new SwingAction();
/**
* Launch the application.
*/
public static void LoginFrmClose() {
frame.dispose();
}
而在adminFrm中加入public static adminFrm frame3;
公有成员和public static void adminFrmClose()
公有函数
public class adminFrm extends JFrame {
private JPanel contentPane;
private JTextField nameTextField;
private JTextField idtextField;
private JTextPane textPane;
public static adminFrm frame3;
public static void adminFrmClose() {
frame3.dispose();
}
在控制另一个界面打开时,这个界面就关闭。我们采取一下代码
\\关闭adminFrm,打开LoginFrm,实现adminFrm中退出功能
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrm.frame = new LoginFrm();
LoginFrm.frame.setVisible(true);
frame3.adminFrmClose();
} catch (Exception e) {
e.printStackTrace();
}
}
});
\\关闭LoginFrm,打开adminFrm,实现LoginFrm中管理员登录功能
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
adminFrm.frame3 = new adminFrm();
adminFrm.frame3.setVisible(true);
frame.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
});
而此项目还未完成是因为无法处理较大量的数据,只适用于应付DDL,如果需要的话可以自行在achievementStatic.java的classScoreNewButton按钮监听时间中用String链表替换String。文章来源:https://www.toymoban.com/news/detail-438344.html
测试数据
use MDB;
alter table choose NOCHECK constraint all;
alter table student NOCHECK constraint all;
alter table course NOCHECK constraint all;
alter table teacher NOCHECK constraint all;
alter table choose DISABLE trigger all;
alter table student DISABLE trigger all;
alter table course DISABLE trigger all;
alter table teacher DISABLE trigger all;
insert into Student values('1561401613',N'毛挺','男','18','2015',N'计联');
insert into Student values('1530561331',N'谈力','男','18','2015',N'计联');
insert into Student values('1530501085',N'晁阳','男','19','2015',N'计联');
insert into Student values('1530591376',N'韩梅梅','女','17','2015',N'计科');
insert into Course values('1008060',N'算法','10001',2,'2','2019');
insert into Course values('1203006',N'计组','10003',1,'2','2018');
insert into Course values('3009405',N'java','20010',2,'1','2020');
insert into Course values('7787564',N'经济学','35790',1,'1','2020');
insert into Teacher values('10001',N'颜老师',N'算法');
insert into Teacher values('10003',N'毛老师',N'计组');
insert into Teacher values('20010',N'张老师',N'java');
insert into Teacher values('35790',N'田老师',N'经济学');
insert into Choose values('1561401613','1008060','10001',95,null);
insert into Choose values('1561401613','1203006','10003',92,null);
insert into Choose values('1561401613','3009405','20010',94,null);
insert into Choose values('1530561331','1008060','10001',96,null);
insert into Choose values('1530561331','1203006','10003',88,null);
insert into Choose values('1530591376','1203006','10003',94,null);
insert into Choose values('1530591376','3009405','20010',92,null);
insert into Permission1 values('1561401613','111111');
insert into Permission1 values('1530561331','222222');
insert into Permission1 values('1530501085','333333');
insert into Permission1 values('1530591376','444444');
insert into Permission2 values('10001','555555');
insert into Permission2 values('10003','666666');
insert into Permission2 values('30405','777777');
insert into Permission2 values('35790','888888');
insert into 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文章来源地址https://www.toymoban.com/news/detail-438344.html
到了这里,关于Eclipse+MySql+JavaSwing(WindowBuilder)学生管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!