今天做了一个1800行代码的学生成绩管理系统,主要实现增删改查等操作,还大部分功能尚未实现,这个项目是基于java+swing+jdbc+mysql的融合,主要对学习过的知识进行综合的复习,如果项目中有bug的地方,可以在评论区留言,在此谢过。
该登陆只能管理员登陆,当用户输入账号和密码时,会与数据库中的信息进行对比,如果信息无误,销毁登陆对话框,显示主窗体,如果输入错误,提示信息并则清空文本框,继续输入。
此功能为修改窗体大小,背景颜色,位置,标题,没有提供代码,留给感兴趣的靓仔实现
package com.student.frame;
import com.student.Data.Dao;
import com.student.modal.User;
import com.student.util.StringUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class Login extends JDialog {
private JTextField userField;
private JPasswordField passwordField;
public Login() {
try {
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
setTitle("学生成绩管理系统");
setResizable(false);
setSize( 545, 381);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
JLabel label = new JLabel("用户登录");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Dialog", Font.BOLD, 30));
label.setBounds(12, 0, 521, 85);
getContentPane().add(label);
JLabel label_1 = new JLabel("账 号:");
label_1.setIcon(new ImageIcon(Login.class.getResource("/icon/user4.png")));
label_1.setFont(new Font("Dialog", Font.BOLD, 15));
label_1.setBounds(92, 166, 98, 32);
getContentPane().add(label_1);
JLabel label_2 = new JLabel("密 码:");
label_2.setIcon(new ImageIcon(Login.class.getResource("/icon/psw3.png")));
label_2.setFont(new Font("Dialog", Font.BOLD, 15));
label_2.setBounds(92, 210, 98, 32);
getContentPane().add(label_2);
userField = new JTextField();
userField.setBounds(191, 173, 240, 22);
getContentPane().add(userField);
userField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setEchoChar('*');
passwordField.setBounds(191, 218, 240, 22);
getContentPane().add(passwordField);
JButton ok = new JButton(" ");
//登陆事件
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(StringUtil.isEmpy(userField.getText())){
JOptionPane.showMessageDialog(null, "账号不能为空");
return;
}
else if(StringUtil.isEmpy(new String(passwordField.getPassword()))){
JOptionPane.showMessageDialog(null, "密码不能为空");
return;
}
else {
User u=new User();
u.setUser(Double.parseDouble(userField.getText()));
u.setPassword(new String(passwordField.getPassword()));
try {
User user = Dao.checkLogin(u);
if(user!=null) {
JOptionPane.showMessageDialog(null, "登陆成功");
dispose();
new MainWindow().setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "账号或密码有误");
userField.setText("");
passwordField.setText("");
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
ok.setIcon(new ImageIcon(Login.class.getResource("/icon/exit2.png")));
ok.setBounds(155, 285, 73, 32);
getContentPane().add(ok);
JButton cancel = new JButton(" ");
//退出系统事件
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if(e.getKeyChar()=='\n') {
ok.doClick(10);
}
}
});
cancel.setIcon(new ImageIcon(Login.class.getResource("/icon/exite1.png")));
cancel.setBounds(358, 285, 73, 32);
getContentPane().add(cancel);
JLabel lblNewLabel = new JLabel("登陆背景");
lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/iimages/timg2.jpeg")));
lblNewLabel.setBounds(0, -54, 545, 405);
getContentPane().add(lblNewLabel);
}
}
package com.student.frame;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
public class ThemeFrame extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
public ThemeFrame() {
setModal(true);
setResizable(false);
try {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
setTitle("主题修改");
setSize(423, 416);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
JComboBox backgroundBox = new JComboBox();
JLabel background = new JLabel("窗口背景:");
background.setFont(new Font("Dialog", Font.BOLD, 15));
JLabel title = new JLabel("标 题:");
title.setFont(new Font("Dialog", Font.BOLD, 15));
textField = new JTextField();
textField.setColumns(15);
JLabel label = new JLabel("窗口大小:");
label.setFont(new Font("Dialog", Font.BOLD, 15));
textField_1 = new JTextField();
textField_1.setText(" ");
textField_1.setColumns(5);
JLabel lblNewLabel_2 = new JLabel("X:");
JLabel lblY = new JLabel("Y:");
textField_2 = new JTextField();
textField_2.setText(" ");
textField_2.setColumns(5);
JLabel label_1 = new JLabel("窗口位置:");
label_1.setFont(new Font("Dialog", Font.BOLD, 15));
JLabel lblX = new JLabel("X:");
textField_3 = new JTextField();
textField_3.setText(" ");
textField_3.setColumns(5);
JLabel lblY_1 = new JLabel("Y:");
textField_4 = new JTextField();
textField_4.setText(" ");
textField_4.setColumns(5);
JButton apllyAndClose = new JButton("应用和关闭");
apllyAndClose.setIcon(new ImageIcon(ThemeFrame.class.getResource("/icon/应用.png")));
/**
* 主题修改单机事件
*
*/
apllyAndClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
JButton Default = new JButton("默认");
Default.setIcon(new ImageIcon(ThemeFrame.class.getResource("/icon/默认.png")));
GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(background)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(backgroundBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(283, Short.MAX_VALUE))
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addComponent(lblY)
.addComponent(lblNewLabel_2))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(31)
.addComponent(label_1))
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addComponent(lblX)
.addComponent(lblY_1))
.addGap(3)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(36))
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(title)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(Default)
.addPreferredGap(ComponentPlacement.RELATED, 237, Short.MAX_VALUE)
.addComponent(apllyAndClose)
.addContainerGap())))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addGap(34)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(background)
.addComponent(backgroundBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(title)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(27)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(lblNewLabel_2)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1)
.addComponent(lblX)
.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblY)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGroup(gl_contentPanel.createSequentialGroup()
.addGap(25)
.addComponent(lblY_1))
.addGroup(gl_contentPanel.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(apllyAndClose)
.addComponent(Default))
.addGap(38))
);
contentPanel.setLayout(gl_contentPanel);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
}
package com.student.modal;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class InternetConfigFrame extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
public InternetConfigFrame() {
try {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
setResizable(false);
setModal(true);
setTitle("MySQL 新建连接");
setSize(591, 556);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBorder(new LineBorder(Color.LIGHT_GRAY, 5));
JLabel label_2 = new JLabel("连接名:");
textField = new JTextField();
textField.setColumns(30);
JLabel label_3 = new JLabel("主机:");
JLabel lblNewLabel_5 = new JLabel("端口");
JLabel label_4 = new JLabel("用户名:");
JLabel label_5 = new JLabel("密码:");
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setColumns(10);
JCheckBox chckbxNewCheckBox = new JCheckBox("保存密码");
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(Color.WHITE, 2));
GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addGap(22)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 539, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addComponent(label_2)
.addComponent(label_3)
.addComponent(lblNewLabel_5)
.addComponent(label_4)
.addComponent(label_5))
.addGap(75)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
.addComponent(textField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
.addComponent(textField_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
.addComponent(textField_2, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(Alignment.LEADING, gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
.addComponent(textField_4, Alignment.LEADING)
.addComponent(textField_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
.addComponent(chckbxNewCheckBox, Alignment.LEADING)))
.addGap(58))))
.addGroup(gl_contentPanel.createSequentialGroup()
.addContainerGap()
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 569, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addGap(38)
.addComponent(label_2))
.addGroup(gl_contentPanel.createSequentialGroup()
.addGap(26)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(26)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(29)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_5)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(26)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_4)
.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_5)
.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(chckbxNewCheckBox)
.addGap(26)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
JButton button = new JButton("测试连接");
button.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/测试 检测.png")));
JButton button_1 = new JButton("确定");
button_1.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/应用.png")));
JButton button_2 = new JButton("取消");
button_2.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/取消.png")));
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();//销毁对话框
}
});
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(button, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 277, Short.MAX_VALUE)
.addComponent(button_1)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button_2)
.addGap(5))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
.addContainerGap(18, Short.MAX_VALUE)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addComponent(button)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(button_1)
.addComponent(button_2)))
.addContainerGap())
);
panel_1.setLayout(gl_panel_1);
JLabel lblNewLabel = new JLabel("Navicat");
JLabel lblNewLabel_1 = new JLabel(" ");
lblNewLabel_1.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/连接.png")));
JLabel lblNewLabel_2 = new JLabel("数据库");
JLabel lblNewLabel_3 = new JLabel(" ");
lblNewLabel_3.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/数据库.png")));
JLabel lblNewLabel_4 = new JLabel(" ");
lblNewLabel_4.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/直线_图形对象_jurassic.png")));
JLabel label = new JLabel(" ");
label.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/直线_图形对象_jurassic.png")));
JLabel label_1 = new JLabel(" ");
label_1.setIcon(new ImageIcon(InternetConfigFrame.class.getResource("/icon/直线_图形对象_jurassic.png")));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(150)
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addComponent(lblNewLabel)
.addComponent(lblNewLabel_1))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblNewLabel_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel_3)
.addComponent(lblNewLabel_2))
.addGap(152))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addContainerGap(19, Short.MAX_VALUE)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_3)
.addComponent(lblNewLabel_1))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(lblNewLabel_2))
.addGap(27))
.addGroup(gl_panel.createSequentialGroup()
.addGap(44)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_4, GroupLayout.PREFERRED_SIZE, 21, Short.MAX_VALUE)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
panel.setLayout(gl_panel);
contentPanel.setLayout(gl_contentPanel);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("常规");
menuBar.add(menu);
}
}
package com.student.frame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.student.Data.Dao;
import com.student.modal.InternetConfigFrame;
import com.student.modal.StudentData;
import com.student.util.StringUtil;
import javax.swing.JToolBar;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainWindow extends JFrame {
/**
* 表单
*/
private static JTable table;
/**
* 文本域
*/
private JTextField stuField;
private JTextField nameField;
private JTextField ageField;
private JTextField mathField;
private JTextField javaField;
private JTextField cField;
private JTextField englishField;
private JRadioButton radioMan;
private JRadioButton radioFemal;
private final ButtonGroup buttonGroup = new ButtonGroup();
public static void main(String[] args) {
try {
MainWindow frame = new MainWindow();
frame.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
public MainWindow() {
new Login();
setFont(new Font("Dialog", Font.PLAIN, 18));
setTitle("学生成绩管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
setLocationRelativeTo(null);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("基本信息");
/**
*
* 主页监听事件
*
*/
menu.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/管理(2).png")));
menuBar.add(menu);
JMenu menu_1 = new JMenu("设置");
menu_1.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/设置.png")));
menuBar.add(menu_1);
JMenuItem menuItem = new JMenuItem("主题");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new ThemeFrame().setVisible(true);
}
});
menuItem.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/界面设计.png")));
menu_1.add(menuItem);
JMenuItem menuItem_2 = new JMenuItem("网络配置");
/**
*
* 网络配置单机事件
*
*/
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new InternetConfigFrame().setVisible(true);
}
});
menuItem_2.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/网络.png")));
menu_1.add(menuItem_2);
JMenuItem menuItem_1 = new JMenuItem("退出系统");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
menuItem_1.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/退出.png")));
menu_1.add(menuItem_1);
JButton button = new JButton("刷新");
button.setForeground(Color.BLACK);
button.setBackground(UIManager.getColor("Button.background"));
/**
*
* 刷新表格
*
*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
fillTable(Dao.selectData());
} catch (SQLException e) {
e.printStackTrace();
}
}
});
button.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/刷新.png")));
menuBar.add(button);
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) panel.getLayout();
flowLayout_1.setVgap(70);
flowLayout_1.setHgap(0);
getContentPane().add(panel, BorderLayout.SOUTH);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
getContentPane().add(panel_1, BorderLayout.WEST);
JLabel stuText = new JLabel("学号:");
stuField = new JTextField();
/**
*
* 学号输入显示信息
*
*/
stuField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
searchData();
}
});
stuField.setColumns(10);
JLabel nameText = new JLabel("姓名:");
nameField = new JTextField();
nameField.setColumns(10);
JLabel ageText = new JLabel("年龄:");
ageField = new JTextField();
ageField.setColumns(10);
JLabel label = new JLabel("性别:");
radioMan = new JRadioButton("男");
radioMan.setSelected(true);
buttonGroup.add(radioMan);
radioFemal = new JRadioButton("女");
buttonGroup.add(radioFemal);
JLabel label_1 = new JLabel("高数:");
mathField = new JTextField();
mathField.setColumns(10);
JLabel lblJava = new JLabel("java:");
javaField = new JTextField();
javaField.setColumns(10);
JLabel lblC = new JLabel("c语言:");
cField = new JTextField();
cField.setColumns(10);
JLabel label_2 = new JLabel("英语:");
englishField = new JTextField();
englishField.setColumns(10);
JButton button_1 = new JButton("修改");
/**
*
* 修改信息事件
*
*/
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
StudentData stu=new StudentData();
if(StringUtil.isEmpy(stuField.getText())) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
}
else if(StringUtil.isEmpy(nameField.getText())) {
JOptionPane.showMessageDialog(null, "姓名不能为空!");
}
else if(StringUtil.isEmpy(ageField.getText())) {
JOptionPane.showMessageDialog(null, "年龄不能为空!");
}
else {
stu.setNum(Integer.parseInt(stuField.getText()));
stu.setName(nameField.getText());
stu.setAge(Integer.parseInt(ageField.getText()));
if(radioMan.isSelected()) {
stu.setSex("男");
}else {
stu.setSex("女");
}
stu.setNum(Integer.parseInt(stuField.getText()));
stu.setMath(Float.parseFloat(mathField.getText()));
stu.setJava(Float.parseFloat(javaField.getText()));
stu.setC(Float.parseFloat(cField.getText()));
stu.setEnglish(Float.parseFloat(englishField.getText()));
try {
if(Dao.updateData(stu)>0) {
JOptionPane.showMessageDialog(null, "修改成功!");
fillTable(Dao.selectData());
}
else {
JOptionPane.showMessageDialog(null, "修改失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
button_1.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/修改(2).png")));
JButton button_2 = new JButton("添加");
/**
*
* 添加信息事件
*
*/
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(StringUtil.isEmpy(stuField.getText())) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
}
else if(StringUtil.isEmpy(nameField.getText())) {
JOptionPane.showMessageDialog(null, "姓名不能为空!");
}
else if(StringUtil.isEmpy(ageField.getText())) {
JOptionPane.showMessageDialog(null, "年龄不能为空!");
}else {
StudentData stu=new StudentData();
stu.setNum(Integer.parseInt(stuField.getText()));
stu.setName(nameField.getText());
stu.setAge(Integer.parseInt(ageField.getText()));
if(radioMan.isSelected()) {
stu.setSex("男");
}else {
stu.setSex("女");
}
stu.setMath(Float.parseFloat(mathField.getText()));
stu.setJava(Float.parseFloat(javaField.getText()));
stu.setC(Float.parseFloat(cField.getText()));
stu.setEnglish(Float.parseFloat(englishField.getText()));
try {
if(JOptionPane.showConfirmDialog(null, "确定添加!")==0) {
if(Dao.addData(stu)==1) {
JOptionPane.showMessageDialog(null, "添加成功!");
fillTable(Dao.selectData());//刷新表单
}
else {
JOptionPane.showMessageDialog(null, "添加失败!");
return;
}
}else {
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
button_2.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/添加.png")));
JButton button_3 = new JButton("删除");
/**
*
* 删除鼠标单机事件
*
*/
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
StudentData stu=new StudentData();
if(StringUtil.isEmpy(stuField.getText())) {
JOptionPane.showMessageDialog(null, "请输入你要删除的学号!");
}else {
stu.setNum(Integer.parseInt(stuField.getText()));
try {
if(JOptionPane.showConfirmDialog(null, "确定要删除")==0) {
if(Dao.deleteData(stu)>1) {
JOptionPane.showMessageDialog(null, "删除成功!");
clearText();
fillTable(Dao.selectData());
clearText();
}else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
}else {
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
button_3.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/删 除 (1).png")));
JButton button_4 = new JButton("搜索");
/**
*
* 搜索点击事件
*
*/
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
searchData();
}
});
button_4.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/查找(1).png")));
JButton button_5 = new JButton("清空");
/**
*
* 清空表单事件
*
*/
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clearText();
}
});
button_5.setIcon(new ImageIcon(MainWindow.class.getResource("/icon/清空.png")));
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(button_1)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(lblJava)
.addComponent(lblC)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addComponent(label_1)
.addComponent(label)))
.addComponent(label_2)
.addComponent(ageText)
.addComponent(nameText)
.addComponent(stuText))
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(javaField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(18)
.addComponent(radioMan)
.addGap(18)
.addComponent(radioFemal))
.addGroup(gl_panel_1.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(englishField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button_2, Alignment.TRAILING)))
.addGroup(gl_panel_1.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(stuField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(mathField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(nameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(ageField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
.addGroup(gl_panel_1.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(button_5)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(button_3)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button_4)))))
.addGap(18))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(stuText)
.addComponent(stuField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(nameText)
.addComponent(nameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(ageText)
.addComponent(ageField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(8)
.addComponent(label))
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(radioMan)
.addComponent(radioFemal)))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(mathField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(lblJava)
.addComponent(javaField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(lblC)
.addComponent(cField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(label_2)
.addComponent(englishField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(button_1)
.addComponent(button_2))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(button_3)
.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
.addComponent(button_5))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(button_4)
.addContainerGap())))
);
panel_1.setLayout(gl_panel_1);
JPanel panel_2 = new JPanel();
FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
flowLayout_2.setHgap(0);
flowLayout_2.setVgap(60);
getContentPane().add(panel_2, BorderLayout.NORTH);
JPanel panel_3 = new JPanel();
FlowLayout flowLayout_3 = (FlowLayout) panel_3.getLayout();
flowLayout_3.setHgap(120);
flowLayout_3.setVgap(50);
getContentPane().add(panel_3, BorderLayout.EAST);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable();
/**
*
* 表单选中事件
*
*/
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row=table.getSelectedRow();
stuField.setText(String.valueOf(table.getValueAt(row, 1)));
nameField.setText((String) (table.getValueAt(row, 2)));
ageField.setText(String.valueOf(table.getValueAt(row, 3)));
String sex=(String) table.getValueAt(row, 4);
if(sex.equals("男")) {
radioMan.setSelected(true);
}
else {
radioFemal.setSelected(true);
}
mathField.setText(String.valueOf(table.getValueAt(row, 5)));
javaField.setText(String.valueOf(table.getValueAt(row, 6)));
cField.setText(String.valueOf(table.getValueAt(row, 7)));
englishField.setText(String.valueOf(table.getValueAt(row, 8)));
}
});
/**
*
*
* 菜单模型
*
*
*/
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\u5E8F\u53F7", "\u5B66\u53F7", "\u59D3\u540D", "\u5E74\u9F84", "\u6027\u522B", "\u9AD8\u6570", "JAVA", "C\u8BED\u8A00", "\u82F1\u8BED"
}
) {
boolean[] columnEditables = new boolean[] {
true, true, true, true, true, true, true, true, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
package com.student.util;
public class StringUtil {
public static Boolean isEmpy(String str) {
if(str==null||"".equals(str.trim())) {
return true;
}
else {
return false;
}
}
public static Boolean notIsEmpy(String str) {
if(str!=null&&!"".equals(str.trim())) {
return true;
}
else {
return false;
}
}
}
}
});
table.getColumnModel().getColumn(8).setResizable(false);
try {
this.fillTable(Dao.selectData());
} catch (SQLException e1) {
e1.printStackTrace();
}
scrollPane.setViewportView(table);
}
/**
*
* 填充表格方法
* @param result
*
*/
public static void fillTable( ResultSet result) {
DefaultTableModel det=(DefaultTableModel) table.getModel();
det.setRowCount(0);
try {
ResultSet rs=result;
while(rs.next()) {
Vector v=new Vector();
v.add(rs.getInt(1));
v.add(rs.getInt(2));
v.add(rs.getString(3));
v.add(rs.getString(4));
v.add(rs.getString(5));
v.add(rs.getString(6));
v.add(rs.getString(7));
v.add(rs.getString(8));
v.add(rs.getString(9));
det.addRow(v);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
*
*
* 搜索数据
*
*
*/
public void searchData() {
if(StringUtil.isEmpy(stuField.getText())) {
JOptionPane.showMessageDialog(null, "请输入学号!");
return;
}else {
try {
StudentData stu=new StudentData();
stu.setNum(Integer.parseInt(stuField.getText()));
ResultSet rs =Dao.search(stu);
if(rs.next()) {
fillTable(Dao.search(stu));
stuField.setText(String.valueOf(rs.getInt(2)));
nameField.setText(rs.getString(3));
ageField.setText(String.valueOf(rs.getInt(4)));
if(rs.getString(5).equals("男")) {
radioMan.setSelected(true);
}else {
radioFemal.setSelected(true);
}
mathField.setText(String.valueOf(rs.getFloat(6)));
javaField.setText(String.valueOf(rs.getFloat(7)));
cField.setText(String.valueOf(rs.getFloat(8)));
englishField.setText(String.valueOf(rs.getFloat(9)));
return;
}
else {
JOptionPane.showMessageDialog(null, "没有此学生或学号输入有误");
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
*
* 文本框制空
*/
public void clearText() {
stuField.setText("");
nameField.setText("");
ageField.setText("");
radioMan.setSelected(true);
mathField.setText("");
javaField.setText("");
cField.setText("");
englishField.setText("");
}
}
package com.student.util;
public class StringUtil {
public static Boolean isEmpy(String str) {
if(str==null||"".equals(str.trim())) {
return true;
}
else {
return false;
}
}
public static Boolean notIsEmpy(String str) {
if(str!=null&&!"".equals(str.trim())) {
return true;
}
else {
return false;
}
}
}
package com.student.modal;
public class StudentData {
private int id;
private int num;
private String name;
private String sex;
private int age;
private float math;
private float java;
private float c;
private float english;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getJava() {
return java;
}
public void setJava(float java) {
this.java = java;
}
public float getC() {
return c;
}
public void setC(float c) {
this.c = c;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
}
package com.student.modal;
public class User {
private double user;
private String password;
public Double getUser() {
return user;
}
public void setUser(Double user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.student.modal;
public class ThemeValue {
private int X1;
private int Y1;
private int X2;
private int Y2;
private String themeTitle;
private String backgroundColor;
public int getX1() {
return X1;
}
public void setX1(int x1) {
X1 = x1;
}
public int getY1() {
return Y1;
}
public void setY1(int y1) {
Y1 = y1;
}
public int getX2() {
return X2;
}
public void setX2(int x2) {
X2 = x2;
}
public int getY2() {
return Y2;
}
public void setY2(int y2) {
Y2 = y2;
}
public String getThemeTitle() {
return themeTitle;
}
public void setThemeTitle(String themeTitle) {
this.themeTitle = themeTitle;
}
public String getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
}
}
package com.student.Data;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.PreparedStatement;
import com.student.modal.StudentData;
import com.student.modal.User;
public class Dao {
/**
* 变量定义
*/
private static Connection conn=null;
private static String user="root";
private static String password="root";
private static String DRIVER="com.mysql.jdbc.Driver";
private static String dataUrl="jdbc:mysql://localhost:3306/StudentSystem?useUnicode=true&characterEncoding=utf-8";
/**
* 加载驱动
* 获取数据库连接
*/
public Dao() {
try {
Class.forName(DRIVER);
if(conn==null) {
conn=DriverManager.getConnection(dataUrl,user,password);
}
else {
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* 关闭数据库
*/
public static void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static PreparedStatement prepareStatement(String sql) throws SQLException {
PreparedStatement pstmt = null;
new Dao();
return pstmt = (PreparedStatement) conn.prepareStatement(sql);
}
/**
* 用户登陆检验后台
* @param user
* @return
* @throws SQLException
*/
public static User checkLogin(User user) throws SQLException {
User reUser=null;
String sql = "select * from user where user=? and password=?";
PreparedStatement pstmt=Dao.prepareStatement(sql);
pstmt.setDouble(1, user.getUser());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
reUser=new User();
reUser.setUser(rs.getDouble(1));
reUser.setPassword(rs.getString(2));
}
pstmt.close();
return reUser;
}
/**
* 搜索数据后台
* @return
* @throws SQLException
*/
public static ResultSet selectData() throws SQLException {
String sql="select * from StudentData";
PreparedStatement pstmt=Dao.prepareStatement(sql);
return pstmt.executeQuery();
}
/**
*
* 删除数据后台
* @param stu
* @return
* @throws SQLException
*/
public static int deleteData(StudentData stu) throws SQLException{
String sql="delete from StudentData where num=?";
int i = 0;
PreparedStatement pst = Dao.prepareStatement(sql);
pst.setInt(1, stu.getNum());
i=pst.executeUpdate();
return i;
}
/**
* 更新数据后台
* @param
* @return
* @throws SQLException
*/
public static int updateData(StudentData stu) throws SQLException {
String sql="update StudentData set name=?,age=?,sex=?,math=?,java=?,c=?,english=? where num=?";
PreparedStatement pst=Dao.prepareStatement(sql);
pst.setString(1, stu.getName());
pst.setInt(2, stu.getAge());
pst.setString(3, stu.getSex());
pst.setFloat(4, stu.getMath());
pst.setFloat(5, stu.getJava());
pst.setFloat(6,stu.getC()) ;
pst.setFloat(7, stu.getEnglish());
pst.setInt(8, stu.getNum());
return pst.executeUpdate();
}
/**
* 添加数据后台
* @param stu
* @return
* @throws SQLException
*/
public static int addData(StudentData stu) throws SQLException {
String sql="insert into StudentData values(null,?,?,?,?,?,?,?,?)";
PreparedStatement pst=Dao.prepareStatement(sql);
pst.setInt(1, stu.getNum());
pst.setString(2, stu.getName());
pst.setInt(3, stu.getAge());
pst.setString(4, stu.getSex());
pst.setFloat(5, stu.getMath());
pst.setFloat(6, stu.getJava());
pst.setFloat(7, stu.getC());
pst.setFloat(8, stu.getEnglish());
return pst.executeUpdate();
}
public static ResultSet search(StudentData stu) throws SQLException {
String sql="select * from StudentData where num=?";
PreparedStatement pstmt=Dao.prepareStatement(sql);
pstmt.setInt(1, stu.getNum());
return pstmt.executeQuery();
}
}
程序中可能有些需要改进的地方,如果又发现,可以联系QQ:873634918,或者发送电子邮件到[email protected],小白菜会及时纠正错误,这个项目主要是入门swing(GUI图新化编程)写的一个入门项目,只实现了增删改查等操作,还有很多功能有待实现。