JAVA swing实现简单增删改查

前言

欢迎大家来到我的博客,请各位看客们点赞、收藏、关注三连!

欢迎大家关注我的知识库,Java之从零开始 · 语雀

你的关注就是我前进的动力!

CSDN专注于问题解决的博客记录,语雀专注于知识的收集与汇总,包括分享前沿技术。


 学完JAVA Swing,不加点餐???

Liunx 全操作过程 【JDK1.8配置、MySQL、Docker、mariaDB、nginx、tomcat、redis、各种问题解决总结】

JDK1.8新特性

UML统一建模语言

此次图形界面的增删改查,基本上非常简单,想要快速看原项目,请直接打开连接:链接:https://pan.baidu.com/s/1lFsOTH9D9Z-3W9yRDAVF7w 
提取码:435o 

关于各位新手不知道怎么启动,这个视频里有如何运行的过程。

链接:https://pan.baidu.com/s/1ud3qyL1bOqOSCFGoTbRDEg 
提取码:i90x 

首先,看看界面样子吧。

因为我用的eclipse开发的swing,所以我在eclipse下载了一个插件,如果你们导项目进去下载了插件,是可以进行图形化设计。而插件就自行百度就行,简便方式打开Help ->  Eclipse Marketplace  ->  直接搜索Windows,然后找到Windowsbuilder,下载下来,就可以使用了,如果没有搜索到,直接点击查询,将Windowsbuilder下载下来安装就行。还不行就按照网上的方式安装就行,下面几张截图,

废话不多说,直接贴代码

jframe主界面代码

package com.lisonglin.frame;


import java.awt.BorderLayout;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;


import com.lisonglin.model.Student;
import com.lisonglin.service.StudentService;
import com.lisonglin.util.DateUtil;


import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;


public class MainFrame extends JFrame {


	private JPanel contentPane;
	private JTable table;
	private String[] columnCount= {"序号","姓名","成绩","生日","城市"};
	private List<Student> list;
	public static Student stu;
	public static MainFrame frame;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new MainFrame();
					//窗口居中
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 764, 469);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(29, 58, 692, 332);
		contentPane.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quaryAll();
			}
		});
		button.setBounds(58, 22, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FromFjame().setVisible(true);
			}
		});
		button_1.setBounds(205, 22, 93, 23);
		contentPane.add(button_1);
		//全屏
//		setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		JButton button_2 = new JButton("修改");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				update();
				quaryAll();
			}
		});
		button_2.setBounds(357, 22, 93, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("删除");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove();
				quaryAll();
			}
		});
		button_3.setBounds(539, 22, 93, 23);
		contentPane.add(button_3);
		
	}
	//查询
	public void quaryAll() {
		StudentService ss=new StudentService();
		list = ss.queryAll();
		if(list==null) {
			JOptionPane.showMessageDialog(null, "服务器繁忙");
			return;
		}
		Object[][] data = DateUtil.listToArray(list);
		table.setModel(new DefaultTableModel(data, columnCount));
	}
	
	//删除
	private void remove() {
		int i = table.getSelectedRow();
		Student s = list.get(i);
		int code = new StudentService().delete(s.getId());
		if(code==0) {
			JOptionPane.showMessageDialog(null, "删除成功");
			return;
		}else {
			JOptionPane.showMessageDialog(null,DateUtil.errors.get(code) );
		}
		quaryAll();
	}
	
	//修改
	private void update() {
		int i = table.getSelectedRow();
		stu = list.get(i);
		new FromFjame().setVisible(true);
	}
}

jframe修改/增加界面

package com.lisonglin.frame;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;


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.JTextField;
import javax.swing.border.EmptyBorder;


import com.lisonglin.model.Student;
import com.lisonglin.service.StudentService;
import com.lisonglin.util.DateUtil;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;


public class FromFjame extends JFrame {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_3;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JComboBox comboBox_2;


