記錄下這學(xué)期java實(shí)訓(xùn)(懶得保存了,干脆寫成文章)
鏈接:https://pan.baidu.com/s/1Tm2diXuhxDx6C7rCOJ0ARA
提取碼:eada
里面有源碼工程文件和相關(guān)工具
mysql8.0官網(wǎng)下載
主窗口
package 學(xué)籍管理系統(tǒng)視窗;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
public class 主窗口 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
主窗口 frame = new 主窗口();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws SQLException
*/
public 主窗口() throws SQLException {
//this.setLocationRelativeTo(null);
setBounds(100, 100, 800, 500);
this.setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
CardLayout card=new CardLayout();
contentPane.setLayout(card);
歡迎面板 歡迎面板=new 歡迎面板();
contentPane.add("0",歡迎面板);
修改面板 修改面板=new 修改面板();
contentPane.add("2",修改面板);
錄入面板 錄入面板=new 錄入面板();
contentPane.add("1",錄入面板);
刪除面板 刪除面板=new 刪除面板();
contentPane.add("3",刪除面板);
查詢面板 查詢面板=new 查詢面板();
contentPane.add("4",查詢面板);
查看面板 查看面板=new 查看面板();
contentPane.add("5",查看面板);
setTitle("\u5E7F\u5DDE\u822A\u6D77\u5B66\u9662\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\668eca61e8886291e5af08b33983448e.jpg"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("\u64CD\u4F5C\u83DC\u5355");
menu.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192426.png"));
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("\u5F55\u5165\u5B66\u751F\u4FE1\u606F");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.ALT_MASK));
menuItem.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192512.png"));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(contentPane, "1");
}
});
menu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("\u4FEE\u6539\u5B66\u751F\u4FE1\u606F");
menuItem_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.ALT_MASK));
menuItem_1.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192548.png"));
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(contentPane, "2");
}
});
menu.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("\u5220\u9664\u5B66\u751F\u4FE1\u606F");
menuItem_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.ALT_MASK));
menuItem_2.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192731.png"));
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(contentPane, "3");
}
});
menu.add(menuItem_2);
JMenuItem menuItem_3 = new JMenuItem("\u67E5\u8BE2\u5B66\u751F\u4FE1\u606F");
menuItem_3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
menuItem_3.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192757.png"));
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(contentPane, "4");
}
});
menu.add(menuItem_3);
JMenuItem menuItem_4 = new JMenuItem("\u663E\u793A\u6240\u6709\u4FE1\u606F");
menuItem_4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.ALT_MASK));
menuItem_4.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192820.png"));
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
card.show(contentPane, "5");
}
});
menu.add(menuItem_4);
}
}
歡迎面板
package 學(xué)籍管理系統(tǒng)視窗;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
public class 歡迎面板 extends JPanel {
/**
* Create the panel.
*/
public 歡迎面板() {
setBackground(Color.CYAN);
setLayout(null);
JLabel lblNewLabel = new JLabel("\u6B22\u8FCE\u4F7F\u7528\u5E7F\u5DDE\u822A\u6D77\u5B66\u9662\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 24));
lblNewLabel.setBounds(34, 90, 387, 62);
add(lblNewLabel);
JLabel label = new JLabel("\u8BF7\u4F60\u6309\u5DE6\u4E0A\u89D2\u83DC\u5355\u680F\u63D0\u793A\u64CD\u4F5C");
label.setBounds(100, 178, 233, 18);
add(label);
}
}
錄入面板
package 學(xué)籍管理系統(tǒng)視窗;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import 數(shù)據(jù)庫(kù).Test;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class 錄入面板 extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JLabel lblNewLabel;
/**
* Create the panel.
*/
public 錄入面板() {
setLayout(null);
lblNewLabel = new JLabel("\u5B66\u53F7");
lblNewLabel.setBounds(72, 55, 30, 18);
add(lblNewLabel);
JLabel label = new JLabel("\u59D3\u540D");
label.setBounds(72, 24, 30, 18);
add(label);
textField = new JTextField("請(qǐng)輸入學(xué)號(hào)");
textField.setBounds(116, 52, 130, 24);
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText("");
}
});
add(textField);
textField.setColumns(10);
textField_1 = new JTextField("請(qǐng)輸入姓名");
textField_1.setBounds(116, 21, 130, 24);
textField_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField_1.setText("");
}
});
add(textField_1);
textField_1.setColumns(10);
JLabel label_1 = new JLabel("\u6027\u522B");
label_1.setBounds(72, 89, 30, 18);
add(label_1);
JRadioButton radioButton = new JRadioButton("\u7537");
radioButton.setBounds(126, 85, 43, 27);
add(radioButton);
JRadioButton radioButton_1 = new JRadioButton("\u5973");
radioButton_1.setBounds(175, 85, 55, 27);
add(radioButton_1);
//創(chuàng)建ButtonGroup對(duì)象
ButtonGroup btgSex = new ButtonGroup();
btgSex.add(radioButton);
btgSex.add(radioButton_1);
JLabel label_2 = new JLabel("\u5B66\u9662");
label_2.setBounds(72, 120, 30, 18);
add(label_2);
ComboBoxModel aModel1=new DefaultComboBoxModel(new String[]{"航海技術(shù)","航海技術(shù)1","航海技術(shù)2"});
ComboBoxModel aModel2=new DefaultComboBoxModel(new String[]{"輪機(jī)工程學(xué)","輪機(jī)工程學(xué)1","輪機(jī)工程學(xué)2"});
ComboBoxModel aModel3=new DefaultComboBoxModel(new String[]{"計(jì)算機(jī)科學(xué)與技術(shù)","電子商務(wù)(本科)","軟件技術(shù)","計(jì)算機(jī)應(yīng)用技術(shù)","電子商務(wù)"});
ComboBoxModel aModel4=new DefaultComboBoxModel(new String[]{"英語(yǔ)","日語(yǔ)","外語(yǔ)學(xué)"});
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBounds(110, 151, 168, 24);
comboBox_1.setToolTipText("");
add(comboBox_1);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(110, 117, 164, 24);
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED)
{
int a=comboBox.getSelectedIndex();
if(a==0)
{
comboBox_1.setModel(aModel1);
comboBox_1.setSelectedIndex(-1);
}
else if(a==1)
{
comboBox_1.setModel(aModel2);
comboBox_1.setSelectedIndex(-1);
}
else if(a==2)
{
comboBox_1.setModel(aModel3);
comboBox_1.setSelectedIndex(-1);
}
else if(a==3)
{
comboBox_1.setModel(aModel4);
comboBox_1.setSelectedIndex(-1);
}
}
}
});
comboBox.setModel(new DefaultComboBoxModel(new String[] {"海運(yùn)學(xué)院", "輪機(jī)工程學(xué)院", "信息與通信工程學(xué)院", "外語(yǔ)學(xué)院"}));
comboBox.setSelectedIndex(-1);
add(comboBox);
JButton button = new JButton("\u5F55\u5165");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sex="";
if(radioButton.isSelected()) {
sex=radioButton.getText();
}else {
sex=radioButton_1.getText();
}
if(textField_1.getText().equals("")||textField_1.getText().equals("請(qǐng)輸入姓名")) {
JOptionPane.showMessageDialog(null,"請(qǐng)輸入姓名","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}
if(textField.getText().equals("")||textField.getText().equals("請(qǐng)輸入學(xué)號(hào)")) {
JOptionPane.showMessageDialog(null,"請(qǐng)輸入學(xué)號(hào)","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}
if(comboBox.getSelectedIndex()==-1) {
JOptionPane.showMessageDialog(null,"請(qǐng)選擇學(xué)院","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}
if(comboBox_1.getSelectedIndex()==-1) {
JOptionPane.showMessageDialog(null,"請(qǐng)選擇專業(yè)","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}
if(textField_2.getText().equals("")) {
JOptionPane.showMessageDialog(null,"請(qǐng)輸入班級(jí)","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);
return;
}
//保存數(shù)據(jù)
String sno=textField.getText();
String name=textField_1.getText();
String ssex;
if(radioButton.isSelected()) {
ssex="男";
}else
ssex="女";
String department=comboBox.getSelectedItem().toString();
String major=comboBox_1.getSelectedItem().toString();
String myclass=textField_2.getText();
//調(diào)用自己鏈接數(shù)據(jù)庫(kù)操作方法test
Connection sin=Test.getconnectin();
boolean flag;
flag=Test.chongfu(sin,sno);
System.out.println(flag);
if(flag==false) {
Object str[]={sno,name,ssex,department,major,myclass};
Test.insert(sin, str);
JOptionPane.showMessageDialog(null,"錄入成功","提示",JOptionPane.INFORMATION_MESSAGE);
radioButton.setSelected(false);
radioButton_1.setSelected(false);
textField.setText("請(qǐng)輸入學(xué)號(hào)");
textField_1.setText("請(qǐng)輸入姓名");
textField_2.setText("");
comboBox.setSelectedIndex(-1);
comboBox_1.setSelectedIndex(-1);
}
else
JOptionPane.showMessageDialog(null,"失敗,學(xué)號(hào)重復(fù)","提示",JOptionPane.INFORMATION_MESSAGE);
}
});
button.setBounds(203, 232, 63, 27);
add(button);
JButton button_1 = new JButton("\u91CD\u7F6E");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
radioButton.setSelected(false);
radioButton_1.setSelected(false);
textField.setText("請(qǐng)輸入學(xué)號(hào)");
textField_1.setText("請(qǐng)輸入姓名");
textField_2.setText("");
comboBox.setSelectedIndex(-1);
comboBox_1.setSelectedIndex(-1);
JOptionPane.showMessageDialog(null,"重置成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
});
button_1.setBounds(106, 232, 63, 27);
add(button_1);
JLabel label_3 = new JLabel("\u4E13\u4E1A");
label_3.setBounds(72, 154, 30, 18);
add(label_3);
JLabel label_4 = new JLabel("\u73ED\u7EA7");
label_4.setBounds(72, 191, 30, 18);
add(label_4);
textField_2 = new JTextField();
textField_2.setBounds(110, 188, 164, 24);
add(textField_2);
textField_2.setColumns(10);
}
}
修改面板
package 學(xué)籍管理系統(tǒng)視窗;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
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;
import javax.swing.JTextField;
import 數(shù)據(jù)庫(kù).Test;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class 修改面板 extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
/**
* Create the panel.
*/
public 修改面板() {
setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u8981\u4FEE\u6539\u4FE1\u606F\u7684\u5B66\u53F7\uFF1A");
label.setFont(new Font("宋體", Font.PLAIN, 16));
label.setBounds(42, 18, 177, 18);
add(label);
textField = new JTextField();
textField.setBounds(213, 15, 144, 24);
add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\uFF08\u65B0\uFF09\u59D3\u540D\uFF1A");
label_1.setBounds(42, 76, 120, 18);
add(label_1);
textField_1 = new JTextField();
textField_1.setBounds(133, 73, 120, 24);
add(textField_1);
textField_1.setColumns(10);
JRadioButton radioButton = new JRadioButton("\u7537");
radioButton.setBounds(107, 119, 51, 27);
add(radioButton);
JRadioButton radioButton_1 = new JRadioButton("\u5973");
radioButton_1.setBounds(168, 119, 51, 27);
add(radioButton_1);
ButtonGroup btgSex = new ButtonGroup();
btgSex.add(radioButton);
btgSex.add(radioButton_1);
ComboBoxModel aModel1=new DefaultComboBoxModel(new String[]{"航海技術(shù)","航海技術(shù)1","航海技術(shù)2"});
ComboBoxModel aModel2=new DefaultComboBoxModel(new String[]{"輪機(jī)工程學(xué)","輪機(jī)工程學(xué)1","輪機(jī)工程學(xué)2"});
ComboBoxModel aModel3=new DefaultComboBoxModel(new String[]{"計(jì)算機(jī)科學(xué)與技術(shù)","電子商務(wù)(本科)","軟件技術(shù)","計(jì)算機(jī)應(yīng)用技術(shù)","電子商務(wù)"});
ComboBoxModel aModel4=new DefaultComboBoxModel(new String[]{"英語(yǔ)","日語(yǔ)","外語(yǔ)學(xué)"});
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setToolTipText("");
comboBox_1.setBounds(133, 207, 185, 24);
this.add(comboBox_1);
JComboBox comboBox = new JComboBox();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED)
{
int a=comboBox.getSelectedIndex();
if(a==0)
{
comboBox_1.setModel(aModel1);
comboBox_1.setSelectedIndex(-1);
}
else if(a==1)
{
comboBox_1.setModel(aModel2);
comboBox_1.setSelectedIndex(-1);
}
else if(a==2)
{
comboBox_1.setModel(aModel3);
comboBox_1.setSelectedIndex(-1);
}
else if(a==3)
{
comboBox_1.setModel(aModel4);
comboBox_1.setSelectedIndex(-1);
}
}
}
});
comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u6D77\u8FD0\u5B66\u9662", "\u8F6E\u673A\u5DE5\u7A0B\u5B66\u9662", "\u4FE1\u606F\u4E0E\u901A\u4FE1\u5DE5\u7A0B\u5B66\u9662", "\u5916\u8BED\u5B66\u9662"}));
comboBox.setSelectedIndex(-1);
comboBox.setBounds(133, 167, 182, 24);
this.add(comboBox);
JLabel lblNewLabel = new JLabel("\u65B0\u5B66\u9662\uFF1A");
lblNewLabel.setBounds(72, 169, 71, 18);
add(lblNewLabel);
JLabel label_2 = new JLabel("\u65B0\u4E13\u4E1A\uFF1A");
label_2.setBounds(70, 210, 72, 18);
add(label_2);
JLabel label_3 = new JLabel("\u65B0 \u73ED\u7EA7");
label_3.setBounds(72, 247, 72, 18);
add(label_3);
textField_2 = new JTextField();
textField_2.setBounds(133, 244, 185, 24);
add(textField_2);
textField_2.setColumns(10);
JButton button = new JButton("\u4FEE\u6539");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sno=textField.getText();
String name=textField_1.getText();
String ssex;
if(radioButton.isSelected()) {
ssex="男";
}else
ssex="女";
String department=comboBox.getSelectedItem().toString();
String major=comboBox_1.getSelectedItem().toString();
String myclass=textField_2.getText();
//調(diào)用自己鏈接數(shù)據(jù)庫(kù)操作方法test
Object str[]={sno,name,ssex,department,major,myclass};
Connection sin=Test.getconnectin();
Test.update(sin, str);
radioButton.setSelected(false);
radioButton_1.setSelected(false);
textField.setText("");
textField_1.setText("");
textField_2.setText("");
comboBox.setSelectedIndex(-1);
comboBox_1.setSelectedIndex(-1);
JOptionPane.showMessageDialog(null,"修改成功","提示",JOptionPane.NO_OPTION );
}
});
button.setBounds(82, 282, 114, 27);
add(button);
JButton button_1 = new JButton("\u91CD\u7F6E");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
radioButton.setSelected(false);
radioButton_1.setSelected(false);
textField.setText("");
textField_1.setText("");
textField_2.setText("");
comboBox.setSelectedIndex(-1);
comboBox_1.setSelectedIndex(-1);
JOptionPane.showMessageDialog(null,"重置成功!","信息",JOptionPane.INFORMATION_MESSAGE );
}
});
button_1.setBounds(204, 282, 114, 27);
add(button_1);
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.setToolTipText("");
btnNewButton.setBounds(364, 15, 79, 27);
add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection sin=Test.getconnectin();
String sno=textField.getText();
ResultSet rs=null;
PreparedStatement stmt=null;
boolean flag=false;
try {
//牛逼查找
stmt=sin.prepareStatement("select * from student where sno=?");
stmt.setObject(1,sno);
rs=stmt.executeQuery();
while(rs.next()){
String msg=rs.getString("sno")+"\n"+rs.getString("name")+"\n"+rs.getString("sex")+"\n"+rs.getString("department")+"\n"+rs.getString("subject")+"\n"+rs.getString("myclass")+"\n";
//JOptionPane.showMessageDialog(null,msg,"查詢結(jié)果:",JOptionPane.NO_OPTION );
flag=true;
//修改
textField_1.setText(rs.getString("name"));
String ssex=null;
ssex=rs.getString("sex");
if(ssex.equalsIgnoreCase("男")) {
radioButton.setSelected(true);
}else {
radioButton_1.setSelected(true);
}
textField_2.setText(rs.getString("myclass"));
comboBox.setSelectedItem(rs.getString("department"));
comboBox_1.setSelectedItem(rs.getString("subject"));
}
if(flag==false) {
radioButton.setSelected(false);
radioButton_1.setSelected(false);
textField_1.setText("");
textField_2.setText("");
comboBox.setSelectedIndex(-1);
comboBox_1.setSelectedIndex(-1);
JOptionPane.showMessageDialog(null,"查無(wú)此人","查詢結(jié)果",JOptionPane.NO_OPTION );
}
stmt.close();//關(guān)閉數(shù)據(jù)庫(kù)
sin.close();
} catch (SQLException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
}
});
}
}
查詢面板
package 學(xué)籍管理系統(tǒng)視窗;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import 數(shù)據(jù)庫(kù).Test;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class 查詢面板 extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Create the panel.
*/
public 查詢面板() {
setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u8981\u67E5\u8BE2\u4FE1\u606F\u7684\u5B66\u53F7\uFF1A");
label.setBounds(37, 22, 189, 18);
add(label);
textField = new JTextField();
textField.setBounds(201, 19, 154, 24);
add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u59D3\u540D");
label_1.setBounds(72, 60, 40, 18);
add(label_1);
textField_1 = new JTextField();
textField_1.setEnabled(false);
textField_1.setBounds(112, 60, 150, 24);
add(textField_1);
textField_1.setColumns(10);
JLabel label_2 = new JLabel("\u6027\u522B");
label_2.setBounds(72, 100, 52, 18);
add(label_2);
textField_2 = new JTextField();
textField_2.setEnabled(false);
textField_2.setBounds(112, 100, 150, 24);
add(textField_2);
textField_2.setColumns(10);
JLabel label_3 = new JLabel("\u5B66\u9662");
label_3.setBounds(72, 140, 41, 18);
add(label_3);
textField_3 = new JTextField();
textField_3.setEnabled(false);
textField_3.setBounds(112, 140, 150, 24);
add(textField_3);
textField_3.setColumns(10);
JLabel label_4 = new JLabel("\u4E13\u4E1A");
label_4.setBounds(72, 180, 45, 18);
add(label_4);
textField_4 = new JTextField();
textField_4.setEnabled(false);
textField_4.setBounds(112, 180, 150, 24);
add(textField_4);
textField_4.setColumns(10);
JLabel label_5 = new JLabel("\u73ED\u7EA7");
label_5.setBounds(72, 220, 45, 18);
add(label_5);
textField_5 = new JTextField();
textField_5.setEnabled(false);
textField_5.setBounds(112, 220, 150, 24);
add(textField_5);
textField_5.setColumns(10);
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection sin=Test.getconnectin();
String sno=textField.getText();
ResultSet rs=null;
PreparedStatement stmt=null;
try {
//牛逼查找
stmt=sin.prepareStatement("select * from student where sno=?");
stmt.setObject(1,sno);
rs=stmt.executeQuery();
boolean flag=false;
while(rs.next()){
String msg=rs.getString("sno")+"\n"+rs.getString("name")+"\n"+rs.getString("sex")+"\n"+rs.getString("department")+"\n"+rs.getString("subject")+"\n"+rs.getString("myclass")+"\n";
JOptionPane.showMessageDialog(null,msg,"查詢結(jié)果:",JOptionPane.NO_OPTION );
textField_1.setText(rs.getString("name"));
textField_2.setText(rs.getString("sex"));
textField_3.setText(rs.getString("department"));
textField_4.setText(rs.getString("subject"));
textField_5.setText(rs.getString("myclass"));
flag=true;
}
if(flag==false) {
JOptionPane.showMessageDialog(null,"查無(wú)此人","查詢結(jié)果",JOptionPane.NO_OPTION );
}
stmt.close();//關(guān)閉數(shù)據(jù)庫(kù)
sin.close();
} catch (SQLException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
}
});
btnNewButton.setBounds(366, 17, 77, 27);
add(btnNewButton);
}
}
刪除面板
package 學(xué)籍管理系統(tǒng)視窗;
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.SQLException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import 數(shù)據(jù)庫(kù).Test;
public class 刪除面板 extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Create the panel.
*/
public 刪除面板() {
setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u8981\u5220\u9664\u4FE1\u606F\u7684\u5B66\u53F7\uFF1A");
label.setBounds(37, 22, 189, 18);
add(label);
textField = new JTextField();
textField.setBounds(201, 19, 154, 24);
add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u59D3\u540D");
label_1.setBounds(72, 60, 40, 18);
add(label_1);
textField_1 = new JTextField();
textField_1.setEnabled(false);
textField_1.setBounds(112, 60, 150, 24);
add(textField_1);
textField_1.setColumns(10);
JLabel label_2 = new JLabel("\u6027\u522B");
label_2.setBounds(72, 100, 52, 18);
add(label_2);
textField_2 = new JTextField();
textField_2.setEnabled(false);
textField_2.setBounds(112, 100, 150, 24);
add(textField_2);
textField_2.setColumns(10);
JLabel label_3 = new JLabel("\u5B66\u9662");
label_3.setBounds(72, 140, 41, 18);
add(label_3);
textField_3 = new JTextField();
textField_3.setEnabled(false);
textField_3.setBounds(112, 140, 150, 24);
add(textField_3);
textField_3.setColumns(10);
JLabel label_4 = new JLabel("\u4E13\u4E1A");
label_4.setBounds(72, 180, 45, 18);
add(label_4);
textField_4 = new JTextField();
textField_4.setEnabled(false);
textField_4.setBounds(112, 180, 150, 24);
add(textField_4);
textField_4.setColumns(10);
JLabel label_5 = new JLabel("\u73ED\u7EA7");
label_5.setBounds(72, 220, 45, 18);
add(label_5);
textField_5 = new JTextField();
textField_5.setEnabled(false);
textField_5.setBounds(112, 220, 150, 24);
add(textField_5);
textField_5.setColumns(10);
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection sin=Test.getconnectin();
String sno=textField.getText();
ResultSet rs=null;
PreparedStatement stmt=null;
try {
//牛逼查找
stmt=sin.prepareStatement("select * from student where sno=?");
stmt.setObject(1,sno);
rs=stmt.executeQuery();
boolean flag=false;
while(rs.next()){
JOptionPane.showMessageDialog(null,"查詢成功","提示",JOptionPane.NO_OPTION );
textField_1.setText(rs.getString("name"));
textField_2.setText(rs.getString("sex"));
textField_3.setText(rs.getString("department"));
textField_4.setText(rs.getString("subject"));
textField_5.setText(rs.getString("myclass"));
flag=true;
}
if(flag==false) {
JOptionPane.showMessageDialog(null,"查無(wú)此人","查詢結(jié)果",JOptionPane.NO_OPTION );
}
stmt.close();//關(guān)閉數(shù)據(jù)庫(kù)
sin.close();
} catch (SQLException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
}
});
btnNewButton.setBounds(366, 17, 77, 27);
add(btnNewButton);
JButton button = new JButton("\u5220\u9664");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//刪除操作
String sno=textField.getText();
boolean flag;
Connection sin=Test.getconnectin();
flag=Test.chongfu(sin,sno);
System.out.println(flag);
if(flag) {
Test.delete(sin, sno);
JOptionPane.showMessageDialog(null,"信息刪除成功!","信息",JOptionPane.INFORMATION_MESSAGE );
textField.setText("");
textField_1.setText("");
textField_2.setText("");
textField_3.setText("");
textField_4.setText("");
textField_5.setText("");
}
else
JOptionPane.showMessageDialog(null,"該學(xué)號(hào)信息不存在!","信息",JOptionPane.INFORMATION_MESSAGE );
}
});
button.setBounds(72, 257, 113, 27);
add(button);
JButton button_1 = new JButton("\u91CD\u7F6E");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("");
textField_2.setText("");
textField_3.setText("");
textField_4.setText("");
textField_5.setText("");
JOptionPane.showMessageDialog(null,"重置成功","提示",JOptionPane.NO_OPTION );
}
});
button_1.setBounds(217, 257, 113, 27);
add(button_1);
}
}
數(shù)據(jù)庫(kù)類
package 數(shù)據(jù)庫(kù);
import java.sql.*;
import javax.swing.JOptionPane;
public class Test {
public final static Connection getconnectin(){
Connection con=null;
//String driver = "com.mysql.jdbc.Driver"; 8.0不需要加載驅(qū)動(dòng)
String url = "jdbc:mysql://localhost:3306/gip?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai";
String user = "root";
String password = "123456";
try{
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
con.setCatalog("gip");
}
catch(SQLException sqle){
System.out.println(sqle+"lianjiecuowu");
}
catch(Exception e){
System.out.println(e+"diergecuowu");
}
return con;
}
//添加數(shù)據(jù)
public final static void insert(Connection con,Object obj[]){
PreparedStatement stmt=null;
try {
stmt=con.prepareStatement("insert into student(sno,name,sex,department,subject,myclass) values(?,?,?,?,?,?)");
stmt.setString(1,obj[0].toString());
stmt.setString(2,obj[1].toString());
stmt.setString(3,obj[2].toString());
stmt.setString(4,obj[3].toString());
stmt.setString(5,obj[4].toString());
stmt.setString(6,obj[5].toString());
stmt.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"shibai","操作提示",JOptionPane.NO_OPTION );
}
}
//查詢數(shù)據(jù)
public final static ResultSet query(Connection con){
ResultSet rs=null;
PreparedStatement stmt=null;
try {
stmt=con.prepareStatement("select * from student");
rs=stmt.executeQuery();
//stmt.close();
//con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//判斷學(xué)號(hào)重復(fù)
public final static boolean chongfu(Connection con,String a){
ResultSet rs=null;
PreparedStatement stmt=null;
boolean flag=true;;
try {
stmt=con.prepareStatement("select sno from student");
rs=stmt.executeQuery();
while(rs.next())
{
if(a.equalsIgnoreCase(rs.getString("sno")))
return true;
//System.out.println(rs.getString("sno"));
}
//stmt.close();
//con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//刪除數(shù)據(jù)
public final static void delete(Connection con,String s){
try {
PreparedStatement stmt;
stmt=con.prepareStatement("delete from student where sno=?");
stmt.setObject(1,s);
stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
}
//更新修改數(shù)據(jù)
public final static void update(Connection con,Object obj[]){
PreparedStatement stmt=null;
try {
stmt=con.prepareStatement("update student set name=?,sex=?,department=?,subject=?,myclass=? where sno=?");
stmt.setString(6,obj[0].toString());
stmt.setString(1,obj[1].toString());
stmt.setString(2,obj[2].toString());
stmt.setString(3,obj[3].toString());
stmt.setString(4,obj[4].toString());
stmt.setString(5,obj[5].toString());
stmt.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,"shibai","操作提示",JOptionPane.NO_OPTION );
}
}
}