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. Typecho博客支持emoji表情设置

    介绍 大家在typecho博客写文章时,很多人都喜欢使用emoji表情(比如这些图标)但是typecho的数据库类型默认不支持emoji编码,因为Emoji是一种在Unicode位于u1F601-u1 ...

  2. es6 快速入门 系列 —— 类 (class)

    其他章节请看: es6 快速入门 系列 类 类(class)是 javascript 新特性的一个重要组成部分,这一特性提供了一种更简洁的语法和更好的功能,可以让你通过一个安全.一致的方式来自定义对象 ...

  3. Git安装详解

    官网地址: https://git-scm.com/ 查看 GNU 协议,可以直接点击下一步. 选择 Git 安装位置,要求是非中文并且没有空格的目录,然后下一步. Git 选项配置,推荐默认设置,然 ...

  4. ant -design vue a-tree 树形控件

    话不多说,先上代码. <a-tree v-if="this.treeData && this.treeData.length > 0" ref=&quo ...

  5. How to find out which process is listening upon a port

    When we covered port scanning a short while ago we discovered how to tell which ports had processes ...

  6. MySQL 5.7.19 简易安装、卸载教程

    前言:传统的 exe 文件安装的MySQL,安装后特别难卸载,而且一旦处理不好,就容易出错,想再安装别的版本也不容易.因为这种方式的安装,虽然是不断的下一步,但是卸载的时候需要处理很多,在本文最后,有 ...

  7. Lesson7——Pandas 使用自定义函数

    pandas目录 简介 如果想要应用自定义的函数,或者把其他库中的函数应用到 Pandas 对象中,有以下三种方法: 操作整个 DataFrame 的函数:pipe() 操作行或者列的函数:apply ...

  8. 无法加载 mcrypt (外链,英语) 扩展,请检查您的 PHP 配置。

    转载请注明来源:https://www.cnblogs.com/hookjc/ 需要安装libcrytp,在下面的地址下载libmarypt: ftp://mcrypt.hellug.gr/pub/c ...

  9. html路径

    一.HTML 相对路径和绝对路径区别分析 HTML初学者会经常遇到这样一个问题,如何正确引用一个文件.比如,怎样在一个HTML网页中引用另外一个HTML网页作为超链接(hyperlink)?怎样在一个 ...

  10. js判断当前浏览设备

    前端开发经常遇到需要判断用户的浏览设备,是pc端还是移动端,移动端使用的是什么手机系统?android.ios.ipad.windows phone等等,有时候还需要知道用户浏览页面是在微信中打开还是 ...