	public FromFjame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 314, 436);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("添加学生");
		label.setFont(new Font("宋体", Font.PLAIN, 17));
		label.setBounds(118, 20, 78, 39);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("姓名");
		label_1.setBounds(23, 71, 40, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(87, 68, 155, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_2 = new JLabel("成绩");
		label_2.setBounds(23, 128, 40, 15);
		contentPane.add(label_2);
		
		textField_1 = new JTextField();
		textField_1.setBounds(87, 125, 155, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("生日");
		lblNewLabel.setBounds(23, 191, 32, 15);
		contentPane.add(lblNewLabel);
		
		JLabel label_3 = new JLabel("城市");
		label_3.setBounds(23, 251, 32, 15);
		contentPane.add(label_3);
		
		textField_3 = new JTextField();
		textField_3.setBounds(87, 248, 155, 21);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				addDay();
			}
		});
		comboBox.setBounds(87, 188, 54, 21);
		contentPane.add(comboBox);
		
		comboBox_1 = new JComboBox();
		comboBox_1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				addDay();
			}
		});
		comboBox_1.setBounds(151, 188, 47, 21);
		contentPane.add(comboBox_1);
		
		comboBox_2 = new JComboBox();
		comboBox_2.setBounds(202, 188, 40, 21);
		contentPane.add(comboBox_2);
		
		//选择框添加内容
		addBirth();
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(MainFrame.stu==null) {
					add();
				}else {
					update();
				}
			}
		});
		button.setBounds(37, 325, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("返回");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//每次返回清空信息
				MainFrame.stu=null;
				//退出
				dispose();
			}
		});
		button_1.setBounds(169, 325, 93, 23);
		contentPane.add(button_1);
		
		//当点击的行数的信息不为空时,进行下面操作
		if(MainFrame.stu!=null) {
			textField.setText(MainFrame.stu.getName());
			textField_1.setText(MainFrame.stu.getScore()+"");
			textField_3.setText(MainFrame.stu.getCity());
			LocalDate birth =MainFrame.stu.getBirth();
			comboBox.setSelectedItem(birth.getYear());
			comboBox_1.setSelectedItem(birth.getMonthValue());
			comboBox_2.setSelectedItem(birth.getDayOfMonth());
			button.setText("修改");
		}
	}


	//选择框填充内容
	private void addBirth() {
		int year=LocalDate.now().getYear();
		for(int i=1970;i<=year;i++) {
			comboBox.addItem(i);
		}
		for(int i=1;i<=12;i++) {
			comboBox_1.addItem(i);
		}
	}
	
	private void addDay() {
		int year=1970;
		int month=1;
		int day=0;
	    Object oYear = comboBox.getSelectedItem();
		Object oMonth =comboBox_1.getSelectedItem();
		if(oYear==null||oMonth==null) {
			return;
		}else {
			year=(int) oYear;
			month=(int) oMonth;
		}
		boolean flag=(year%4==0&&year%100!=0)||year%400==0;
		switch(month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				day=31;
				break;
			case 2:
				day=flag?28:29;
				break;
			default:
				day=30;
				break;
		}
		comboBox_2.removeAllItems();
		for(int i=1;i<=day;i++) {
			comboBox_2.addItem(i);
		}
	}
	
	//增加
	private void add() {
		String name=textField.getText();
		String strSouce=textField_1.getText();
		String city=textField_3.getText();
		int year=(int)comboBox.getSelectedItem();
		int month=(int) comboBox_1.getSelectedItem();
		int day=(int) comboBox_2.getSelectedItem();
		Student s=new Student(name,Double.parseDouble(strSouce),LocalDate.of(year, month, day),city);
		int insert = new StudentService().insert(s);		
		if(insert==0) {
			JOptionPane.showMessageDialog(null, "添加成功");
			textField.setText("");
			textField_1.setText("");
			textField_3.setText("");
			return;
		}else {
			JOptionPane.showMessageDialog(null, DateUtil.errors.get(insert));
		}
	}
	
	//修改
	private void update() {
		String name=textField.getText();
		String strSouce=textField_1.getText();
		String city=textField_3.getText();
		int year=(int)comboBox.getSelectedItem();
		int month=(int) comboBox_1.getSelectedItem();
		int day=(int) comboBox_2.getSelectedItem();
		MainFrame.stu.setName(name);
		MainFrame.stu.setScore(Double.parseDouble(strSouce));
		MainFrame.stu.setBirth(LocalDate.of(year, month, day));
		MainFrame.stu.setCity(city);
		int i = new StudentService().update(MainFrame.stu);
		if(i==0) {
			JOptionPane.showMessageDialog(null, "修改成功");
			MainFrame.stu=null;
			textField.setText("");
			textField_1.setText("");
			textField_3.setText("");
			MainFrame.frame.quaryAll();
			dispose();
			return;
		}else {
			JOptionPane.showMessageDialog(null, DateUtil.errors.get(i));
		}
	}
}

