JAVA swing实现简单增删改查

 

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

首先,看看界面样子吧。JAVA swing实现简单增删改查_第1张图片

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

JAVA swing实现简单增删改查_第2张图片JAVA swing实现简单增删改查_第3张图片JAVA swing实现简单增删改查_第4张图片

废话不多说,直接贴代码

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 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 queryAll() throws SQLException{
		List 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包没有,数据库没有导入,在这里一一给你们讲解,有问题可以加QQ群聊,如果你想要博主写的博客都可以在此群提出。此群是没有几个人,只有大学同学几人,如果加入此群,也请大家不要打扰他们。

QQ群号:

399497938

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

JAVA swing实现简单增删改查_第5张图片

 

 

你可能感兴趣的:(JAVA swing实现简单增删改查)