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. golang中内存地址计算-根据内存地址获取下一个内存地址对应的值

    package main import ( "fmt" "unsafe" ) func main() { // 根据内存地址获取下一个字节内存地址对应的值 da ...

  2. 计算机/linux启动过程

    开机过程指的是从打开计算机电源直到LINUX显示用户登录画面的全过程. 分析LINUX开机过程也是深入了解LINUX核心工作原理的一个很好的途径. 计算机开机过程是一个非常复杂的过程,想真正理解透彻并 ...

  3. Error: xz compression not available解决办法

    centos6升级php时误安装: rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm rpm -Uvh https://mir ...

  4. /etc/crontab和crontab -e的区别

    (1) /etc/crontab是系统级别的crontab,系统的设置等,这种方法只有root用户能用 crontab -e是用户级的crontab,会被写到 /var/spool/cron 目录下, ...

  5. C++ 微信多开

    应用是如何判断多开 一.通过查找窗口标题或者类名来判断程序是否正在运行. 二.通过互斥对象确定程序是否运行,大多数软件都是使用CreateMutexW 判断多开的. 三.内存映射物理文件,控制多开. ...

  6. 使用require.context实现前端工程自动化

    require.context是什么 一个webpack的api,通过执行require.context函数获取一个特定的上下文,主要用来实现自动化导入模块,在前端工程中,如果遇到从一个文件夹引入很多 ...

  7. Redis 的持久化有哪几种方式?

    面试题 redis 的持久化有哪几种方式? 不同的持久化机制都有什么优缺点? 持久化机制具体底层是如何实现的? 面试官心理分析 redis 如果仅仅只是将数据缓存在内存里面,如果 redis 宕机了再 ...

  8. Objects、Arrays、Collectors、System工具类

    Objects类 定义 位于java.util包中,JDK1.7以后操作对象的类,对对象的空,对象是否相等进行判断. 常用方法 1.public static boolean equals(Objec ...

  9. php导出excel xml word

    转载请注明来源:https://www.cnblogs.com/hookjc/ Excel: <?php header("Content-Type: application/vnd.m ...

  10. 开发时Blocks跟Delegates如何选择----董鑫

    1.大多数delegate protocols 都拥有几个消息源. 以GKMatch为例(A GKMatch object provides a peer-to-peer network betwee ...