0%

Java实训项目(大一下)

记录下这学期java实训(懒得保存了,干脆写成文章)

文章原址

文件百度云链接:https://pan.baidu.com/s/1Tm2diXuhxDx6C7rCOJ0ARA
提取码:eada

里面有源码工程文件和相关工具
mysql8.0官网下载


主窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package 学籍管理系统视窗;

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);





}
}

欢迎面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package 学籍管理系统视窗;

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);


}
}

录入面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package 学籍管理系统视窗;

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 数据库.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("请输入学号");
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("请输入姓名");
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);

//创建ButtonGroup对象
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[]{"航海技术","航海技术1","航海技术2"});
ComboBoxModel aModel2=new DefaultComboBoxModel(new String[]{"轮机工程学","轮机工程学1","轮机工程学2"});
ComboBoxModel aModel3=new DefaultComboBoxModel(new String[]{"计算机科学与技术","电子商务(本科)","软件技术","计算机应用技术","电子商务"});
ComboBoxModel aModel4=new DefaultComboBoxModel(new String[]{"英语","日语","外语学"});
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[] {"海运学院", "轮机工程学院", "信息与通信工程学院", "外语学院"}));
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("请输入姓名")) {
JOptionPane.showMessageDialog(null,"请输入姓名","错误",JOptionPane.ERROR_MESSAGE);
return;
}

if(textField.getText().equals("")||textField.getText().equals("请输入学号")) {
JOptionPane.showMessageDialog(null,"请输入学号","错误",JOptionPane.ERROR_MESSAGE);
return;
}

if(comboBox.getSelectedIndex()==-1) {
JOptionPane.showMessageDialog(null,"请选择学院","错误",JOptionPane.ERROR_MESSAGE);
return;
}
if(comboBox_1.getSelectedIndex()==-1) {
JOptionPane.showMessageDialog(null,"请选择专业","错误",JOptionPane.ERROR_MESSAGE);
return;
}

if(textField_2.getText().equals("")) {
JOptionPane.showMessageDialog(null,"请输入班级","错误",JOptionPane.ERROR_MESSAGE);
return;
}


//保存数据
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();
//调用自己链接数据库操作方法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("请输入学号");
textField_1.setText("请输入姓名");
textField_2.setText("");
comboBox.setSelectedIndex(-1);
comboBox_1.setSelectedIndex(-1);
}
else
JOptionPane.showMessageDialog(null,"失败,学号重复","提示",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("请输入学号");
textField_1.setText("请输入姓名");
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);

}

}

修改面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package 学籍管理系统视窗;

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 数据库.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[]{"航海技术","航海技术1","航海技术2"});
ComboBoxModel aModel2=new DefaultComboBoxModel(new String[]{"轮机工程学","轮机工程学1","轮机工程学2"});
ComboBoxModel aModel3=new DefaultComboBoxModel(new String[]{"计算机科学与技术","电子商务(本科)","软件技术","计算机应用技术","电子商务"});
ComboBoxModel aModel4=new DefaultComboBoxModel(new String[]{"英语","日语","外语学"});
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();
//调用自己链接数据库操作方法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,"查询结果:",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,"查无此人","查询结果",JOptionPane.NO_OPTION );


}
stmt.close();//关闭数据库
sin.close();
} catch (SQLException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
}

});

}
}

查询面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package 学籍管理系统视窗;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


import 数据库.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,"查询结果:",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,"查无此人","查询结果",JOptionPane.NO_OPTION );
}

stmt.close();//关闭数据库
sin.close();
} catch (SQLException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
}








});
btnNewButton.setBounds(366, 17, 77, 27);
add(btnNewButton);

}

}

删除面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package 学籍管理系统视窗;

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 数据库.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,"查无此人","查询结果",JOptionPane.NO_OPTION );
}
stmt.close();//关闭数据库
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,"该学号信息不存在!","信息",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);


}

}

数据库类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package 数据库;

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不需要加载驱动
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;
}
//添加数据
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 );
}
}
//查询数据
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;
}
//判断学号重复
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;
}

//删除数据
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{

}
}

//更新修改数据
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 );
}
}


}
坚持原创技术分享,您的支持将鼓励我继续分享!