📄 insert_student_inf.java
字号:
package stu.view;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
class insert_student_inf extends JFrame {
private static final long serialVersionUID = 3491871229709914774L;
private JButton button_1;
private ButtonGroup buttonGroup = new ButtonGroup();
private String number = "";
private String name = "";
private String sex = "";
private String classname = "";
private int age = 0;
private FileInputStream photo = null;
public insert_student_inf() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 544, 412);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension my = this.getSize();
setLocationByPlatform(true);
setLocation((screen.width - my.width) / 2,
(screen.height - my.height) / 2);
setResizable(false);
setVisible(true);
final JLabel label = new JLabel();
label.setFont(new Font("宋体", Font.PLAIN, 14));
label.setText("学 号:");
label.setBounds(43, 52, 66, 18);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setFont(new Font("宋体", Font.PLAIN, 14));
label_1.setText("姓 名:");
label_1.setBounds(43, 95, 66, 18);
getContentPane().add(label_1);
final JLabel label_2 = new JLabel();
label_2.setFont(new Font("宋体", Font.PLAIN, 14));
label_2.setText("性 别:");
label_2.setBounds(43, 140, 66, 18);
getContentPane().add(label_2);
final JLabel label_3 = new JLabel();
label_3.setFont(new Font("宋体", Font.PLAIN, 14));
label_3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
label_3.setText("年 龄:");
label_3.setBounds(43, 186, 66, 18);
getContentPane().add(label_3);
final JLabel label_3_1 = new JLabel();
label_3_1.setFont(new Font("宋体", Font.PLAIN, 14));
label_3_1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
label_3_1.setText("班 级 名:");
label_3_1.setBounds(43, 230, 66, 18);
getContentPane().add(label_3_1);
final JTextField textField = new JTextField();
textField.setBounds(115, 51, 135, 22);
getContentPane().add(textField);
final JTextField textField_1 = new JTextField();
textField_1.setBounds(115, 94, 135, 22);
getContentPane().add(textField_1);
final JTextField textField_2 = new JTextField();
textField_2.setBounds(115, 185, 135, 22);
getContentPane().add(textField_2);
final JRadioButton radioButton = new JRadioButton();
radioButton.setFont(new Font("", Font.PLAIN, 14));
buttonGroup.add(radioButton);
radioButton.setText("男");
radioButton.setBounds(115, 137, 45, 26);
radioButton.setSelected(true);
getContentPane().add(radioButton);
final JRadioButton radioButton_1 = new JRadioButton();
radioButton_1.setFont(new Font("", Font.PLAIN, 14));
buttonGroup.add(radioButton_1);
radioButton_1.setText("女");
radioButton_1.setBounds(205, 137, 45, 26);
getContentPane().add(radioButton_1);
// buttonGroup.setSelected(radioButton, true);
final JLabel label_4 = new JLabel();
label_4.setBorder(new EmptyBorder(0, 0, 0, 0));
label_4.setBackground(Color.BLACK);
// label_4.setBorder(new TitledBorder(null, "照片", TitledBorder.LEFT,
// TitledBorder.BELOW_TOP, null, null));
label_4.setBounds(275, 71, 219, 251);
Image image = Toolkit.getDefaultToolkit().getImage(
"src/images/默认照片.jpg");
image = image.getScaledInstance(label_4.getWidth(),
label_4.getHeight(), Image.SCALE_DEFAULT);
ImageIcon img = new ImageIcon(image);
img.setImage(image);
label_4.setIcon(img);
getContentPane().add(label_4);
final JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("", Font.PLAIN, 14));
comboBox.setBounds(115, 227, 135, 27);
getContentPane().add(comboBox);
String url = "jdbc:odbc:DB_Student";
Connection con;
try {
con = DriverManager.getConnection(url);
String str = "select 班级名 from 班级表";
Statement stt = con.createStatement();
ResultSet rst = stt.executeQuery(str);
while (rst.next()) {
comboBox.addItem((String) rst.getString(1));
}
} catch (SQLException e2) {
// e2.printStackTrace();
}
final JButton button = new JButton();
button.setFont(new Font("", Font.PLAIN, 14));
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
{
try {
photo = new FileInputStream(chooser
.getSelectedFile());
} catch (FileNotFoundException e1) {
// e1.printStackTrace();
}
Image image = Toolkit.getDefaultToolkit().getImage(
chooser.getSelectedFile().getAbsolutePath());
image = image.getScaledInstance(label_4.getWidth(),
label_4.getHeight(), Image.SCALE_DEFAULT);
ImageIcon img = new ImageIcon(image);
img.setImage(image);
label_4.setIcon(img);
}
}
}
});
button.setText("请选择照片:");
button.setBounds(275, 53, 219, 18);
getContentPane().add(button);
button_1 = new JButton();
button_1.setFont(new Font("宋体", Font.PLAIN, 15));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if ((number = textField.getText()).equals(""))
JOptionPane.showMessageDialog(null, "请输入学号!", "系统提示",
JOptionPane.WARNING_MESSAGE);
else if ((name = textField_1.getText()).equals(""))
JOptionPane.showMessageDialog(null, "请输入姓名!", "系统提示",
JOptionPane.WARNING_MESSAGE);
else if (textField_2.getText().equals("")){
JOptionPane.showMessageDialog(null, "请输入年龄!", "系统提示",
JOptionPane.WARNING_MESSAGE);
}
else{
try {
age = Integer.parseInt(textField_2.getText());
} catch (Exception ee) {
JOptionPane.showMessageDialog(null,
"输入的年龄数据格式不对,请重新录入!", "系统提示",
JOptionPane.WARNING_MESSAGE);
textField_2.setText("");
}
classname = (String) comboBox.getSelectedItem();
if (radioButton.isSelected() == true)
sex = "男";
else
sex = "女";
String url = "jdbc:odbc:DB_Student";
try {
Connection con1 = DriverManager.getConnection(url);
String str = "insert into 学生表 values('" + number
+ "','" + name + "','" + sex + "','" + age
+ "','" + classname + "',";
PreparedStatement pst1 = con1.prepareStatement(str
+ "?)");
try {
pst1.setBinaryStream(1, photo, photo.available());
} catch (Exception e1) {
// e1.printStackTrace();
}
pst1.execute();
pst1.close();
con1.close();
JOptionPane.showMessageDialog(null, "学生信息添加成功!",
"系统提示", JOptionPane.INFORMATION_MESSAGE);
textField.setText("");
textField_1.setText("");
textField_2.setText("");
} catch (SQLException e1) {
int result = JOptionPane.showOptionDialog(null,
"是否使用默认照片?", "系统提示", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
new String[] { "是", "否" }, "否");
if (result == JOptionPane.OK_OPTION) {
try {
photo = new FileInputStream(
"src/images/默认照片.jpg");
Connection con1 = DriverManager.getConnection(url);
String str = "insert into 学生表 values('" + number
+ "','" + name + "','" + sex + "','" + age
+ "','" + classname + "',";
PreparedStatement pst1 = con1.prepareStatement(str
+ "?)");
try {
pst1.setBinaryStream(1, photo, photo.available());
} catch (Exception e2) {
// e1.printStackTrace();
}
pst1.execute();
pst1.close();
con1.close();
JOptionPane.showMessageDialog(null, "学生信息添加成功!",
"系统提示", JOptionPane.INFORMATION_MESSAGE);
textField.setText("");
textField_1.setText("");
textField_2.setText("");
} catch (Exception e2) {
//e2.printStackTrace();
JOptionPane.showMessageDialog(null,
"该学生信息已经!", "系统提示",
JOptionPane.INFORMATION_MESSAGE);
}
} else {
}
}
}
}
});
button_1.setText("确定");
button_1.setBounds(43, 291, 66, 34);
getContentPane().add(button_1);
setTitle("添加学生");
setVisible(true);
final JLabel label_5 = new JLabel();
final TitledBorder titledBorder = new TitledBorder(null, "录入学生基本信息",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null);
titledBorder.setTitleFont(new Font("", Font.PLAIN, 14));
final TitledBorder titledBorder_1 = new TitledBorder(null, "录入学生基本信息",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null);
titledBorder_1.setTitleFont(new Font("宋体", Font.PLAIN, 15));
final TitledBorder titledBorder_2 = new TitledBorder(null, "录入学生基本信息",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null);
titledBorder_2.setTitleFont(new Font("宋体", Font.PLAIN, 15));
label_5.setBorder(titledBorder_2);
label_5.setBounds(18, 10, 501, 353);
getContentPane().add(label_5);
final JButton button_2 = new JButton();
button_2.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
dispose();
}
});
button_2.setFont(new Font("宋体", Font.PLAIN, 15));
button_2.setText("退出");
button_2.setBounds(184, 290, 66, 32);
getContentPane().add(button_2);
//
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -