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

  

Java课程设计---添加学生的更多相关文章

  1. Java课程设计报告——学生成绩管理系统

    一.需求分析 1.数据存储在数据库和文件中 2.分为"教师"模块和"学生"模块. 3.学生模块提供登陆功能,登陆成功后可查询数学.Java.体育成绩 (学生学号 ...

  2. Java课程设计---删除学生

    1.界面已经在上次修改操作的过程添加完成 2.在StudentDao中添加删除方法 public boolean delete(int id) throws SQLException { DbUtil ...

  3. Java课程设计---修改学生基本信息

    1.修改窗体 2.在StudentDao中增加修改学生信息的方法 /** * 修改的方法 * * @param student * @return * @throws SQLException */ ...

  4. Java课程设计---浏览学生(实现根据姓名查询)

    1.修改窗口 2.在StudentDao中增加根据姓名查找的方法 public List<Student> getStudent(String name)throws SQLExcepti ...

  5. Java课程设计---浏览学生(表格的使用)

    1.创建显示表格的窗体 package com.student.view; import java.awt.EventQueue; import javax.swing.JFrame; import ...

  6. Java课程设计---索引

    一.基础配置 ============================================================== 1.Java课程设计---Eclipse基本环境配置 2.J ...

  7. Java课程设计—学生成绩管理系统(201521123004-林艺如)

    1.团队课程设计博客 团队课程设计博客链接 2.个人负责模块或任务说明 ①.Menu Menu.jsp 在页面中给出提示,用HTML的 MenuTeacher.jsp 利用Menu.jsp进行具体化完 ...

  8. Java课程设计——学生成绩管理系统(201521123003 董美凤)

    Java课程设计--学生成绩管理系统(201521123003 董美凤) 1.团队课程设计博客链接 学生成绩管理系统博客链接 2.个人负责模块或任务说明 信息修改 密码修改 部分界面设计 3.自己的代 ...

  9. Java课程设计----仿Windows标准型计算器

    JAVA课程设计 仿Windows标准型计算器(By Yanboooooooo) 一.团队介绍: 连燕波[组长]:网络1513学生. 张文博[组员]:网络1513学生. 二.项目git地址 码云项目地 ...

随机推荐

  1. CTFSHOW-SSRF篇

    之前就想着写一下 ctfshow 的 wp, 但由于时间问题,一直没有机会, 其实是懒≥.≤ 这次趁着寒假刷几篇ctfshow的文章 那,开始吧. web351 存在一个flag.php页面,访问会返 ...

  2. docker和K8s对应参数

    创建 Pod 时设置命令及参数 创建 Pod 时,可以为其下的容器设置启动时要执行的命令及其参数.如果要设置命令,就填写在配置文件的 command 字段下,如果要设置命令的参数,就填写在配置文件的  ...

  3. debian老版本下载地址

    https://cdimage.debian.org/cdimage/archive/

  4. 《Effective TypeScript》条款22 - 类型收缩

    本文主要记录书中关于TypeScript类型收缩的内容 本文主要内容如下 类型收缩的一些方法 条件判断 抛错误 instanceof 和 in 属性检查 "标签联合"或" ...

  5. 面向计算机视觉的深度学习 | iBooker&#183;ApacheCN

    原文:Deep Learning for Computer Vision 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 不要担心自己的形象,只关心如何实现目标.--<原则>,生 ...

  6. 求一个number数组中的最大值和最小值的差

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  7. JFrame 的层次结构 及 背景设置说明

    感谢原文:https://blog.csdn.net/qq_32006373/article/details/49659129 一.JFrame 的层次结构 我们通过两个图来说明一下 JFrame 的 ...

  8. Window 共享内存

    转载请注明来源:https://www.cnblogs.com/hookjc/ C++使用共享内存实现进程间通信文件映射是一种实现进程间单向或双向通信的机制.它允许两个或多个本地进程间相互通信.为了共 ...

  9. java中静态代码块详解

    感谢大佬:https://blog.csdn.net/qq_35868412/article/details/89360250 今天在项目中看到这行代码,静态代码块,很久没用静态代码块了,今天来复习一 ...

  10. linux安装python3.6.6和新建虚拟环境

    基础准备 修改本地时区 cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 安装epel yum源 yum -y install epel- ...