Alumni Information Management System - Sophomore Winter Vacation Computer Training

Table of contents

Zero, project function realization

        1. General function

        2. Code operation explanation

        3. For the operation settings of the interface

        4. Connect the table with the data in the database

        5. Some small operation codes of jtable

        6. Interface background setting

1. Operation interface display

2. Some code display and personal understanding

        1、AlumnusDao

        2、Alumnus

        3、AlumnusController

        4、AlumnusEditFrm

3. Blog sharing used

Four. Summary


Zero, project function realization

        1. General function

        Click on the table in the interface to modify its name, age, gender, location, etc., click to add alumni to add it, and update the data when returning to the table interface.

        2. Code operation explanation

        

         

         

         dao: add, delete, modify and check the information managed by them, and define function operations

         model: declare the required information elements, getter and setter functions, and constructors to set objects

        util: tool class - "connect to the database and judge whether the string is empty

        view: interface design and calls to corresponding code operations

        3. For the operation settings of the interface

        You can make corresponding settings in the plug-in windowbuilder in eclipse, the operation is simple, just look at the code after a lot of operations

        4. Connect the table with the data in the database

        It is necessary to set up a special function such as fillsalumnus to connect it and fill the form. The code is as follows

       

public void fillsalumnus(Alumnus alumnus) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();//获取数据库的连接
			ResultSet rs = alumnusDao.alumnusList(con, alumnus);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getInt("aid"));
				v.add(rs.getString("aname"));
				v.add(rs.getInt("aage"));
				v.add(rs.getString("agender"));
				v.add(rs.getString("afrom"));

				dtm.addRow(v);
			}//填充表格
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

        5. Some small operation codes of jtable

        Get the number of rows of the table clicked by the mouse:

        Note: The elements and columns of each row in the table are obtained from subscript 0, and the following is to get the element value of row 0, column 0 of the table, etc., pay attention to the corresponding data type

int aid = (int)this.table.getValueAt(hang,0);
String aname = (String) this.table.getValueAt(hang,1);
int aage;
aage = (int)this.table.getValueAt(hang,2);
String agender = (String) this.table.getValueAt(hang,3);
String afrom = (String) this.table.getValueAt(hang,4);
String achieve = this.textField_1.getText();//获取文本框textField_1中的字符串

        6. Interface background setting

        Select labels such as jlable (pull the size to the desired interface size, you can remember), remove the text, and set its icon (you can choose an absolute path, the size of the picture is the same as the frame size of jlable, so there is no need to adjust the size, the most labor-saving Method)

1. Operation interface display

      

2. Some code display and personal understanding

        1、AlumnusDao

package com.java.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.java.model.Alumnus;
import com.java.util.StringUtil;

public class AlumnusDao {

    //添加校友信息
    public int alumnusAdd(Connection con, Alumnus alumnus) throws Exception{
        String sql="insert into alumnus values(null,?,?,?,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);

        pstmt.setString(1, alumnus.getAname());
        pstmt.setInt(2, alumnus.getAage());
        pstmt.setString(3, alumnus.getAgender());
        pstmt.setString(4, alumnus.getAfrom());
        pstmt.setString(5, alumnus.getAchieve());

        return pstmt.executeUpdate();
    }

    //展示校友信息
    public ResultSet alumnusList(Connection con,Alumnus alumnus) throws Exception{
        StringBuffer sb=new StringBuffer("select * from alumnus");
        if(StringUtil.isNotEmpty(alumnus.getAname())) {
            sb.append(" and aname = '"+alumnus.getAname()+"'");
        }

        PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
        return pstmt.executeQuery();
    }

    //删除校友信息
    public int alumnusDelete(Connection con,String aname)throws Exception{
        String sql="delete from alumnus where aname=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, aname);
        return pstmt.executeUpdate();
    }

    //修改校友信息
    public int alumnusModify(Connection con, Alumnus alumnus)throws Exception{
        String sql="update alumnus set aage=?,agender=?,afrom=?,achieve=?,aname=? where aid=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setInt(1, alumnus.getAage());
        pstmt.setString(2, alumnus.getAgender());
        pstmt.setString(3, alumnus.getAfrom());
        pstmt.setString(4,alumnus.getAchieve());
        pstmt.setString(5,alumnus.getAname());
        pstmt.setInt(6, alumnus.getAid());
        return pstmt.executeUpdate();
    }
}

        2、Alumnus

