使用java swing 和 jdbc 技术 管理学生信息
文章目录- 学生成绩管理系统
- 一,总体架构
- 二、controller层的编写
- 三、pojo层的编写
- 四,view层的编写
- 五,数据库的连接
- 六,启动程序
一共五个包
包含select及update两个核心类的编写,以后的按钮绑定的事件会使用其中的函数。
select类如下
package Controller;
import POJO.Student;
import POJO.StudentCourse;
import POJO.inputStudentInfo;
import Utils.DbConnection;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Select {
// 查询执行行数
public int userloginSelect(String sql) {
ResultSet resultSet = DbConnection.query(sql);
int a = 0;
try {
while (resultSet.next()) {
a = resultSet.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
//查询全部的学生
public Object[][] selectStudentAll(){
String sql = "SELECT * FROM student ";
System.out.println(sql);
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
stmt = (PreparedStatement) conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setID(rs.getInt(1));
student.setName(rs.getString(2));
student.setSex(rs.getString(3));
student.setDateOfBirth(rs.getString(4));
student.setStudentID(rs.getString(5));
list.add(student);
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
DbConnection.colse(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
objects[i][0] = list.get(i).getID();
objects[i][1] = list.get(i).getName();
objects[i][2] = list.get(i).getSex();
objects[i][3] = list.get(i).getDateOfBirth();
objects[i][4] = list.get(i).getStudentID();
}
return objects;
}
//根据姓名查询学生信息
public Object[][] selectStudentByName(String name){
String sql = "SELECT * FROM student where name = '"+name+"'";
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
stmt = (PreparedStatement) conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setID(rs.getInt(1));
student.setName(rs.getString(2));
student.setSex(rs.getString(3));
student.setDateOfBirth(rs.getString(4));
student.setStudentID(rs.getString(5));
list.add(student);
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
DbConnection.colse(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
objects[i][0] = list.get(i).getID();
objects[i][1] = list.get(i).getName();
objects[i][2] = list.get(i).getSex();
objects[i][3] = list.get(i).getDateOfBirth();
objects[i][4] = list.get(i).getStudentID();
}
return objects;
}
//查询全部的学生对应的成绩
public Object[][] selectStudentCourseAll(){
String sql = "SELECT * FROM studentcourse ";
System.out.println(sql);
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList<>();
try {
stmt = (PreparedStatement) conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
StudentCourse student = new StudentCourse();
student.setId(rs.getInt(1));
student.setStudentName(rs.getString(2));
student.setCourseName(rs.getString(3));
student.setCourseGrade(rs.getInt(4));
student.setStudentID(rs.getString(5));
list.add(student);
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
DbConnection.colse(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
objects[i][0] = list.get(i).getId();
objects[i][1] = list.get(i).getStudentName();
objects[i][2] = list.get(i).getCourseName();
objects[i][3] = list.get(i).getCourseGrade();
objects[i][4] = list.get(i).getStudentID();
}
return objects;
}
//根据课程查询成绩信息
public Object[][] selectStudentCourseByCourseName(String name){
String sql = "SELECT * FROM studentcourse where course_name = '"+name+"'";
System.out.println(sql);
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList<>();
try {
stmt = (PreparedStatement) conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
StudentCourse student = new StudentCourse();
student.setId(rs.getInt(1));
student.setStudentName(rs.getString(2));
student.setCourseName(rs.getString(3));
student.setCourseGrade(rs.getInt(4));
student.setStudentID(rs.getString(5));
list.add(student);
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
DbConnection.colse(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
Object[][] objects = new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
objects[i][0] = list.get(i).getId();
objects[i][1] = list.get(i).getStudentName();
objects[i][2] = list.get(i).getCourseName();
objects[i][3] = list.get(i).getCourseGrade();
objects[i][4] = list.get(i).getStudentID();
}
return objects;
}
//导出成绩
public void inputInfo() throws IOException {
FileWriter fileWriter = new FileWriter(new File("info.txt"));
String sql = " SELECt course_name,max(course_grade),min(course_grade),avg(course_grade)n" +
"FROM test.studentcoursen" +
"group by course_name;n" +
";";
System.out.println(sql);
Connection conn = DbConnection.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList<>();
try {
stmt = (PreparedStatement) conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
inputStudentInfo info = new inputStudentInfo();
info.setName(rs.getString(1));
info.setMax(rs.getInt(2));
info.setMin(rs.getInt(3));
info.setAvg(rs.getDouble(4));
list.add(info);
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
DbConnection.colse(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
list.forEach(
x->{
try {
fileWriter.write("课程名称:"+x.getName()+"课程最高分"+x.getMax()+"课程最低分:"+x.getMin()+"课程平均分"+x.getAvg()+"n");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
);
fileWriter.close();
}
}
update类的编写
package Controller;
import Utils.DbConnection;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Updata {
// 添加数据
public int addData(String sql) {
Connection conn = DbConnection.getConnection();
// 获取连接
PreparedStatement stmt = null;
ResultSet rs = null;
try {
System.out.println(sql);
stmt = conn.prepareStatement(sql);
int executeUpdate = stmt.executeUpdate();
return executeUpdate;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "执行语句出错n" + e.toString());
e.printStackTrace();
}finally {
try {
DbConnection.colse(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}
}
三、pojo层的编写
对应的数据库表的实体类
管理员类的编写
package POJO;
//管理类
public class Admin {
//数据库对应ID,方便操作
private Integer ID;
//用户名
private String username;
//密码
private String password;
public Admin(Integer ID, String username, String password, String nickName) {
this.ID = ID;
this.username = username;
this.password = password;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
inputstudentinfo类的编写(接收和处理要导出的成绩信息)
package POJO;
public class inputStudentInfo {
private String name;
private Integer max;
private Integer min;
private Double avg;
public inputStudentInfo(String name, Integer max, Integer min, Double avg) {
this.name = name;
this.max = max;
this.min = min;
this.avg = avg;
}
public inputStudentInfo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getMax() {
return max;
}
public void setMax(Integer max) {
this.max = max;
}
public Integer getMin() {
return min;
}
public void setMin(Integer min) {
this.min = min;
}
public Double getAvg() {
return avg;
}
public void setAvg(Double avg) {
this.avg = avg;
}
}
student类的编写
package POJO;
public class Student {
//学生学号,数据库自增
private Integer ID;
//姓名
private String name;
//性别
private String sex;
//出生年月日
private String dateOfBirth;
//学号
private String studentID;
public Student() {
}
public Student(Integer ID, String name, String sex, String dateOfBirth, String studentID) {
this.ID = ID;
this.name = name;
this.sex = sex;
this.dateOfBirth = dateOfBirth;
this.studentID = studentID;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
}
studentcourse类的编写
package POJO;
//学生的课程成绩
public class StudentCourse {
//编号
private Integer id;
//学生名称
private String studentName;
//课程名称
private String courseName;
//课程成绩
private Integer courseGrade;
//学号
private String studentID;
public StudentCourse() {
}
public StudentCourse(Integer id, String studentName, String courseName, Integer courseGrade, String studentID) {
this.id = id;
this.studentName = studentName;
this.courseName = courseName;
this.courseGrade = courseGrade;
this.studentID = studentID;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Integer getCourseGrade() {
return courseGrade;
}
public void setCourseGrade(Integer courseGrade) {
this.courseGrade = courseGrade;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
}
四,view层的编写
AdminView类的编写,负责管理员的选择登陆界面
package View;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AdminView extends JFrame {
public static StudentCourseView studentCourseView = new StudentCourseView();
public static StudentView studentView = new StudentView();
public AdminView(){
super("学生管理系统");
getContentPane().setFont(new Font("宋体", Font.PLAIN, 35));
this.setBounds(0, 0, 900, 400);
this.setLocationRelativeTo(null);// 让窗口在屏幕中间显示
this.setResizable(false);
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 用户单击窗口的关闭按钮时程序执行的操作
JLabel lblxxx = new JLabel("你好,欢迎使用学生管理系统");
lblxxx.setFont(new Font("宋体", Font.PLAIN, 35));
lblxxx.setBounds(44, 35, 726, 91);
getContentPane().add(lblxxx);
JButton button = new JButton("学生信息管理");
button.setFont(new Font("宋体", Font.BOLD, 20));
button.setBounds(10, 192, 300, 100);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
studentView.setVisible(true);
dispose();
}
});
getContentPane().add(button);
JButton button_1 = new JButton("学生成绩管理");
button_1.setFont(new Font("宋体", Font.BOLD, 20));
button_1.setBounds(500, 192, 300, 100);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
studentCourseView.setVisible(true);
dispose();
}
});
getContentPane().add(button_1);
}
}
StudentCourseView类的编写
package View;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AdminView extends JFrame {
public static StudentCourseView studentCourseView = new StudentCourseView();
public static StudentView studentView = new StudentView();
public AdminView(){
super("学生管理系统");
getContentPane().setFont(new Font("宋体", Font.PLAIN, 35));
this.setBounds(0, 0, 900, 400);
this.setLocationRelativeTo(null);// 让窗口在屏幕中间显示
this.setResizable(false);
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 用户单击窗口的关闭按钮时程序执行的操作
JLabel lblxxx = new JLabel("你好,欢迎使用学生管理系统");
lblxxx.setFont(new Font("宋体", Font.PLAIN, 35));
lblxxx.setBounds(44, 35, 726, 91);
getContentPane().add(lblxxx);
JButton button = new JButton("学生信息管理");
button.setFont(new Font("宋体", Font.BOLD, 20));
button.setBounds(10, 192, 300, 100);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
studentView.setVisible(true);
dispose();
}
});
getContentPane().add(button);
JButton button_1 = new JButton("学生成绩管理");
button_1.setFont(new Font("宋体", Font.BOLD, 20));
button_1.setBounds(500, 192, 300, 100);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
studentCourseView.setVisible(true);
dispose();
}
});
getContentPane().add(button_1);
}
}
StudentView类的编写
下面展示一些 内联代码片。
package View;
import Controller.Select;
import Controller.Updata;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.io.IOException;
import java.util.Objects;
public class StudentView extends JFrame {
Select select = new Select();
Updata updata = new Updata();
private JTable table;
DefaultTableModel df;
Object[][] data = null;
String[] header = new String[] { "数据编号","学生姓名" ,"性别","出生年月日","学号" };
private JTextField searchid;
private JTextField ID;
private JTextField name;
private JTextField sex;
private JTextField dateOfBirth;
private JTextField studentID;
public StudentView(){
super("学生信息管理");
data = select.selectStudentAll();
this.setBounds(0, 0, 935, 700);
this.setLocationRelativeTo(null);// 让窗口在屏幕中间显示
this.setResizable(false);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(27, 321, 876, 331);
getContentPane().add(scrollPane);
//数据版
table = new JTable();
df = new DefaultTableModel(data,header);
table.setModel(df);
scrollPane.setViewportView(table);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// 处理鼠标释放
}
@Override
public void mousePressed(MouseEvent e) {
// 处理鼠标按下
}
@Override
public void mouseExited(MouseEvent e) {
// 处理鼠标离开
}
@Override
public void mouseEntered(MouseEvent e) {
// 处理鼠标移入
}
public void mouseClicked(MouseEvent e) {
// 处理鼠标点击
ID.setText(table.getValueAt(table.getSelectedRow(),0).toString());
name.setText(table.getValueAt(table.getSelectedRow(),1).toString());
sex.setText(table.getValueAt(table.getSelectedRow(),2).toString());
dateOfBirth.setText(table.getValueAt(table.getSelectedRow(),3).toString());
studentID.setText(table.getValueAt(table.getSelectedRow(),4).toString());
}
});
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "学生信息", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(27, 27, 876, 200);
getContentPane().add(panel);
panel.setLayout(null);
JLabel label = new JLabel("数据编号:");
label.setBounds(32, 30, 75, 18);
panel.add(label);
ID = new JTextField();
ID.setBounds(112, 26, 171, 24);
panel.add(ID);
ID.setEditable(false);
ID.setColumns(10);
JLabel label_1 = new JLabel("学生姓名:");
label_1.setBounds(300, 30, 75, 18);
panel.add(label_1);
name = new JTextField();
name.setBounds(400, 26, 171, 24);
panel.add(name);
name.setColumns(10);
JLabel label_1_1 = new JLabel("学生性别:");
label_1_1.setBounds(32, 60, 60, 18);
panel.add(label_1_1);
sex = new JTextField();
sex.setColumns(10);
sex.setBounds(112, 60, 171,24);
panel.add(sex);
JLabel label_1_1_1 = new JLabel("出生年月:");
label_1_1_1.setBounds(300, 60, 75, 18);
panel.add(label_1_1_1);
dateOfBirth = new JTextField();
dateOfBirth.setBounds(400, 60, 171, 24);
panel.add(dateOfBirth);
dateOfBirth.setColumns(10);
JLabel label_1_1_1_1 = new JLabel("学生学号:");
label_1_1_1_1.setBounds(32, 90, 60, 18);
panel.add(label_1_1_1_1);
studentID = new JTextField();
studentID.setColumns(10);
studentID.setBounds(112, 90, 171,24);
panel.add(studentID);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "条件查询", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(27, 240, 531, 68);
getContentPane().add(panel_1);
JLabel label_5 = new JLabel("学生姓名:");
panel_1.add(label_5);
searchid = new JTextField();
panel_1.add(searchid);
searchid.setColumns(10);
JButton searchBuut = new JButton("查询");
panel_1.add(searchBuut);
searchBuut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理鼠标点击
name.setText("");
ID.setText("");
dateOfBirth.setText("");
sex.setText("");
studentID.setText("");
String name = searchid.getText();
Object[][] data = select.selectStudentByName(name);
df.setDataVector(data, header);
}
});
JButton upBuut = new JButton("重置");
panel_1.add(upBuut);
upBuut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理鼠标点击
name.setText("");
ID.setText("");
sex.setText("");
dateOfBirth.setText("");
searchid.setText("");
studentID.setText("");
Object[][] data = select.selectStudentAll();
df.setDataVector(data, header);
}
});
searchBuut.setIcon(new ImageIcon(Objects.requireNonNull(StudentView.class.getResource(""))));
JButton addButt = new JButton("添加");
addButt.setBounds(700, 265, 80, 27);
getContentPane().add(addButt);
addButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sName = name.getText();
String sSex = sex.getText();
String sDateOfBirth = dateOfBirth.getText();
String sstringID = studentID.getText();
String sql = "INSERT INTO student VALUES(null,'" + sName + "','"+sSex+"','" + sDateOfBirth+ "','" + sstringID+ "')";
int reselt = updata.addData(sql);
if (reselt > 0) {
JOptionPane.showMessageDialog(null, "添加学生信息成功!");
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
refresh();
}
});
JButton updataButt = new JButton("修改");
updataButt.setBounds(559, 265, 100, 27);
getContentPane().add(updataButt);
updataButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sName = name.getText();
String sSex = sex.getText();
String sDateOfBirth = dateOfBirth.getText();
String id = ID.getText();
String sstringID= studentID.getText();
if (sName.equals("") && sSex.equals("") && sDateOfBirth.equals("")) {
JOptionPane.showMessageDialog(null, "请填写完整信息!");
} else {
String sql = "UPDATE student SET `name`='"+sName+"',sex='"+sSex+"',`dateOfBirth`='"+sDateOfBirth+"',`school_id`='"+sstringID+"' WHERe id='"+id+"'";
int reselt = updata.addData(sql);
if (reselt > 0) {
JOptionPane.showMessageDialog(null, "修改信息成功!");
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
}
refresh();
}
});
JButton button_3 = new JButton("删除");
button_3.setBounds(800, 265, 100, 27);
getContentPane().add(button_3);
button_3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (table.getSelectedColumn() < 0) {
JOptionPane.showMessageDialog(null, "请选中要删除的数据!");
} else {
String text = ID.getText();
String dsql = "delete from student where id=" + text;
int result = updata.addData(dsql);
if (result > 0) {
JOptionPane.showMessageDialog(null, "删除成功!");
Object[][] data = select.selectStudentAll();
df.setDataVector(data, header);
} else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
new AdminView().setVisible(true);
}
});
}
public void refresh(){
name.setText("");
ID.setText("");
sex.setText("");
dateOfBirth.setText("");
searchid.setText("");
studentID.setText("");
Object[][] data = select.selectStudentAll();
df.setDataVector(data, header);
}
}
UserLogin类的编写
下面展示一些 内联代码片。
package View;
import Controller.Select;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserLogin extends JFrame{
String uid;
private static JTextField username;
private JTextField password;
JCheckBox chckbxNewCheckBox;
public UserLogin() {
super("学生管理系统后台登陆");
Select select = new Select();
this.setBounds(0, 0, 600, 350);
this.setLocationRelativeTo(null);// 让窗口在屏幕中间显示
this.setResizable(false);// 让窗口大小不可改变
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 用户单击窗口的关闭按钮时程序执行的操作
this.setIconImage(Toolkit.getDefaultToolkit().getImage(UserLogin.class.getResource("")));
JLabel usernameText = new JLabel("账号:");
usernameText.setBounds(142, 70, 72, 18);
usernameText.setIcon(new ImageIcon(UserLogin.class.getResource("")));
getContentPane().add(usernameText);
username = new JTextField();
username.setBounds(228, 67, 203, 24);
getContentPane().add(username);
username.setColumns(10);
JLabel passwordText = new JLabel("密码:");
passwordText.setIcon(new ImageIcon(UserLogin.class.getResource("")));
passwordText.setBounds(142, 138, 72, 18);
getContentPane().add(passwordText);
password = new JPasswordField();
password.setColumns(10);
password.setBounds(228, 135, 203, 24);
getContentPane().add(password);
JButton logButton = new JButton("登录");
logButton.setIcon(new ImageIcon(UserLogin.class.getResource("")));
logButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String uname = username.getText();
String pwd = password.getText();
if (uname.equals("") && pwd.equals("")) {
JOptionPane.showMessageDialog(null, "账户名或密码未填写!");
} else {
//核心判断语句
String sql = "select COUNT(*) from admin where username='" + uname + "' and password='" + pwd
+ "'";
int reselt = select.userloginSelect(sql);
if (reselt > 0) {
JOptionPane.showMessageDialog(null, "登录成功!欢迎使用!");
new AdminView().setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(null, "登录失败!账户名或密码不正确!请重新输入!");
}
}
}
});
logButton.setBounds(318, 228, 113, 27);
getContentPane().add(logButton);
JButton regButton = new JButton("重置");
regButton.setIcon(new ImageIcon(UserLogin.class.getResource("")));
regButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username.setText("");
password.setText("");
}
});
regButton.setBounds(142, 228, 113, 27);
getContentPane().add(regButton);
}
// public static String getUsername() {
// if (username.getText().equals("")) {
// return username.getText();
// }
// return "系统管理员";
// }
public static void main(String[] args) {
new UserLogin().setVisible(true);
}
}
五,数据库的连接
utils包下的DbConnection类代码如下
package Utils;
import javax.swing.*;
import java.sql.*;
public class DbConnection {
// 驱动类的类名
private static final String DRIVERNAME = "com.mysql.cj.jdbc.Driver";
// 连接数据的URL路径
private static final String URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
// 数据库登录账号
private static final String USER = "root";
// 数据库登录密码
private static final String PASSWORD = "yuyue123";
// 加载驱动
static {
try {
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() {
try {
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// 查询
public static ResultSet query(String sql) {
System.out.println(sql);
// 获取连接
Connection connection = getConnection();
PreparedStatement psd;
try {
psd = connection.prepareStatement(sql);
return psd.executeQuery();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "执行语句出错n" + e.toString());
e.printStackTrace();
}
return null;
}
// 增、删、改、查
public static int updataInfo(String sql) {
System.out.println(sql);
// 获取连接
Connection connection = getConnection();
try {
PreparedStatement psd = connection.prepareStatement(sql);
return psd.executeUpdate();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "执行语句出错n" + e.toString());
e.printStackTrace();
}
return 0;
}
// 关闭连接
public static void colse(ResultSet rs, PreparedStatement stmt, Connection conn) throws Exception {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.cancel();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
}
public static void main(String[] args) {
}
}
六,启动程序
package Main;
import View.UserLogin;
public class DoMain {
public static void main(String[] args) {
new UserLogin().setVisible(true);
}
}
写的比较草率,等课设答辩完会仔细修稿。