连接数据库工具类

package com.lisonglin.util;


import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;


import com.mchange.v2.c3p0.ComboPooledDataSource;


public class JdbcUtil {


	public static final ComboPooledDataSource ds=new ComboPooledDataSource();
	
	static 
	{
		//加载连接数据库的信息
		try {
			InputStream is =new FileInputStream("jdbcinfo.config");
			Properties porp =new Properties();
			porp.load(is);
			is.close();
			//获取连接数据库信息
			String user =porp.getProperty("user");
			String pwd =porp.getProperty("pwd");
			String url=porp.getProperty("url");
			String driver =porp.getProperty("driver");
			//设置链接库的信息
			ds.setUser(user);
			ds.setDriverClass(driver);
			ds.setJdbcUrl(url);
			ds.setPassword(pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void release(Connection conn) {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}

配置信息

user=root
pwd=123
url=jdbc:mysql://localhost:3306/test?userSSL=false
driver=com.mysql.jdbc.Driver

数据访问包--增删改查实现代码

package com.lisonglin.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.lisonglin.model.Student;
import com.lisonglin.util.DateUtil;
import com.lisonglin.util.JdbcUtil;


public class StudentDao {
	
	public void add(Student s) throws SQLException {
		//获取连接
		Connection conn = JdbcUtil.getConnection();
		//准备sql
		String sql="insert into stus values(null,?,?,?,?)";
		//获取PreparedStatement
		PreparedStatement ps=conn.prepareStatement(sql);
		//填充占位符
		ps.setString(1, s.getName());
		ps.setDouble(2, s.getScore());
		ps.setObject(3, s.getBirth());
		ps.setString(4, s.getCity());
		//执行sql
		ps.executeUpdate();
		//关闭连接
		JdbcUtil.release(conn);
	}
	
	public void detele(int id) throws SQLException {
		//获取连接
		Connection conn = JdbcUtil.getConnection();
		//准备sql
		String sql="delete from stus where id=?";
		//获取PreparedStatement
		PreparedStatement ps=conn.prepareStatement(sql);
		//填充占位符
		ps.setInt(1, id);
		//执行sql
		ps.executeUpdate();
		//关闭连接
		JdbcUtil.release(conn);
	}
	
	public void update(Student s) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql="update stus set name=?,score=?,birth=?,city=? where id=?";
		PreparedStatement ps=conn.prepareStatement(sql);
		ps.setString(1, s.getName());
		ps.setDouble(2, s.getScore());
		ps.setObject(3, s.getBirth());
		ps.setString(4, s.getCity());
		ps.setInt(5, s.getId());
		ps.executeUpdate();
		JdbcUtil.release(conn);
	}
	
	public List<Student> queryAll() throws SQLException{
		List<Student> stus=new ArrayList<>();
		//获取连接
		Connection conn = JdbcUtil.getConnection();
		//准备sql
		String sql="select * from stus";
		//获取PreparedStatement
		PreparedStatement ps=conn.prepareStatement(sql);
		//执行sql
		ResultSet rs = ps.executeQuery();
		while(rs.next()) {
			Student s=new Student();
			s.setId(rs.getInt(1));
			s.setName(rs.getString(2));
			s.setScore(rs.getDouble(3));
			s.setBirth(DateUtil.dateToLocalDate(rs.getDate(4)));
			s.setCity(rs.getString(5));
			stus.add(s);
		}
		//关闭连接
		JdbcUtil.release(conn);
		return stus;
	}
}

数据库就不贴了,打包里面有。

=========分割线===========

大家对于此项目有诸多环境问题,基本上私信我的都是那个jar包没有,数据库没有导入。

欢迎大家支持我!有稳定收入的各位可以支持一下,谢谢,支付宝二维码!

  • 40
    点赞
  • 326
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论
### 回答1: 这个问题的意思是如何用Java、MySQL和Swing实现一个简单增删改查管理系统。 为了实现这样一个系统,需要以下步骤: 1. 设计数据库表结构,包括需要存储的数据和各个表之间的关系。 2. 使用Java连接MySQL数据库,并编写代码读取和写入数据。 3. 编写Swing用户界面,包括各种表单、列表和按钮,以实现各种增删改查操作。 4. 将Java代码和数据库表结构打包成一个完整的可执行程序,安装并运行在目标计算机上。 通过以上几个步骤,就可以实现一个简单增删改查管理系统了。 ### 回答2: Java是一种广泛使用的编程语言,MySQL是一种常用的关系型数据库管理系统,SwingJava的GUI工具包。结合这三种技术,可以实现简单增删改查管理系统。 首先,需要连接MySQL数据库。可以使用Java提供的JDBC API来实现。可以通过以下步骤实现连接: 1. 加载MySQL驱动程序:使用Class.forName()方法加载驱动程序。 2. 建立连接:使用DriverManager.getConnection()方法建立与数据库的连接。 在连接建立之后,就可以进行增删改查操作了。可以使用JDBC的PreparedStatement对象来执行SQL语句。 增加数据: 1. 创建PreparedStatement对象。 2. 编写SQL语句。 3. 使用setXXX()方法给参数占位符赋值。 4. 调用executeUpdate()方法执行SQL语句,插入数据。 删除数据: 1. 创建PreparedStatement对象。 2. 编写SQL语句。 3. 使用setXXX()方法给参数占位符赋值。 4. 调用executeUpdate()方法执行SQL语句,删除数据。 修改数据: 1. 创建PreparedStatement对象。 2. 编写SQL语句。 3. 使用setXXX()方法给参数占位符赋值。 4. 调用executeUpdate()方法执行SQL语句,修改数据。 查询数据: 1. 创建PreparedStatement对象。 2. 编写SQL语句。 3. 调用executeQuery()方法执行SQL语句,查询数据。 4. 使用ResultSet对象获取数据。 在完成数据的增删改查之后,需要将数据显示到GUI界面上。可以使用Swing工具包来创建GUI界面。 首先,需要使用Swing组件来布置GUI界面。 例如,JTextField、JLabel、JButton、JPanel等组件可以布置图形用户界面。 其次,需要使用Swing事件处理机制来处理用户交互事件。包括按钮点击事件、鼠标事件等等。 最后,将Swing组件和数据查询、修改、删除、添加、刷新等操作紧密结合,就可以实现简单增删改查管理系统。 总之,结合Java编程语言、MySQL数据库和Swing界面工具包,可以很容易地实现一个简单增删改查管理系统。只要熟悉以上提到的技术,就能够轻松地完成这一任务。 ### 回答3: Java是一种广泛使用的面向对象编程语言,而MySQL是一种流行的关系型数据库管理系统。SwingJava中的一个GUI工具包,可以用来实现图形用户界面。对于一些管理系统,我们通常需要实现增删改查的操作,现在我们就来看看如何使用Java,MySQL和Swing实现一个简单增删改查系统。 首先,我们需要在MySQL中创建一个数据库和一个表来保存我们的数据,比如我们可以创建一个名为"management"的数据库,里面包含一个名为"users"的表,该表包含id、name、age、sex、address等字段。 接着,我们需要用Java编写代码来连接MySQL并执行相应的操作。我们可以使用JDBC(Java数据连接)API来实现这些操作,步骤如下: 1. 加载MySQL的JDBC驱动程序。 2. 通过DriverManager类创建数据库连接。 3. 创建Statement对象,用来执行SQL语句。 4. 使用ResultSet对象来处理查询结果。 5. 关闭连接。 在Java代码中我们可以定义一个User类代表我们要管理的用户对象,并在界面上实现相应的增删改查操作。在Swing中,我们可以使用不同的组件来实现图形用户界面,如JFrame、JPanel、JButton、JTextField等。我们可以将这些组件放置在对应的容器中,通过事件监听器来实现界面与操作的交互。 对于一个简单增删改查系统,我们可以考虑先实现查询操作。当用户点击查询按钮时,我们可以通过执行相应的SQL语句来查询数据库中的数据,并将结果展示在界面上。当用户点击添加、编辑或删除按钮时,我们需要弹出一个对话框来输入或修改数据,并将相应的操作结果保存到数据库中。 总的来说,通过Java、MySQL和Swing的结合,我们可以很方便地实现一个简单增删改查管理系统。这种方式可以帮助我们高效地保存和处理大量数据,并提高用户的使用体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hikktn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值