package com.java.model;

/**
 * 校友Model类
 * @author somewon
 *
 */

public class Alumnus {

    private int aid;
    private String aname;
    private int aage;
    private String agender;
    private String afrom;
    private String achieve;

    public Alumnus() {
        super();
    }

    public Alumnus(String aname) {
        this.aname = aname;
    }

    public Alumnus(int aid, String aname, int aage, String agender, String afrom, String achieve) {
        super();
        this.aid = aid;
        this.aname = aname;
        this.aage = aage;
        this.agender = agender;
        this.afrom = afrom;
        this.achieve = achieve;
    }

    //成员get和set
    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public int getAage() {
        return aage;
    }

    public void setAage(int aage) {
        this.aage = aage;
    }

    public String getAgender() {
        return agender;
    }

    public void setAgender(String agender) {
        this.agender = agender;
    }

    public String getAfrom() {
        return afrom;
    }

    public void setAfrom(String afrom) {
        this.afrom = afrom;
    }

    public String getAchieve() {
        return achieve;
    }

    public void setAchieve(String achieve) {
        this.achieve = achieve;
    }
}

        3、AlumnusController

package com.java.view;

import com.java.dao.AlumnusDao;
import com.java.model.Alumnus;
import com.java.util.DbUtil;
import com.java.util.StringUtil;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AlumnusController extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	DbUtil dbUtil = new DbUtil();
	AlumnusDao alumnusDao = new AlumnusDao();
	private JTable table;
	private JTextField textField_1;
	/**
	 * Launch the application.
	 */
	public void fillsalumnus(Alumnus alumnus) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = alumnusDao.alumnusList(con, alumnus);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getInt("aid"));
				v.add(rs.getString("aname"));
				v.add(rs.getInt("aage"));
				v.add(rs.getString("agender"));
				v.add(rs.getString("afrom"));

				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	//成就的框显现文字
	protected void chengjiu() {
		// TODO Auto-generated method stub
		//选中行
		int hang = this.table.getSelectedRow();

		//获取数据
		String issuer = (String) this.table.getValueAt(hang,1);  //所在行的校友名字
		Alumnus alumnus = new Alumnus(issuer);
		Connection con = null;
		//获得连接
		try {
			con = dbUtil.getCon();
			ResultSet rs = alumnusDao.alumnusList(con, alumnus);
			if(rs.next()) {
				String chengjiu = rs.getString("achieve");
				this.textField_1.setText(chengjiu);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AlumnusController frame = new AlumnusController();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AlumnusController() {
		setFont(new Font("幼圆", Font.PLAIN, 14));
		setTitle("校友系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 633);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);

		JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBackground(new Color(199, 39, 12));
		desktopPane.setBounds(0, 0, 893, 590);
		contentPane.add(desktopPane);
//		this.fillsalumnus(new Alumnus());

		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.setBounds(533, 66, 69, 29);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				xiugai();
				fillsalumnus(new Alumnus());
			}
		});
		desktopPane.setLayout(null);
		btnNewButton_1.setBackground(new Color(255, 255, 255));
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 14));
		desktopPane.add(btnNewButton_1);

		JButton btnNewButton_1_2 = new JButton("删除");
		btnNewButton_1_2.setBounds(650, 66, 69, 29);
		btnNewButton_1_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Deletealumnus();
				fillsalumnus(new Alumnus());
			}
		});
		btnNewButton_1_2.setBackground(new Color(255, 255, 255));
		btnNewButton_1_2.setFont(new Font("宋体", Font.PLAIN, 14));
		desktopPane.add(btnNewButton_1_2);

		JButton btnNewButton_1_1 = new JButton("添加校友");
		btnNewButton_1_1.setBounds(755, 10, 115, 29);
		btnNewButton_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tianjia();
				fillsalumnus(new Alumnus());
			}
		});
		btnNewButton_1_1.setBackground(new Color(255, 255, 255));
		btnNewButton_1_1.setFont(new Font("宋体", Font.PLAIN, 14));
		desktopPane.add(btnNewButton_1_1);

		textField = new JTextField();
		textField.setBounds(533, 143, 213, 29);
		textField.setFont(new Font("宋体", Font.PLAIN, 14));
		textField.setColumns(10);
		desktopPane.add(textField);

		JButton btnNewButton = new JButton("Q");
		btnNewButton.setFont(new Font("微軟正黑體", Font.BOLD, 13));
		btnNewButton.setBounds(769, 142, 46, 29);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chaxun();
			}
		});
		desktopPane.add(btnNewButton);

		JButton btnNewButton_2 = new JButton("退出");
		btnNewButton_2.setBounds(533, 491, 69, 29);
		btnNewButton_2.setBackground(new Color(255, 255, 255));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tuichu();
			}
		});
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 14));
		desktopPane.add(btnNewButton_2);

		JButton btnNewButton_2_1 = new JButton("清除");
		btnNewButton_2_1.setBounds(650, 491, 69, 29);
		btnNewButton_2_1.setBackground(new Color(255, 255, 255));
		btnNewButton_2_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				qingchu();
			}
		});
		btnNewButton_2_1.setFont(new Font("宋体", Font.PLAIN, 14));
		desktopPane.add(btnNewButton_2_1);

		JLabel lblNewLabel_4 = new JLabel("成就");
		lblNewLabel_4.setBounds(533, 209, 49, 27);
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 18));
		desktopPane.add(lblNewLabel_4);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(2, 7, 474, 316);
		desktopPane.add(scrollPane);

		table = new JTable();
		table.setBackground(new Color(255, 255, 255));
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				chengjiu();
			}
		});
		table.setFont(new Font("宋体", Font.PLAIN, 12));
		table.setModel(new DefaultTableModel(
				new Object[][]{
				},
				new String[]{
						"id", "姓名", "\u5E74\u9F84", "\u6027\u522B", "\u6240\u5728\u5730\u533A"
				}
		));
		scrollPane.setViewportView(table);

		JLabel lblNewLabel_4_1 = new JLabel("姓名");
		lblNewLabel_4_1.setBounds(533, 115, 49, 27);
		lblNewLabel_4_1.setFont(new Font("宋体", Font.PLAIN, 18));
		desktopPane.add(lblNewLabel_4_1);

		textField_1 = new JTextField();
		textField_1.setBackground(new Color(255, 255, 255));
		textField_1.setBounds(533, 234, 286, 227);
		desktopPane.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdihaita1.png"));
		lblNewLabel.setBounds(174, 318, 453, 302);
		desktopPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1.setBounds(490, 108, 175, 80);
		desktopPane.add(lblNewLabel_1);

		JLabel lblNewLabel_1_1 = new JLabel("");
		lblNewLabel_1_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_1.setBounds(646, 107, 109, 84);
		desktopPane.add(lblNewLabel_1_1);

		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
		lblNewLabel_2.setBounds(492, 168, 164, 198);
		desktopPane.add(lblNewLabel_2);

		JLabel lblNewLabel_2_1 = new JLabel("");
		lblNewLabel_2_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
		lblNewLabel_2_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_2_1.setBounds(684, 166, 164, 198);
		desktopPane.add(lblNewLabel_2_1);

		JLabel lblNewLabel_2_2 = new JLabel("");
		lblNewLabel_2_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
		lblNewLabel_2_2.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_2_2.setBounds(500, 298, 164, 198);
		desktopPane.add(lblNewLabel_2_2);

		JLabel lblNewLabel_2_3 = new JLabel("");
		lblNewLabel_2_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
		lblNewLabel_2_3.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_2_3.setBounds(681, 301, 164, 198);
		desktopPane.add(lblNewLabel_2_3);

		JLabel lblNewLabel_1_2 = new JLabel("");
		lblNewLabel_1_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_2.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_2.setBounds(516, 52, 102, 53);
		desktopPane.add(lblNewLabel_1_2);

		JLabel lblNewLabel_1_2_1 = new JLabel("");
		lblNewLabel_1_2_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_2_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_2_1.setBounds(634, 52, 98, 55);
		desktopPane.add(lblNewLabel_1_2_1);

		JLabel lblNewLabel_1_2_1_1 = new JLabel("");
		lblNewLabel_1_2_1_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_2_1_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_2_1_1.setBounds(765, 134, 54, 48);
		desktopPane.add(lblNewLabel_1_2_1_1);

		JLabel lblNewLabel_1_3 = new JLabel("");
		lblNewLabel_1_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_3.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_3.setBounds(590, 190, 175, 80);
		desktopPane.add(lblNewLabel_1_3);

		JLabel lblNewLabel_1_3_1 = new JLabel("");
		lblNewLabel_1_3_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
		lblNewLabel_1_3_1.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_3_1.setBounds(581, 389, 175, 80);
		desktopPane.add(lblNewLabel_1_3_1);

		JLabel lblNewLabel_1_2_2 = new JLabel("");
		lblNewLabel_1_2_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_2_2.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_2_2.setBounds(520, 479, 102, 53);
		desktopPane.add(lblNewLabel_1_2_2);

		JLabel lblNewLabel_1_2_3 = new JLabel("");
		lblNewLabel_1_2_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_2_3.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_2_3.setBounds(642, 479, 93, 52);
		desktopPane.add(lblNewLabel_1_2_3);

		JLabel lblNewLabel_1_4 = new JLabel("");
		lblNewLabel_1_4.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_4.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_4.setBounds(177, 297, 175, 80);
		desktopPane.add(lblNewLabel_1_4);

		JLabel lblNewLabel_1_5 = new JLabel("");
		lblNewLabel_1_5.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
		lblNewLabel_1_5.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_1_5.setBounds(340, 293, 175, 80);
		desktopPane.add(lblNewLabel_1_5);

		JLabel lblNewLabel_3 = new JLabel("欢迎");
		lblNewLabel_3.setFont(new Font("华文行楷", Font.PLAIN, 40));
		lblNewLabel_3.setBounds(6, 337, 132, 74);
		desktopPane.add(lblNewLabel_3);

		JLabel lblNewLabel_3_1 = new JLabel("进入");
		lblNewLabel_3_1.setFont(new Font("华文行楷", Font.PLAIN, 40));
		lblNewLabel_3_1.setBounds(82, 401, 132, 74);
		desktopPane.add(lblNewLabel_3_1);

		this.fillsalumnus(new Alumnus());
	}

	protected void qingchu() {
		// TODO Auto-generated method stub
		textField.setText("");
		textField_1.setText("");
	}

	protected void tuichu() {
		// TODO Auto-generated method stub
		MainFrm MainFrm = new MainFrm();
		MainFrm.setVisible(true);
		this.setVisible(false);
	}

	//查询校友信息
	protected void chaxun() {
		// TODO Auto-generated method stub
		String aname = this.textField.getText();
		Alumnus alumnus = new Alumnus();
		alumnus.setAname(aname);

		//传入名字并调用查询方法
		this.fillsalumnus(alumnus);
	}

	//删除校友
	private void Deletealumnus() {
		// TODO Auto-generated method stub
		//选中需要删除的行
		int hang = this.table.getSelectedRow();
		if(hang==-1){
			JOptionPane.showMessageDialog(null, "请选中需要修改的行!");
		}

		//获取数据
		String issuer = (String) this.table.getValueAt(hang,1);

		//判断数据
		if (StringUtil.isEmpty(issuer)) {
			JOptionPane.showMessageDialog(null, "没有选择需要删除的校友信息!");
			return;
		}

		Alumnus alumnus = new Alumnus(issuer);
		Connection con = null;
		try {
			//获得连接
			con = dbUtil.getCon();
			int currentAlumnus = alumnusDao.alumnusDelete(con, issuer);
			if (currentAlumnus != 0) {
				JOptionPane.showMessageDialog(null, "删除成功!");
			} else {
				JOptionPane.showMessageDialog(null, "删除失败!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "删除失败!");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	//修改校友信息
	protected void xiugai() {
		// TODO Auto-generated method stub
		//获取表格数据
		int hang = this.table.getSelectedRow();
		if(hang==-1){
			JOptionPane.showMessageDialog(null, "请选中需要修改的行!");
		}
		int aid = (int)this.table.getValueAt(hang,0);
		String aname = (String) this.table.getValueAt(hang,1);
		int aage;
		aage = (int)this.table.getValueAt(hang,2);
		String agender = (String) this.table.getValueAt(hang,3);
		String afrom = (String) this.table.getValueAt(hang,4);
		String achieve = this.textField_1.getText();//获取文本框textField_1中的字符串

		//判断数据
		if ("".equals(String.valueOf(aid))) {
			JOptionPane.showMessageDialog(null, "校友id不能为空!");
			return;
		}
		if (StringUtil.isEmpty(aname)) {
			JOptionPane.showMessageDialog(null, "校友姓名不能为空!");
			return;
		}
		if ("".equals(String.valueOf(aage))) {
			JOptionPane.showMessageDialog(null, "校友年龄不能为空!");
			return;
		}
		if (StringUtil.isEmpty(agender)) {
			JOptionPane.showMessageDialog(null, "校友性别不能为空!");
			return;
		}
		if (StringUtil.isEmpty(afrom)) {
			JOptionPane.showMessageDialog(null, "校友所在区域不能为空!");
			return;
		}
		if (StringUtil.isEmpty(achieve)) {
			JOptionPane.showMessageDialog(null, "校友成就不能为空!");
			return;
		}

		Alumnus alumnus = new Alumnus(aid, aname, aage, agender, afrom, achieve);
		Connection con = null;
		try {
			//获得连接
			con = dbUtil.getCon();
			int currentAlumnus = alumnusDao.alumnusModify(con, alumnus);
			if (currentAlumnus != -1) {
				this.fillsalumnus(alumnus);
				JOptionPane.showMessageDialog(null, "修改成功!");
			} else {
				JOptionPane.showMessageDialog(null, "修改失败!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//JOptionPane.showMessageDialog(null, "注册失败");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	//添加校友信息
	protected void tianjia() {
		// TODO Auto-generated method stub
		//跳转界面
		AlumnusEditFrm AlumnusEditFrm = new AlumnusEditFrm();
		AlumnusEditFrm.setVisible(true);
		this.setVisible(false);
	}
}

        4、AlumnusEditFrm

package com.java.view;

import com.java.dao.AlumnusDao;
import com.java.model.Alumnus;
import com.java.util.DbUtil;
import com.java.util.StringUtil;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AlumnusEditFrm extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    DbUtil dbUtil = new DbUtil();
    AlumnusDao alumnusDao = new AlumnusDao();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AlumnusEditFrm frame = new AlumnusEditFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AlumnusEditFrm() {
        setTitle("校友信息编辑界面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 755, 505);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(199, 46, 18));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("姓名");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 18));
        lblNewLabel.setBounds(10, 98, 49, 27);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("年龄");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 18));
        lblNewLabel_1.setBounds(269, 98, 49, 27);
        contentPane.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("性别");
        lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 18));
        lblNewLabel_2.setBounds(10, 163, 49, 27);
        contentPane.add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("所在地区");
        lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 18));
        lblNewLabel_3.setBounds(269, 163, 82, 27);
        contentPane.add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("成就");
        lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 18));
        lblNewLabel_4.setBounds(10, 230, 49, 27);
        contentPane.add(lblNewLabel_4);

        textField = new JTextField();
        textField.setBounds(69, 99, 158, 27);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(328, 98, 158, 27);
        contentPane.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(69, 163, 158, 27);
        contentPane.add(textField_2);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(361, 163, 158, 27);
        contentPane.add(textField_3);

        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(69, 230, 282, 196);
        contentPane.add(textField_4);

        JButton btnNewButton = new JButton("添加");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tianjia();
            }
        });
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 14));
        btnNewButton.setBounds(456, 399, 76, 27);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("返回");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fanhui();
            }
        });
        btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 14));
        btnNewButton_1.setBounds(574, 399, 76, 27);
        contentPane.add(btnNewButton_1);

        JLabel lblNewLabel_3_1 = new JLabel("欢迎");
        lblNewLabel_3_1.setFont(new Font("华文行楷", Font.PLAIN, 40));
        lblNewLabel_3_1.setBounds(556, 68, 132, 74);
        contentPane.add(lblNewLabel_3_1);

        JLabel lblNewLabel_3_1_1 = new JLabel("进入");
        lblNewLabel_3_1_1.setFont(new Font("华文行楷", Font.PLAIN, 40));
        lblNewLabel_3_1_1.setBounds(609, 117, 132, 74);
        contentPane.add(lblNewLabel_3_1_1);

        JLabel lblNewLabel_5 = new JLabel("");
        lblNewLabel_5.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdihaita5.png"));
        lblNewLabel_5.setBounds(430, 205, 391, 263);
        contentPane.add(lblNewLabel_5);

        JLabel lblNewLabel_6 = new JLabel("");
        lblNewLabel_6.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
        lblNewLabel_6.setBounds(1, 88, 58, 43);
        contentPane.add(lblNewLabel_6);

        JLabel lblNewLabel_6_1 = new JLabel("");
        lblNewLabel_6_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
        lblNewLabel_6_1.setBounds(1, 159, 58, 35);
        contentPane.add(lblNewLabel_6_1);

        JLabel lblNewLabel_6_2 = new JLabel("");
        lblNewLabel_6_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
        lblNewLabel_6_2.setBounds(1, 222, 58, 35);
        contentPane.add(lblNewLabel_6_2);

        JLabel lblNewLabel_6_3 = new JLabel("");
        lblNewLabel_6_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
        lblNewLabel_6_3.setBounds(260, 88, 58, 43);
        contentPane.add(lblNewLabel_6_3);

        JLabel lblNewLabel_7 = new JLabel("");
        lblNewLabel_7.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
        lblNewLabel_7.setBounds(260, 159, 91, 39);
        contentPane.add(lblNewLabel_7);

        JLabel lblNewLabel_8 = new JLabel("");
        lblNewLabel_8.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
        lblNewLabel_8.setBounds(546, 61, 158, 118);
        contentPane.add(lblNewLabel_8);
    }

    protected void fanhui() {
        // TODO Auto-generated method stub
        AlumnusController AlumnusController = new AlumnusController();
        AlumnusController.setVisible(true);
        this.setVisible(false);
    }

    protected void tianjia() {
        // TODO Auto-generated method stub
        //获取表格数据
        int aid = -1;
        String aname = this.textField.getText();
        int aage = Integer.parseInt(this.textField_1.getText());
        String agender = this.textField_2.getText();
        String afrom = this.textField_3.getText();
        String achieve = this.textField_4.getText();//获取文本框textField_1中的字符串
        //判断数据
        if ("".equals(String.valueOf(aid))) {
            JOptionPane.showMessageDialog(null, "校友id不能为空!");
            return;
        }
        if (StringUtil.isEmpty(aname)) {
            JOptionPane.showMessageDialog(null, "校友姓名不能为空!");
            return;
        }
        if ("".equals(String.valueOf(aage))) {
            JOptionPane.showMessageDialog(null, "校友年龄不能为空!");
            return;
        }
        if (StringUtil.isEmpty(agender)) {
            JOptionPane.showMessageDialog(null, "校友性别不能为空!");
            return;
        }
        if (StringUtil.isEmpty(afrom)) {
            JOptionPane.showMessageDialog(null, "校友所在区域不能为空!");
            return;
        }
        if (StringUtil.isEmpty(achieve)) {
            JOptionPane.showMessageDialog(null, "校友成就不能为空!");
            return;
        }

        Alumnus alumnus = new Alumnus(aid, aname, aage, agender, afrom, achieve);
        Connection con = null;
        try {
            //获得连接
            con = dbUtil.getCon();
            int currentAlumnus = alumnusDao.alumnusAdd(con, alumnus);
            if (currentAlumnus != -1) {
                JOptionPane.showMessageDialog(null, "添加成功!");
                this.setVisible(false);
                AlumnusController AlumnusController = new AlumnusController();
                AlumnusController.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, "添加失败!");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //JOptionPane.showMessageDialog(null, "注册失败");
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

3. Blog sharing used

        1. Mysql download and installation: It is recommended to download the simplest mysql folder here

                You can directly search for download and installation suggestions on csdn

        2. The eclipse plug-in window builder download suggestion: re-download the latest version of eclipse and then download and install

        3. The database connection that comes with idea is really comfortable

Four. Summary

        How to put it, after the training, generally speaking, it is not very useful, it just gives me one more reason to stay up late. You can use this to get to know the big guys around you, look at the code, the substitute teacher should have the corresponding code to share, what kind of library management system, student information management system, the codes are similar, if you want to do it well, you can Design the interface a lot. After all, the skin package that comes with windobuilder is really not very good-looking. You can search a lot to see how to set up the front end. There are still many things that can be improved for this small project. The code is for reference only, and the specific design is still It would be better to have a big guy with great art work.

Guess you like

Origin blog.csdn.net/somewon/article/details/128643737