该程序是链接mysql数据库的,然后我们是使用了阿里云的云服务器配置了数据库(这个是另一个同学做的),不过一下代码对于本地数据库应该也是可以实现的,然后实现了一些基本的功能比如增删查改,使用了javaswing来写页面(比较丑哈哈),学生和老师注册登录啊之类的功能。
import Mainfunction.CheckAccount;
import Mainfunction.CheckAccount2;
import Mainfunction.InsertStudentInformation;
import Mainfunction.InsertTeacherInformation;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
class Fun implements ActionListener{
Connection connection;
JFrame frame=new JFrame();
JLabel lb1;//顶部需要的组件
Button bt1,bt2,bt3,bt4;
//定义中间所需要的组件
// 中部有4个panel,选项卡窗口管理
JTabbedPane pane;
JPanel panel_s,panel_t;
JLabel label1,label2;
JLabel label3,label4;
JLabel stu;//用来提示这是学生的登录界面,学生默认输入长度为11位
JLabel tea;//用来提示这是老师的登录界面,老师默认输入长度为3位
JTextField textField_student;//学生区域的学生输入框
JPasswordField Password_student;//密码输入框
JTextField textField_teacher;//老师区域输入框
JPasswordField Password_teacher;//老师区域密码输入框
//底部所需要的组件
JPanel jPanel1;
JButton jButton;
//3学生注册界面相关控件
JPanel stu_register;
JLabel S_register_lb1,S_register_lb2;
JTextField S_register;
JPasswordField S_pass;
JLabel Student;
//4老师注册界面相关控件
JPanel tea_register;
JLabel T_register_lb1,T_register_lb2;
JTextField T_register;
JPasswordField T_pass;
JLabel Teacher;
@Override
public void actionPerformed(ActionEvent e) {
}
public Fun()
{
//数据库连接
jdbcConnection jdbc=new jdbcConnection();
connection=jdbc.giveConnection();
//处理底部
lb1=new JLabel(new ImageIcon("src/mao.png"));
jPanel1=new JPanel();
//jButton=new JButton("确定");
//jPanel1.add(jButton);//将button组件加进去
//后面舍弃了这种做法直接四个界面加入button应对不同的情况
//处理中部
panel_s=new JPanel(new GridLayout(3,3));
label1=new JLabel("学生账号",JLabel.CENTER);
label2=new JLabel("学生密码",JLabel.CENTER);
textField_student=new JTextField();
Password_student=new JPasswordField();
stu=new JLabel("这里是学生登录界面",JLabel.CENTER);
bt1=new Button("login");
//按控件顺序加入到每个板面
panel_s.add(label1);
panel_s.add(textField_student);
panel_s.add(label2);
panel_s.add(Password_student);
panel_s.add(stu);
panel_s.add(bt1);
//老师
panel_t=new JPanel(new GridLayout(3,3));
label3=new JLabel("老师账号",JLabel.CENTER);
label4=new JLabel("老师密码",JLabel.CENTER);
textField_teacher=new JTextField();
Password_teacher=new JPasswordField();
bt2=new Button("login");
tea=new JLabel("这里是老师登录界面",JLabel.CENTER);
panel_t.add(label3);
panel_t.add(textField_teacher);
panel_t.add(label4);
panel_t.add(Password_teacher);
panel_t.add(tea);
panel_t.add(bt2);
//学生注册
stu_register=new JPanel(new GridLayout(3,3));
S_register_lb1=new JLabel("注册学生账号",JLabel.CENTER);
S_register_lb2=new JLabel("注册学生密码",JLabel.CENTER);
S_register=new JTextField();
S_pass=new JPasswordField();
Student=new JLabel("这里是学生注册",JLabel.CENTER);
bt3=new Button("register");
Student.setForeground(Color.RED);
stu_register.add(S_register_lb1);
stu_register.add(S_register);
stu_register.add(S_register_lb2);
stu_register.add(S_pass);
stu_register.add(Student);
stu_register.add(bt3);
//老师注册
tea_register=new JPanel(new GridLayout(3,3));
T_register_lb1=new JLabel("注册老师账号",JLabel.CENTER);
T_register_lb2=new JLabel("注册老师密码",JLabel.CENTER);
T_register=new JTextField();
T_pass=new JPasswordField();
Teacher=new JLabel("这里是进行老师注册",JLabel.CENTER);
bt4=new Button("register");
Teacher.setForeground(Color.BLUE);
tea_register.add(T_register_lb1);
tea_register.add(T_register);
tea_register.add(T_register_lb2);
tea_register.add(T_pass);
tea_register.add(Teacher);
tea_register.add(bt4);
//创建选项卡窗口
//加入页面
pane=new JTabbedPane();
pane.add("学生登录",panel_s);
pane.add("老师登录",panel_t);
pane.add("学生注册",stu_register);
pane.add("老师注册",tea_register);
frame.setSize(550,340);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(lb1,"North");
frame.add(jPanel1,"South");
frame.add(pane,"Center");
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account= textField_student.getText();
String password=new String(Password_student.getPassword());
CheckAccount checkAccount=new CheckAccount(frame,connection,account,password);
if(checkAccount.check()){
UserView view= new UserView(1,connection,account);
frame.dispose();
// view.number=1;
}
else{
JOptionPane.showMessageDialog(
frame,
"登录错误",
"Error",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account= textField_teacher.getText();
String password=new String(Password_teacher.getPassword());
CheckAccount2 checkAccount=new CheckAccount2(frame,connection,account,password);
if(checkAccount.check()){
UserView view= new UserView(2,connection,account);
frame.dispose();
// view.number=1;
}
else{
JOptionPane.showMessageDialog(
frame,
"登录错误",
"Error",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
bt3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
//读取账号和密码
String account= S_register.getText();
String password=new String(S_pass.getPassword());
//进行数字的判断
int number=Integer.parseInt(account);
//进行数字长度的判断
if(account.length()==6){
InsertStudentInformation s=new InsertStudentInformation(frame,connection,account,password);
if(s.giveBool()){
JOptionPane.showMessageDialog(
frame,
"注册成功",
"Yes",
JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(
frame,
"账号不合法,注意为6位数字",
"Error",
JOptionPane.INFORMATION_MESSAGE);
}
}catch (Exception exception){
JOptionPane.showMessageDialog(
frame,
"账号不合法,注意为三位数字",
"Error",
JOptionPane.INFORMATION_MESSAGE
);
}
//将学生的数据存入数据库
}
});
bt4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
//读取账号和密码
String account= T_register.getText();
String password=new String(T_pass.getPassword());
//进行数字的判断
int number=Integer.parseInt(account);
//进行数字长度的判断
if(account.length()==3){
InsertTeacherInformation t=new InsertTeacherInformation(frame,connection,account,password);
//返回bool值检查是否已存在已有账号
if(t.giveBool()){
JOptionPane.showMessageDialog(
frame,
"注册成功",
"Yes",
JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(
frame,
"账号不合法,注意为3位数字",
"Error",
JOptionPane.INFORMATION_MESSAGE);
}
}catch (Exception exception){
JOptionPane.showMessageDialog(
frame,
"账号不合法,注意为三位数字",
"Error",
JOptionPane.INFORMATION_MESSAGE
);
}
//老师的数据存入数据库
}
});
}
}
public class Main {
public static void main(String[] args) {
Fun Fun=new Fun();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import check.*;
import check.Teacher_InsertScore;
import view.*;
import javax.swing.*;
public class UserView {
//用户id
private String id;
//数据库
private Connection connection;
private JFrame userView = new JFrame("界面");
private JPanel panel = new JPanel(new FlowLayout());
public int number;
//添加功能选择
private JButton studentInformation = new JButton("学生信息");
private JButton Student_Course = new JButton("学生选课");
private JButton Teacher_Information = new JButton("老师信息");
private JButton Teacher_CheckScore = new JButton("查看分数");
private JButton Teacher_InsertScore = new JButton("登记分数");
private JButton function6 = new JButton("数据库备份");
//先声明功能的选择,到以后写好包之后即可删除
private JFrame fuction5_View;
private JFrame fuction6_View;
public UserView(int number, Connection connection, String id) {
this.connection = connection;
this.number = number;
this.id = id;
init();
}
public void init() {
if (this.number == 1) {
studentInformation.setFont(new Font("", Font.BOLD, 15));
panel.add(studentInformation);
Student_Course.setFont(new Font("", Font.BOLD, 15));
panel.add(Student_Course);
} else {
Teacher_Information.setFont(new Font("", Font.BOLD, 15));
panel.add(Teacher_Information);
Teacher_InsertScore.setFont(new Font("", Font.BOLD, 15));
panel.add(Teacher_InsertScore);
Teacher_CheckScore.setFont(new Font("", Font.BOLD, 15));
panel.add(Teacher_CheckScore);
function6.setFont(new Font("", Font.BOLD, 15));
panel.add(function6);
}
//添加佛大图片
JLabel Fosu = new JLabel(new ImageIcon("src/img.png"));
panel.add(Fosu);
Fosu.setBounds(0, 150, 700, 200);
//添加文字
JLabel showName = new JLabel("成绩管理系统");
showName.setFont(new Font("", Font.BOLD, 22));
panel.add(showName);
userView.add(panel);
userView.setSize(500, 500);
userView.setLocationRelativeTo(null);
userView.setVisible(true);
userView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* 给按钮添加事件监听器
*/
//学生信息页面
studentInformation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userView.setVisible(false);
Student_Information student_information = new Student_Information(connection, id, userView);
student_information.studentView.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
userView.setVisible(true);
userView.setLocationRelativeTo(null);
super.windowClosing(e);
}
});
}
});
//学生选课页面
Student_Course.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//这里为功能输入,后期直接写好包之后导入即可
userView.setVisible(false);
Student_Course course1 = new Student_Course(connection, id);
course1.course.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
userView.setVisible(true);
userView.setLocationRelativeTo(null);
super.windowClosing(e);
}
});
}
});
//老师信息页面
Teacher_Information.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userView.setVisible(false);
Teacher_Information course1 = new Teacher_Information(connection, id, userView);
course1.teacherView.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
userView.setVisible(true);
userView.setLocationRelativeTo(null);
super.windowClosing(e);
}
});
}
});
//登记分数。
Teacher_InsertScore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userView.setVisible(false);
Teacher_InsertScore teacher_insertScore=new Teacher_InsertScore(id);
teacher_insertScore.jFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
userView.setVisible(true);
userView.setLocationRelativeTo(null);
super.windowClosing(e);
}
});
}
});
//检查分数
Teacher_CheckScore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userView.setVisible(false);
Teacher_CheckScore teacher_checkScore = new Teacher_CheckScore(connection, id);
teacher_checkScore.jf.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
userView.setVisible(true);
userView.setLocationRelativeTo(null);
super.windowClosing(e);
}
});
}
});
//功能六页面
function6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//这里为功能输入,后期直接写好包之后导入即可
// fuction2_View = Singleton2.getInstance(); 单例模式
fuction6_View = new JFrame();
fuction6_View.setTitle("第二个窗口");
fuction6_View.setSize(500, 500);
fuction6_View.setLocationRelativeTo(null);
userView.setVisible(false);
fuction6_View.setVisible(true);
fuction6_View.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
userView.setVisible(true);
super.windowClosing(e);
}
});
}
});
}
}
import java.sql.*;
public class jdbcConnection {
Connection connection;
public jdbcConnection() {
Connection conn = null;
try {
try {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
} catch (SQLException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://XXXXX";//这里为自己的数据库的链接
String user = "XXXX"; //这里的信息是需要更改的账号
String password = "XXXX";//这里的信息是需要更改的密码
conn = DriverManager.getConnection(url, user, password);
System.out.println("数据库连接对象" + conn);
connection= conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection giveConnection(){
return connection;
}
}
package check;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBexecute {
Connection connection;
private PreparedStatement pstm;
private ResultSet rs;
public Connection getConnection() throws Exception {
try {
String url = "jdbc:mysql://120.25.152.50:3306/database";
String user = "root"; //这里的信息是需要更改的
String password = "zqy939597A";
connection = DriverManager.getConnection(url, user, password);
System.out.println("数据库连接对象" + connection);
return connection;
} catch (Exception e) {
throw new SQLException("驱动错误或连接失败!");
}
}
//查询操作
public ResultSet executeQurey(String sql,String a) {
try {
//得到preparaStatement语句
pstm = connection.prepareStatement(sql);
if(a!=null){
pstm.setString(1,a);
}
//执行sql语句
rs = pstm.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回查询结果集
return rs;
}
//插入、删除操作
public int executeUpdate(String sql, String[] data,String course) {
int count = 0;
try {
pstm = connection.prepareStatement(sql);
if (data != null) {
for (int i = 0; i < data.length; i++) {
pstm.setString(i + 1, data[i]);
}
count = pstm.executeUpdate();
String sql1 = "update score set course=? where id=?";
pstm = connection.prepareStatement(sql1);
System.out.println("!!!"+course);
System.out.println("!!!"+data[0]);
pstm.setString(1,course);
pstm.setString(2,data[0]);
pstm.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}
//返回修改成功的行数
return count;
}
public int executeDelete(String sql, String[] data) {
int count = 0;
try {
pstm = connection.prepareStatement(sql);
if (data != null) {
for (int i = 0; i < data.length; i++) {
pstm.setString(i + 1, data[i]);
}
count = pstm.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}
//返回修改成功的行数
return count;
}
public void closeAll() {
// 如果rs不空,关闭rs
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 如果pstm不空,关闭pstm
if (pstm != null) {
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 如果conn不空,关闭conn
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Teacher_InsertScore
package check;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class Teacher_InsertScore {
private String id;
public JFrame jFrame =new JFrame();
//滚动面板
private JScrollPane sptable;
//表格
private JTable table;
//放置按钮的面板
private JPanel panel;
//设置按钮
private JButton btsave, btdelete, btadd, btflush;
//设置默认表格格式
private DefaultTableModel model;
private String course;
Connection connection;
public Teacher_InsertScore(String id) {
this.id=id;
returnCourse();
jFrame.setTitle(course+"课程成绩管理页面");
//设置表格
model = new DefaultTableModel();
table = new JTable(model);
table.setForeground(Color.BLACK); // 字体颜色
table.setFont(new Font(null, Font.PLAIN, 14)); // 字体样式
table.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色
table.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景
table.setGridColor(Color.GRAY);
table.getTableHeader().setFont(new Font("楷体", Font.BOLD, 14)); // 设置表头名称字体样式
table.getTableHeader().setForeground(Color.RED); // 设置表头名称字体颜色
table.getTableHeader().setResizingAllowed(false); // 设置不允许手动改变列宽
table.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列
//将表格放入滚动面板中
sptable = new JScrollPane(table);
//将滚动面板放入主面板中部
jFrame.add(sptable, BorderLayout.CENTER);
btsave = new JButton("保存");
btadd = new JButton("添加");
btdelete = new JButton("删除");
btflush = new JButton("刷新");
panel = new JPanel();
panel.add(btadd);
panel.add(btsave);
panel.add(btdelete);
panel.add(btflush);
btsave.setVisible(false);
jFrame.add(panel, BorderLayout.SOUTH);
//显示初始数据
showData();
//设置界面基本数据
jFrame.setSize(1000, 600);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
jFrame.setVisible(true);
//添加监听器,因为每个实现的功能不同,所以这里使用匿名内部类来实现
btadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addData();
}
});
btsave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
saveDate();
}
});
btdelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
deleteDate();
}
});
btflush.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showData();
}
});
}
void showData() {
// TODO Auto-generated method stub
try {
returnCourse();
String sql = "select id,name,score from score where course= ? ";
DBexecute db = new DBexecute();
connection=db.getConnection();
//连接数据库
db.getConnection();
//获取返回集
ResultSet rs = db.executeQurey(sql,course);
//获取列名
System.out.println(id);
System.out.println(course);
ResultSetMetaData rsmt = rs.getMetaData();
//获取列数
int count = rsmt.getColumnCount();
//创建一个Vector集合存放列名title
Vector title = new Vector<>();
//存放列名
for (int i = 1; i <= count; i++) {
title.add(rsmt.getColumnLabel(i));
}
//存放集合(行数据)的集合
Vector> data = new Vector();
//判断表中有无数据
int rowCount = 0;
//将数据放入data中
while (rs.next()) {
rowCount++;
//设置一个集合存放行数据,在放入data中
Vector rowData = new Vector<>();
for (int i = 1; i <= count; i++) {
rowData.add(rs.getString(i));
}
data.add(rowData);
}
if (rowCount == 0) {
//若行为0即数据库表中没有数据,便将title列名放入table即可
model.setDataVector(null, title);
} else {
//若有数据,则将调用setDataVector将title插入data的第0列
model.setDataVector(data, title);
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(jFrame, "系统错误!请仔细检查!");
}
}
private void addData() {
// TODO Auto-generated method stub
int rowCount = model.getRowCount();
int rowCount1 = table.getRowCount();
//最好使用Object[]
model.insertRow(rowCount, new String[]{"0", "0","0"});
btadd.setVisible(false);
btsave.setVisible(true);
}
private void saveDate() {
// TODO Auto-generated method stub
int rowCount = table.getRowCount() - 1;
//获取自己填写的数据
String id = table.getValueAt(rowCount, 0).toString();
String name = table.getValueAt(rowCount, 1).toString();
String score = table.getValueAt(rowCount, 2).toString();
DBexecute db = new DBexecute();
try {
db.getConnection();
String sql = "insert score values(?,?,?,?)";
int count = db.executeUpdate(sql, new String[]{id,name,null,score},course);
showData();
btadd.setVisible(true);
btsave.setVisible(false);
if (count == 1) {
JOptionPane.showMessageDialog(jFrame, "插入数据成功!");
} else {
JOptionPane.showMessageDialog(jFrame, "插入数据失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
db.closeAll();
}
}
private void deleteDate() {
// TODO Auto-generated method stub
int index[] = table.getSelectedRows();
if (index == null) {
JOptionPane.showMessageDialog(jFrame, "请选择需要删除的数据!", "删除", JOptionPane.PLAIN_MESSAGE);
} else {
try {
int k = JOptionPane.showConfirmDialog(jFrame, "是否要删除这条记录?", "删除", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (k == JOptionPane.YES_OPTION) {
DBexecute db = new DBexecute();
try {
db.getConnection();
String sql = "delete from score where id=?";
String name = table.getValueAt(index[0], 0).toString();
int count = db.executeDelete(sql, new String[]{name});
showData();
if (count == 1) {
JOptionPane.showMessageDialog(jFrame, "删除数据成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(jFrame, "删除数据失败!", "失败", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
db.closeAll();
}
}
} catch (Exception ee) {
JOptionPane.showMessageDialog(jFrame, "抱歉!删除数据失败!【系统异常!】", "失败:", 0);
}
}
}
public void returnCourse() {
try {
DBexecute db = new DBexecute();
db.getConnection();
String sql="select course from 老师基本信息表 where teacherid=?";
ResultSet rs=db.executeQurey(sql,id);
if(rs.next()){
this.course=rs.getString(1);
}
} catch (Exception e) {
}
}
}
package Mainfunction;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class CheckAccount {
private Connection connection;
private String account;
private JFrame frame;
private String password;
PreparedStatement stmt=null;
ResultSet result=null;
public CheckAccount(JFrame frame,Connection connection, String account, String password){
this.frame=frame;
this.account=account;
this.password=password;
this.connection=connection;
}
public boolean check(){
try {
String sql = "select * from studentregister where username= ? and password= ? ";
stmt = connection.prepareStatement(sql);
stmt.setString(1, account);
stmt.setString(2, password);
result = stmt.executeQuery();
if (result.next()) {
JOptionPane.showMessageDialog(
frame,
"登录成功",
"Yes",
JOptionPane.INFORMATION_MESSAGE);
return true;
}
else{
return false;
}
}
catch (Exception e){
return false;
}
}
}
package Mainfunction;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class CheckAccount2 {
private Connection connection;
private String account;
private JFrame frame;
private String password;
PreparedStatement stmt = null;
ResultSet result = null;
public CheckAccount2(JFrame frame, Connection connection, String account, String password) {
this.frame = frame;
this.account = account;
this.password = password;
this.connection = connection;
}
public boolean check() {
try {
String sql = "select * from teacherregister where username= ? and password= ? ";
stmt = connection.prepareStatement(sql);
stmt.setString(1, account);
stmt.setString(2, password);
result = stmt.executeQuery();
if (result.next()) {
JOptionPane.showMessageDialog(
frame,
"登录成功",
"Yes",
JOptionPane.INFORMATION_MESSAGE);
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
}
package Mainfunction;
import javax.swing.*;
import java.sql.*;
public class InsertStudentInformation {
private String account;
private String password;
private Connection connection;
private JFrame frame;
PreparedStatement stmt=null;
ResultSet result=null;
public InsertStudentInformation(JFrame frame,Connection connection,String account,String password){
this.account=account;
this.password=password;
this.connection=connection;
this.frame=frame;
}
public boolean giveBool(){
try {
String sql = "select * from studentregister where username= ?";
stmt = connection.prepareStatement(sql);
stmt.setString(1, account);
result = stmt.executeQuery();
//查询到已有账号就是已经存在,则返回false
if (result.next()) {
JOptionPane.showMessageDialog(
frame,
"注册错误,已存在此账号",
"No",
JOptionPane.INFORMATION_MESSAGE);
return false;
}
//插入注册的账号和密码
String sql1 = "insert into studentregister value (?,?)";
String sql2 = "insert into 学生基本信息表 value (?,?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql1);
PreparedStatement pstmt2 = connection.prepareStatement(sql2);
pstmt.setString(1,account);
pstmt.setString(2,password);
pstmt2.setString(1,account);
pstmt2.setString(2,"null");
pstmt2.setString(3,"null");
pstmt2.setString(4,"null");
int count1 = pstmt.executeUpdate();
int count2 = pstmt2.executeUpdate();
if(count1 ==1&&count2==1){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
}
``
package Mainfunction;
import javax.swing.;
import java.sql.;
public class InsertTeacherInformation {
private String account;
private String password;
private Connection connection;
private JFrame frame;
PreparedStatement stmt=null;
ResultSet result=null;
public InsertTeacherInformation(JFrame frame,Connection connection,String account,String password){
this.account=account;
this.password=password;
this.connection=connection;
this.frame=frame;
}
public boolean giveBool(){
try {
String sql = "select * from teacherregister where username= ?";
stmt = connection.prepareStatement(sql);
stmt.setString(1, account);
result = stmt.executeQuery();
//查询到已有账号就是已经存在,则返回false
if (result.next()) {
JOptionPane.showMessageDialog(
frame,
"注册错误,已存在此账号",
"No",
JOptionPane.INFORMATION_MESSAGE);
return false;
}
//插入注册的账号和密码
String sql1 = "insert into teacherregister value (?,?)";
String sql2 = "insert into 老师基本信息表 value (?,?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql1);
PreparedStatement pstmt2 = connection.prepareStatement(sql2);
pstmt.setString(1,account);
pstmt.setString(2,password);
pstmt2.setString(1,account);
pstmt2.setString(2,"null");
pstmt2.setString(3,"null");
pstmt2.setString(4,"null");
int count1 = pstmt.executeUpdate();
int count2 = pstmt2.executeUpdate();
if(count1 ==1&&count2==1){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
package view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class Student_Course extends Component implements ActionListener {
ActionEvent event;
private Connection connection;
private String id;
private int selectclass;
public JFrame course=new JFrame();
private JPanel panel = new JPanel(new GridLayout());
public Student_Course(Connection connection,String id){
this.connection=connection;
this.id=id;
init();
}
void init(){
course=new JFrame();
course.setTitle("学生选课查询");
course.setSize(500,500);
JRadioButton b1=new JRadioButton("大数据技术");
b1.setFont(new Font("", Font.BOLD, 15));
JRadioButton b2=new JRadioButton("算法分析与设计");
b2.setFont(new Font("", Font.BOLD, 15));
JRadioButton b3=new JRadioButton("Kotlin语言");
b3.setFont(new Font("", Font.BOLD, 15));
JRadioButton b4=new JRadioButton("汇编语言");
b4.setFont(new Font("", Font.BOLD, 15));
b1.setBounds(75,50,100,30);
b2.setBounds(75,90,100,30);
b3.setBounds(75,130,100,30);
b4.setBounds(75,170,100,30);
ButtonGroup bg=new ButtonGroup();
JTextArea area=new JTextArea("请选择你的选课,点击选择后页面会自动退出,请注意",40,40);
area.setFont(new Font("", Font.BOLD, 22));
area.setEditable(false);
area.setLineWrap(true);
bg.add(b1);
bg.add(b2);
bg.add(b3);
bg.add(b4);
panel.add(area);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
course.add(panel);
course.setLocationRelativeTo(null);
course.setVisible(true);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(b1.isSelected())
{
JOptionPane.showMessageDialog(course,"你已选择大数据技术课程成功");
selectclass=1;
updateCourse(selectclass);
course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(b2.isSelected())
{
JOptionPane.showMessageDialog(course,"你已选择算法设计与分析课程成功");
selectclass=2;
updateCourse(selectclass);
course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
}
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(b3.isSelected())
{
JOptionPane.showMessageDialog(course,"你已选择Kotlin语言课程成功");
selectclass=3;
updateCourse(selectclass);
course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
}
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(b4.isSelected())
{
JOptionPane.showMessageDialog(course,"你已选择汇编语言成功");
selectclass=4;
updateCourse(selectclass);
course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void updateCourse(int i){
String a="大数据技术";
String b="算法分析与设计";
String c="Kotlin语言";
String d="汇编语言";
switch (i){
case 1:
try{
String sql = "update 学生基本信息表 set course=? where id=?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,a);
pstmt.setString(2,id);
pstmt.executeUpdate();
}catch (Exception abc){
}
break;
case 2:
try{
String sql = "update 学生基本信息表 set course=? where id=?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,b);
pstmt.setString(2,id);
pstmt.executeUpdate();
}catch (Exception abc){
}
break;
case 3:
try{
String sql = "update 学生基本信息表 set course=? where id=?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,c);
pstmt.setString(2,id);
pstmt.executeUpdate();
}catch (Exception abc){
}
break;
case 4:
try{
String sql = "update 学生基本信息表 set course=? where id=?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,d);
pstmt.setString(2,id);
pstmt.executeUpdate();
}catch (Exception abc){
}
break;
}
}
}
### Student_Information
package view;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Student_Information {
ResultSet result=null;
private String id;
private String name;
private String classes;
private String course;
private JFrame userview;
private Connection connection;
private int score;
public JFrame studentView=new JFrame();
private JPanel panel = new JPanel(new GridLayout());
JTextField textField_stu=new JTextField();
JTextField textField_class=new JTextField();
JTextField textField_account=new JTextField();
JTextField textField_course=new JTextField();
JTextField fun_course=new JTextField();
JButton submit=new JButton(“提交”);
JButton Return=new JButton(“返回”);
public Student_Information(Connection connection,String id,JFrame userview){
this.connection=connection;
this.userview=userview;
this.id=id;
init();
}
void init(){
studentView=new JFrame();
studentView.setTitle(“学生信息查询”);
studentView.setSize(500,500);
studentView.setLocationRelativeTo(null);
studentView.setVisible(true);
GridLayout layout=new GridLayout(6,2);
JPanel panel=new JPanel(layout);//设置为layout布局
JLabel L_stu=new JLabel(“名字”);
JLabel L_class=new JLabel(“班级”);
JLabel L_account=new JLabel(“账户”);
JLabel L_course=new JLabel(“已选课程”);
JLabel L_fun=new JLabel(“所选课程成绩”);
L_fun.setFont(new Font(“”,Font.BOLD,22));
fun_course.setFont(new Font("",Font.BOLD,22));
textField_account.setEditable(false);
textField_course.setEditable(false);
fun_course.setEditable(false);
L_stu.setFont(new Font("",Font.BOLD,22));
L_class.setFont(new Font("",Font.BOLD,22));
L_account.setFont(new Font("",Font.BOLD,22));
L_course.setFont(new Font("",Font.BOLD,22));
textField_course.setFont(new Font("",Font.BOLD,22));
textField_class.setFont(new Font("",Font.BOLD,22));
textField_stu.setFont(new Font("",Font.BOLD,22));
textField_account.setFont(new Font("",Font.BOLD,22));
submit.setFont(new Font("",Font.BOLD,22));
Return.setFont(new Font("",Font.BOLD,22));
giveFun();
panel.add(L_account);
panel.add(textField_account);
panel.add(L_class);
panel.add(textField_class);
panel.add(L_stu);
panel.add(textField_stu);
panel.add(L_course);
panel.add(textField_course);
panel.add(L_fun);
panel.add(fun_course);
panel.add(submit);
panel.add(Return);
studentView.add(panel);
studentView.setVisible(true);
getInformation();
function();
}
public void getInformation(){
try {
PreparedStatement stmt=null;
String sql = "select * from 学生基本信息表 where id= ? ";
System.out.println(id);
stmt = connection.prepareStatement(sql);
stmt.setString(1, id);
result = stmt.executeQuery();
if (result.next()) {
textField_account.setText(result.getString(1));
textField_class.setText(result.getString(3));
textField_stu.setText(result.getString(2));
textField_course.setText(result.getString(4));
course=result.getString(4);
System.out.println("!"+course);
}
giveFun();
}catch (Exception e){
}
}
public void function(){
submit.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try{
id=textField_account.getText();
name=textField_stu.getText();
classes=textField_class.getText();
course=textField_course.getText();
String sql = "update 学生基本信息表 set name=?,class=? where id=?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,classes);
pstmt.setString(3,id);
giveFun();
int count = pstmt.executeUpdate();
if(count==1){
JOptionPane.showMessageDialog(
studentView,
"提交成功",
"Yes",
JOptionPane.INFORMATION_MESSAGE);
}
}catch(Exception exception){
}
}
});
Return.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
userview.setVisible(true);
studentView.dispose();
}
});
}
public void giveFun(){
try{
course=textField_course.getText();
String sql = " select score from score where id=? and course=? ";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, course);
ResultSet resultset = pstmt.executeQuery();
if (resultset.next())
score = resultset.getInt(1);
fun_course.setText( String.valueOf(score));
}catch (Exception e){
score=0;
fun_course.setText( String.valueOf(score));
}
}
}
package view;
import com.mysql.cj.protocol.Resultset;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutionException;
//大概每个模块的粗略模板
public class Teacher_CheckScore {
private Connection connection;
private String course;
private String id;
private int fun;
public JFrame jf;
ResultSet resultset = null;
public Teacher_CheckScore(Connection connection, String id) {
this.connection = connection;
this.id = id;
exe();
}
void exe() {
try {
jf = new JFrame("查看成绩");
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
returnCourse();
JPanel panel = new JPanel(new BorderLayout());
String lSqlStr = "select count(*) from score where course=?";
PreparedStatement statement1 = connection.prepareStatement(lSqlStr);
statement1.setString(1, course);
resultset = statement1.executeQuery();
int count = 0;
if (resultset.next()) {
count = resultset.getInt(1);
System.out.println("numberOfRows= " + count);
String sql = " select * from score where course=? ";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, course);
resultset = statement.executeQuery();
System.out.println(count);
Object[][] rowData = new Object[count][4];
System.out.println(rowData.length);
int i = 0;
while (resultset.next()) {
rowData[i][0] = resultset.getString(1);
rowData[i][1] = resultset.getString(2);
rowData[i][2] = resultset.getString(3);
rowData[i][3] = resultset.getString(4);
i++;
}
// 表头(列名)
Object[] columnNames = {"id", "名字", "科目", "分数"};
// 表格所有行数据
// 创建一个表格,指定 所有行数据 和 表头
JTable table = new JTable(rowData, columnNames);
// 设置表格内容颜色
table.setForeground(Color.BLACK); // 字体颜色
table.setFont(new Font(null, Font.PLAIN, 14)); // 字体样式
table.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色
table.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景
table.setGridColor(Color.GRAY); // 网格颜色
// 设置表头
table.getTableHeader().setFont(new Font(null, Font.BOLD, 14)); // 设置表头名称字体样式
table.getTableHeader().setForeground(Color.RED); // 设置表头名称字体颜色
table.getTableHeader().setResizingAllowed(false); // 设置不允许手动改变列宽
table.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列
table.setEnabled(false);
// 设置行高
table.setRowHeight(30);
// 第一列列宽设置为40
table.getColumnModel().getColumn(0).setPreferredWidth(40);
// 设置滚动面板视口大小(超过该大小的行数据,需要拖动滚动条才能看到)
table.setPreferredScrollableViewportSize(new Dimension(500, 500));
// 把 表格 放到 滚动面板 中(表头将自动添加到滚动面板顶部)
JScrollPane scrollPane = new JScrollPane(table);
// 添加 滚动面板 到 内容面板
panel.add(scrollPane);
JPanel panel1=new JPanel();
JButton seekScore = new JButton("查看成绩");
seekScore.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
giveFun();
JOptionPane.showMessageDialog(
jf,
"该课程的平均分为"+fun,
"avg",
JOptionPane.INFORMATION_MESSAGE
);
}
});
panel1.add(seekScore);
// 设置 内容面板 到 窗口
jf.setContentPane(panel);
jf.add(panel1, BorderLayout.SOUTH);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
} catch (Exception e) {
}
}
public void giveFun(){
try{
String sql = " select avg(score) from score where course=? ";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, course);
ResultSet resultset = pstmt.executeQuery();
if (resultset.next())
fun = resultset.getInt(1);
}catch (Exception e){
}
}
public void returnCourse() {
try {
String sql = " select course from 老师基本信息表 where teacherid=? ";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet resultset = pstmt.executeQuery();
if (resultset.next())
course = resultset.getString(1);
else {
JOptionPane.showMessageDialog(
jf,
"该老师没有选择教任何的课",
"No",
JOptionPane.INFORMATION_MESSAGE);
jf.dispose();
}
} catch (Exception e) {
}
}
public void function2() {
}
public void function3() {
}
public void function4() {
}
}
package view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Teacher_Information {
ResultSet result = null;
private String id;
private String name;
private String pos;
private String course;
private JFrame userview;
private Connection connection;
public JFrame teacherView = new JFrame();
private JPanel panel = new JPanel(new GridLayout());
JTextField textField_tea = new JTextField();
JTextField textField_pos = new JTextField();
JTextField textField_account = new JTextField();
JButton submit = new JButton("提交");
JButton Return = new JButton("返回");
JComboBox comboBox;
int coursenumber;
public Teacher_Information(Connection connection, String id, JFrame userview) {
this.connection = connection;
this.userview = userview;
this.id = id;
init();
}
void init() {
teacherView = new JFrame();
teacherView.setTitle("老师信息查询");
teacherView.setSize(500, 500);
teacherView.setLocationRelativeTo(null);
teacherView.setVisible(true);
GridLayout layout = new GridLayout(5, 2);
JPanel panel = new JPanel(layout);//设置为layout布局
JLabel L_tea = new JLabel("姓名");
JLabel L_pos = new JLabel("职位");
JLabel L_account = new JLabel("账户");
JLabel L_choose = new JLabel("讲授的课程");
textField_account.setEditable(false);
L_tea.setFont(new Font("", Font.BOLD, 22));
L_pos.setFont(new Font("", Font.BOLD, 22));
L_account.setFont(new Font("", Font.BOLD, 22));
L_choose.setFont(new Font("", Font.BOLD, 12));
String[] listData = {"无", "大数据技术", "算法分析与设计", "Kotlin语言", "汇编语言"};
comboBox = new JComboBox(listData);
course=returnCourse();
System.out.println(course);
System.out.println(coursenumber);
coursenumber=returnNumber(course);
comboBox.setSelectedIndex(coursenumber);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
String lesson = (String) comboBox.getSelectedItem();
try {
String sql = "update 老师基本信息表 set course=? where teacherid=?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, lesson);
pstmt.setString(2, id);
pstmt.executeUpdate();
int number=returnNumber(lesson);
coursenumber=number;
} catch (SQLException ex) {
}
}
});
textField_pos.setFont(new Font("", Font.BOLD, 22));
textField_tea.setFont(new Font("", Font.BOLD, 22));
textField_account.setFont(new Font("", Font.BOLD, 22));
submit.setFont(new Font("", Font.BOLD, 22));
Return.setFont(new Font("", Font.BOLD, 22));
panel.add(L_account);
panel.add(textField_account);
panel.add(L_pos);
panel.add(textField_pos);
panel.add(L_tea);
panel.add(textField_tea);
panel.add(L_choose);
panel.add(comboBox);
panel.add(submit);
panel.add(Return);
teacherView.add(panel);
teacherView.setVisible(true);
getInformation();
function();
}
public void getInformation() {
try {
PreparedStatement stmt = null;
String sql = "select * from 老师基本信息表 where teacherid=?";
stmt = connection.prepareStatement(sql);
stmt.setString(1, id);
result = stmt.executeQuery();
System.out.println(result);
if (result.next()) {
textField_account.setText(result.getString(1));
textField_tea.setText(result.getString(3));
textField_pos.setText(result.getString(2));
}
} catch (Exception eee) {
}
}
public int returnNumber(String course){
if(course.equals("汇编语言"))
return 4;
else if(course.equals("大数据技术"))
return 1;
else if(course.equals("算法分析与设计"))
return 2;
else if(course.equals("Kotlin语言"))
return 3;
else
return 0;
}
public String returnCourse(){
try{
String sql = "select course from 老师基本信息表 where teacherid = ?";
System.out.println("1");
PreparedStatement stmt1= connection.prepareStatement(sql);
System.out.println("1");
stmt1.setString(1, id);
System.out.println(id);
ResultSet resultSet= stmt1.executeQuery();
if(resultSet.next()){
return resultSet.getString(1);
}
return "无";
}catch (Exception e){
return "无";
}
}
public void function() {
submit.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
id = textField_account.getText();
name = textField_tea.getText();
pos = textField_pos.getText();
String sql = "update 老师基本信息表 set name=?,position=? where teacherid=?";//
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, pos);
pstmt.setString(3, id);
int count = pstmt.executeUpdate();
if (count == 1) {
JOptionPane.showMessageDialog(teacherView, "提交成功", "Yes", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception ee) {
}
}
});
Return.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
userview.setVisible(true);
teacherView.dispose();
}
});
}
}
链接: https://download.csdn.net/download/qq_21315871/85839034
由于该程序的一切都是在连接数据库的基础上实现的,所以我们组使用了阿里云的云服务器进行配置mysql服务器。(服务器的配置不是本人做就不过多介绍),同时java运行环境啊,服务器的运行环境啊,jdbc包这些就这里就不过多赘述了。
链接: https://download.csdn.net/download/qq_21315871/85839115
该程序还是比较烂的,此博文只是来记录一下自己实验周做过的事情哈哈哈。自己编写的时候也借鉴了很多资料和很多内容。