1、创建添加窗体
package com.student.view; import java.awt.EventQueue; import javax.swing.ButtonGroup; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; /* * 项目名称: * * 文件名称为:AddStudent.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月22日 上午8:57:41 * @copyright daxiang */ public class AddStudent extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Add frame = new Add(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public AddStudent() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 474, 452); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("学号"); lblNewLabel.setBounds(56, 51, 72, 18); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("姓名"); lblNewLabel_1.setBounds(56, 109, 72, 18); contentPane.add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("性别"); lblNewLabel_2.setBounds(56, 178, 72, 18); contentPane.add(lblNewLabel_2); JLabel lblNewLabel_3 = new JLabel("班级"); lblNewLabel_3.setBounds(56, 243, 72, 18); contentPane.add(lblNewLabel_3); textField = new JTextField(); textField.setBounds(128, 48, 175, 24); contentPane.add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setBounds(128, 106, 175, 24); contentPane.add(textField_1); textField_1.setColumns(10); ButtonGroup buttonGroup = new ButtonGroup(); JRadioButton gril = new JRadioButton("女"); gril.setBounds(223, 174, 57, 27); contentPane.add(gril); JRadioButton boy = new JRadioButton("男"); boy.setBounds(140, 174, 57, 27); contentPane.add(boy); buttonGroup.add(boy); buttonGroup.add(gril); JComboBox comboBox = new JComboBox(); comboBox.setModel(new DefaultComboBoxModel(new String[] {"计科1班", "计科2班", "计科3班", "计科4班"})); comboBox.setBounds(128, 240, 175, 24); contentPane.add(comboBox); JButton btnNewButton = new JButton("添加"); btnNewButton.setBounds(56, 325, 113, 27); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("取消"); btnNewButton_1.setBounds(226, 325, 113, 27); contentPane.add(btnNewButton_1); } }
2、创建StudentDao并增加add方法
package com.student.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.student.model.Student; import com.student.util.DbUtil; /* * 项目名称: * * 文件名称为:StudentDao.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月20日 上午8:10:50 * @copyright daxiang */ public class StudentDao { public boolean add(Student student) throws SQLException { DbUtil dbUtil = new DbUtil(); String sql = "insert into tb_student (name,sno,sex,classname) values ('"+student.getName() + "','" + student.getSno() + "','" + student.getSex() + "','" + student.getClassName() + "')"; return dbUtil.execute(sql); } }
3、创建StudentService并增加add服务
package com.student.service; import java.sql.SQLException; import java.util.List; import com.student.dao.StudentDao; import com.student.model.Student; /* * 项目名称: * * 文件名称为:StudentService.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月20日 上午8:09:56 * @copyright daxiang */ public class StudentService { /** * 增加学生 * * @param student * @return * @throws SQLException */ public boolean addStudent(Student student) throws SQLException { StudentDao studentDao = new StudentDao(); return studentDao.add(student); } }
4、窗体实现添加
package com.student.view; import java.awt.EventQueue; import java.awt.HeadlessException; import javax.swing.ButtonGroup; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import com.student.model.Student; import com.student.service.StudentService; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.sql.SQLException; /* * 项目名称: * * 文件名称为:AddStuent.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月22日 上午8:57:41 * @copyright daxiang */ public class AddStudent extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Add frame = new Add(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public AddStudent() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); setBounds(100, 100, 474, 452); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("学号"); lblNewLabel.setBounds(56, 51, 72, 18); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("姓名"); lblNewLabel_1.setBounds(56, 109, 72, 18); contentPane.add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("性别"); lblNewLabel_2.setBounds(56, 178, 72, 18); contentPane.add(lblNewLabel_2); JLabel lblNewLabel_3 = new JLabel("班级"); lblNewLabel_3.setBounds(56, 243, 72, 18); contentPane.add(lblNewLabel_3); textField = new JTextField(); textField.setBounds(128, 48, 175, 24); contentPane.add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setBounds(128, 106, 175, 24); contentPane.add(textField_1); textField_1.setColumns(10); ButtonGroup buttonGroup = new ButtonGroup(); JRadioButton gril = new JRadioButton("女"); gril.setBounds(223, 174, 57, 27); contentPane.add(gril); JRadioButton boy = new JRadioButton("男"); boy.setBounds(140, 174, 57, 27); contentPane.add(boy); buttonGroup.add(boy); buttonGroup.add(gril); JComboBox comboBox = new JComboBox(); comboBox.setModel(new DefaultComboBoxModel(new String[] {"计科1班", "计科2班", "计科3班", "计科4班"})); comboBox.setBounds(128, 240, 175, 24); contentPane.add(comboBox); JButton btnNewButton = new JButton("添加"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String sno=textField.getText().trim(); String name = textField_1.getText().trim(); String sex=""; if (boy.isSelected()) { sex="男"; }else if(gril.isSelected()){ sex="女"; } String className =comboBox.getSelectedItem().toString(); Student student = new Student(sno, name, sex, className); StudentService service = new StudentService(); try { if(service.addStudent(student)){ JOptionPane.showMessageDialog(null, "添加成功"); }else{ JOptionPane.showMessageDialog(null, "添加失败"); } } catch (HeadlessException | SQLException e1) { e1.printStackTrace(); } } }); btnNewButton.setBounds(56, 325, 113, 27); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("取消"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(""); textField_1.setText(""); } }); btnNewButton_1.setBounds(226, 325, 113, 27); contentPane.add(btnNewButton_1); } }