开发语言:java; 开发环境:Mysql, java; 开发工具:eclipse
基于Java swing+MySQL实现学生信息管理系统:主要实现JDBC对学生信息进行增删改查。
1.Student类(学生的基本信息)
package com.zhang.Student;
public class Student {
public String num;//学号
public String name;//姓名
public String grade;//成绩
public Student(String num,String name,String grade ) {
this.num = num;
this.name = name;
this.grade = grade;
}
}
2.StudentJFrame类(简单的图形界面–真丑)
package com.zhang.Student;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class StudentJFrame {
public void setJFrame(){
/*
* 设置窗口基本样式
*/
JFrame jframe = new JFrame("学生管理系统");
jframe.setSize(500, 382);
jframe.setLocation(100, 100);
jframe.setLayout(null);
/*
* 创建容器增加按钮
*/
Container c = jframe.getContentPane();
JButton b =new JButton("增加");
JButton b1 =new JButton("删除");
JButton b2 =new JButton("修改");
JButton b3 =new JButton("查看");
ImageIcon image = new ImageIcon("images/school.jpg");
final JLabel l = new JLabel();
l.setIcon(image);
l.setBounds(0,0,image.getIconWidth(),image.getIconHeight());
JLabel l1 = new JLabel("欢迎来到学生管理系统");
l1.setBounds(100, 0, 400, 180);
Font font = new Font("楷体",Font.BOLD,30);
l1.setFont(font);
b.setBounds(120, 250, 60, 30);
b1.setBounds(190, 250, 60, 30);
b2.setBounds(260, 250, 60, 30);
b3.setBounds(330, 250, 60, 30);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new StudentJDialogAdd(jframe).setVisible(true);;
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StudentJDialogDelete(jframe).setVisible(true);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StudentJDialogUpdate(jframe).setVisible(true);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StudentJDialogSelect(jframe).setVisible(true);
}
});
c.add(b);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(l1);
c.add(l);
/*
* 设置窗口可见与关闭方式
*/
jframe.setVisible(true);
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
StudentJFrame f =new StudentJFrame();
f.setJFrame();
}
}
3.StudentDAO接口(实现JDBC对数据库进行增删改查)
package com.zhang.Student;
public interface StudentDAO {
public void add(Student student);
public void delete(Student student);
public void update(Student student);
public void select(Student student);
}
4.StudentJDBC类(连接数据库对学生信息进行增删改查)
package com.zhang.Student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class StudentJDBC implements StudentDAO{
public StudentJDBC() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException {
System.out.println("数据库连接成功");
return DriverManager.getConnection("jdbc:mysql://localhost/student","root","123456");
}
public void add(Student student) {
String sql = "insert into student values(?,?,?)";
try(Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql))
{
ps.setString(1, student.num);
ps.setString(2, student.name);
ps.setString(3, student.grade);
ps.execute();
System.out.println("数据添加成功");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void delete(Student student) {
}
@Override
public void update(Student student) {
// TODO Auto-generated method stub
}
@Override
public void select(Student student) {
}
}
5.数据库信息
6.StudentJDialogAdd类(连接数据库增加学生信息)
package com.zhang.Student;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class StudentJDialogAdd extends JDialog{
public static int rowCount=0;
public int columnCount = 3;;//设置初始行与列
String num[] = new String[100];
String name[] = new String[100];
String grade[] = new String[100];
public String[][] newtableValues = new String[100][3];
public StudentJDialogAdd(JFrame jframe) {
/*
* 设置简单的窗口格式
*/
super(jframe,"增加",true);
setBounds(200, 200, 400, 400);
Container c =getContentPane();
c.setLayout(null);
/*
* 设置表格内容
*/
String columnNames[] = new String[]{"学号","姓名","成绩"};
String tableValues[][] = new String[0][3];
DefaultTableModel tablemodel = new DefaultTableModel(tableValues,columnNames);
JTable table = new JTable(tablemodel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JButton b= new JButton("增加");
JButton b1= new JButton("删除");
JButton b2= new JButton("保存");
b.setBounds(100, 200, 60, 30);
b1.setBounds(170, 200, 60, 30);
b2.setBounds(240, 200, 60, 30);
c.add(b);
c.add(b1);
c.add(b2);
JScrollPane js = new JScrollPane(table);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
js.setVisible(true);
String []newdata = {""};
tablemodel.addRow(newdata);
rowCount++;
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if(selectedRow!=-1) {
tablemodel.removeRow(selectedRow);
rowCount--;
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newtableValues[][] = new String[rowCount][3];
for(int i=0; i
7.StudentJDialogDelete类(连接数据库删除学生信息)
package com.zhang.Student;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class StudentJDialogDelete extends JDialog{
int i=0,j=0;
int count=0;
int selectedRow;
String selected;
public StudentJDialogDelete(JFrame jframe) {
super(jframe,"删除",true);
setBounds(200, 200, 400, 400);
Container c =getContentPane();
c.setLayout(null);
String columnNames[] = new String[]{"学号","姓名","成绩"};
String tableValues[][] =new String[100][3];
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
try(Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/student","root","123456");
Statement s = connection.createStatement();
)
{
String sql = "select num from student";
String sql1 = "select name from student";
String sql2 = "select grade from student";
String sql3 = "select count(num) from student";
ResultSet rs =s.executeQuery(sql);
while(rs.next()) {
tableValues[i][0] = rs.getString("num");
i++;
}
i=0;
ResultSet rs1 =s.executeQuery(sql1);
while(rs1.next()) {
tableValues[i][1] = rs1.getString("name");
i++;
}
i=0;
ResultSet rs2 =s.executeQuery(sql2);
while(rs2.next()) {
tableValues[i][2] = rs2.getString("grade");
i++;
}
ResultSet rs3=s.executeQuery(sql3);
while(rs3.next()) {
count = rs3.getInt("count(num)");
}
} catch (SQLException e) {
// TODO: handle exception
}
String newtableValues[][] = new String[count+1][3];
for(i=0 ;i
8.StudentJDialogUpdate(连接数据库修改学生信息)
package com.zhang.Student;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class StudentJDialogUpdate extends JDialog{
int i=0,j=0;
int count=0;
int selectedRow;
int selectColumn;
String selected;
String selectednum;
public StudentJDialogUpdate(JFrame jframe){
super(jframe,"修改",true);
setBounds(200, 200, 400, 400);
Container c =getContentPane();
c.setLayout(null);
String columnNames[] = new String[]{"学号","姓名","成绩"};
String tableValues[][] =new String[100][3];
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
try(Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/student","root","123456");
Statement s = connection.createStatement();
)
{
String sql = "select num from student";
String sql1 = "select name from student";
String sql2 = "select grade from student";
String sql3 = "select count(num) from student";
ResultSet rs =s.executeQuery(sql);
while(rs.next()) {
tableValues[i][0] = rs.getString("num");
i++;
}
i=0;
ResultSet rs1 =s.executeQuery(sql1);
while(rs1.next()) {
tableValues[i][1] = rs1.getString("name");
i++;
}
i=0;
ResultSet rs2 =s.executeQuery(sql2);
while(rs2.next()) {
tableValues[i][2] = rs2.getString("grade");
i++;
}
ResultSet rs3=s.executeQuery(sql3);
while(rs3.next()) {
count = rs3.getInt("count(num)");
}
} catch (SQLException e) {
// TODO: handle exception
}
String newtableValues[][] = new String[count+1][3];
for(i=0 ;i
9.StudentJDialogSelect(连接数据库查看学生信息)
package com.zhang.Student;
import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class StudentJDialogSelect extends JDialog{
int i=0,j=0;
int count=0;
int selectedRow;
String selected;
public StudentJDialogSelect(JFrame jframe){
super(jframe,"查看",true);
setBounds(200, 200, 400, 400);
Container c =getContentPane();
c.setLayout(null);
String columnNames[] = new String[]{"学号","姓名","成绩"};
String tableValues[][] =new String[100][3];
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
try(Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/student","root","123456");
Statement s = connection.createStatement();
)
{
String sql = "select num from student";
String sql1 = "select name from student";
String sql2 = "select grade from student";
String sql3 = "select count(num) from student";
ResultSet rs =s.executeQuery(sql);
while(rs.next()) {
tableValues[i][0] = rs.getString("num");
i++;
}
i=0;
ResultSet rs1 =s.executeQuery(sql1);
while(rs1.next()) {
tableValues[i][1] = rs1.getString("name");
i++;
}
i=0;
ResultSet rs2 =s.executeQuery(sql2);
while(rs2.next()) {
tableValues[i][2] = rs2.getString("grade");
i++;
}
ResultSet rs3=s.executeQuery(sql3);
while(rs3.next()) {
count = rs3.getInt("count(num)");
}
} catch (SQLException e) {
// TODO: handle exception
}
String newtableValues[][] = new String[count+1][3];
for(i=0 ;i