Student management system(学生信息管理系统)

dao包

StudentInformationDao.java

package dao;
import java.sql.Connection;


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

import model.StudentInformation;
import util.StringUtil;

/**
 * @author TongShuHao
 * @date 2020年12月15日 
 * @version 1.0  
 * Description: 对学生信息进行增、删、改、查
 */
public class StudentInformationDao {
	/**
	 * 学生添加
	 */
	public int add(Connection con,StudentInformation studentInformation)throws Exception{
		String sql="insert into stu_information values(?,?,?,?,?,?)";//学生添加sql语句
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, studentInformation.getSno());
		pstmt.setString(2, studentInformation.getSname());
		pstmt.setString(3, studentInformation.getSsex());
		pstmt.setString(4, studentInformation.getSsite()); 
		pstmt.setString(5, studentInformation.getSphone());
		pstmt.setString(6, studentInformation.getSmajor());
		return pstmt.executeUpdate();
	}
	/**
	 * 学生信息查询
	 */
	public ResultSet list(Connection con,StudentInformation studentInformation)throws Exception{
		StringBuffer sb=new StringBuffer("select * from stu_information");
				if (StringUtil.isNotEmpty(studentInformation.getSno())) {
					sb.append(" and Sno like '%"+studentInformation.getSno()+"%'");
				}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		return pstmt.executeQuery();
	}
	/**
	 * 学生信息删除
	 */
	public int delete(Connection con,String Sno)throws Exception{
		String sql="delete from stu_information where Sno=?";//学生信息删除语句
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, Sno);
		return pstmt.executeUpdate();
	}
	/**
	 * 学生信息修改
	 */
	public int update(Connection con,StudentInformation studentInformation)throws Exception{
		String sql="update stu_information set Sname=?,Ssex=?,Ssite=?,Sphone=?,Smajor=? where Sno=?";//学生信息修改
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, studentInformation.getSname());
		pstmt.setString(2, studentInformation.getSsex());
		pstmt.setString(3, studentInformation.getSsite());
		pstmt.setString(4, studentInformation.getSphone());
		pstmt.setString(5, studentInformation.getSmajor());
		pstmt.setString(6, studentInformation.getSno());
		return pstmt.executeUpdate();
	}
}

UserDao.java

package dao;

import java.sql.Connection;

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

import model.User;

/**
 * @author TongShuHao
 * @date 2020年12月14日 
 * @version 1.0  
 * Description: 用户访问数据库
 */
public class UserDao {
	//登陆验证
	public User login(Connection con,User user)throws Exception {
		User resulUser=null;
		String sql="select * from stu_user where username=? and password=?";//查找数据库接口
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassWord());
		ResultSet rs=pstmt.executeQuery();
		if (rs.next()) {
			resulUser=new User();
			resulUser.setId(rs.getInt("id"));
			resulUser.setUserName(rs.getString("userName"));
			resulUser.setPassWord("passWord");
		}
		return resulUser;
	}
}

model包

StudentInformation.java

package model;
/**
 * @author TongShuHao
 * @date 2020年12月15日 
 * @version 1.0  
 * Description:学生信息实体 
 */
public class StudentInformation {
	private String Sno;//学号
	private String Sname;//姓名
	private String Ssex;//性别
	private String Ssite;//地址
	private String Sphone;//电话
	private String Smajor;//专业
	public String getSno() {
		return Sno;
	}
	public void setSno(String sno) {
		Sno = sno;
	}
	public String getSname() {
		return Sname;
	}
	public void setSname(String sname) {
		Sname = sname;
	}
	public String getSsex() {
		return Ssex;
	}
	public void setSsex(String ssex) {
		Ssex = ssex;
	}
	public String getSsite() {
		return Ssite;
	}
	public void setSsite(String ssite) {
		Ssite = ssite;
	}
	public String getSphone() {
		return Sphone;
	}
	public void setSphone(String sphone) {
		Sphone = sphone;
	}
	public String getSmajor() {
		return Smajor;
	}
	public void setSmajor(String smajor) {
		Smajor = smajor;
	}
	public StudentInformation() {
		super();
	}
	public StudentInformation(String Sno, String Sname, String Ssex, String Ssite, String Sphone, String Smajor) {
		super();
		this.Sno = Sno;
		this.Sname = Sname;
		this.Ssex = Ssex;
		this.Ssite = Ssite;
		this.Sphone = Sphone;
		this.Smajor = Smajor;
	}
	
	

}

User.java

package model;
/**
 * @author TongShuHao
 * @date 2020年12月14日 
 * @version 1.0  
 * Description: 用户的实体:用户名和密码
 */
public class User {
	private int id;//编号
	private String userName;//用户名
	private String passWord;//密码
	private int is_admin;//是否管理员
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public int getIs_admin() {
		return is_admin;
	}
	public void setIs_admin(int is_admin) {
		this.is_admin = is_admin;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String userName, String passWord) {
		super();
		this.userName = userName;
		this.passWord = passWord;
	}
	
	
	
}

util包

DBU.java

package util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author TongShuHao
 * @date 2020年12月14日 
 * @version 1.0  
 * Description: 连接数据库
 */
public class DBU {
	
	private String dbUrl="jdbc:mysql://localhost:3306/bigdata1901";//数据库的连接地址
	private String dbUserName="root";//数据库用户名
	private String dbPassWord="246810";//数据库密码
	private String jdbcName="com.mysql.jdbc.Driver";//驱动名称
	
	///数据库连接
	public Connection getCon()throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassWord);
		return con;
	}
	
	///数据库关闭
	public void closeCon(Connection con)throws Exception{
		if(con!=null){
			con.close();
		}
	}
	///测试数据库是否连接
	public static void main(String[] args) {
		DBU dbu=new DBU();
		try {
			dbu.getCon();
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

StringUtil.java

package util;
/**
 * @author TongShuHao
 * @date 2020年12月14日 
 * @version 1.0  
 * Description: 处理字符串工具类
 */
public class StringUtil {
	///判断是否为空
	public static boolean isEmpty(String str) {
		if (str==null||"".equals(str.trim())) {
			return true;
		}else{
			return false;
		}
	}
	///判断不是空,是不是多余?????    不多余!!!!!但是条件要写对
	public static boolean isNotEmpty(String str) {
		if ((str!=null)&&!"".equals(str.trim())) {
			return true;
		}else{
			return false;
		}
	}
}

view包

LogOnFrm.java

package view;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import dao.UserDao;
import model.User;
import util.DBU;
import util.StringUtil;
import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
/**
 * @author TongShuHao
 * @date 2020年12月14日 
 * @version 1.0  
 * Description: 
 */
public class LogOnFrm extends JFrame {
	private JPanel contentPane;
	private JTextField userName;
	private JPasswordField passWord;
	private DBU dbu=new DBU();
	private UserDao userDao=new UserDao();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogOnFrm frame = new LogOnFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public LogOnFrm() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(LogOnFrm.class.getResource("/img/Ltitle.jpg")));
		setTitle("\u5927\u6570\u636E1901\u73ED\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 525);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel label = new JLabel("\u5C71\u897F\u519C\u4E1A\u5927\u5B66\u8F6F\u4EF6\u5B66\u9662");
		label.setFont(new Font("华文新魏", Font.BOLD, 30));
		
		JLabel label_1 = new JLabel("\u5927\u6570\u636E1901\u73ED\u5B66\u751F\u4FE1\u606F\u7BA1\u7406");
		label_1.setFont(new Font("华文隶书", Font.PLAIN, 20));
		
		JLabel label_2 = new JLabel("\u5B66\u53F7\uFF1A");
		label_2.setFont(new Font("等线", Font.PLAIN, 15));
		
		JLabel label_3 = new JLabel("\u5BC6\u7801\uFF1A");
		label_3.setFont(new Font("等线", Font.PLAIN, 15));
		
		userName = new JTextField();
		userName.setColumns(10);
		
		JButton loginBut = new JButton("\u767B\u9646");
		loginBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		JButton resetBut = new JButton("\u91CD\u7F6E");
		resetBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetActionPerformed(e);
			}
		});
		JLabel label_4 = new JLabel("\u5236\u4F5C\u4EBA\uFF1A\u8F6F\u4EF6\u5B66\u9662\u5927\u6570\u636E1901\u73ED\u7AE5\u4E66\u6D69     \u8054\u7CFB\u7535\u8BDD\uFF1A18855428512");
		
		passWord = new JPasswordField();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(166)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(label_1)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(loginBut)
										.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(resetBut))
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(label_2)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(userName))
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(label_3)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(passWord, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(126)
							.addComponent(label))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(47)
							.addComponent(label_4)))
					.addContainerGap(65, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(52)
					.addComponent(label)
					.addGap(47)
					.addComponent(label_1)
					.addGap(62)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2)
						.addComponent(userName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(31)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_3)
						.addComponent(passWord, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(59)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(loginBut)
						.addComponent(resetBut))
					.addPreferredGap(ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
					.addComponent(label_4)
					.addContainerGap())
		);
		contentPane.setLayout(gl_contentPane);
		///设置窗口居中显示
		this.setLocationRelativeTo(null);
	}
	
	///用户登陆
	protected void loginActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String userName=this.userName.getText();
		String passWord=new String(this.passWord.getPassword());
		if (StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "学号不能为空");
			return;
		}
		if (StringUtil.isEmpty(passWord)) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		User user=new User(userName,passWord);
		Connection con=null;
		try {
			con=dbu.getCon();
			User currenUser=userDao.login(con, user);
			if (currenUser!=null) {
				//JOptionPane.showMessageDialog(null, "登陆成功");
				dispose();//销毁当前窗口
				new MainFrm().setVisible(true);
			}else{
				JOptionPane.showMessageDialog(null, "学号或密码错误");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				dbu.closeCon(con);///关闭数据库
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	///重置操作
	private void resetActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		this.userName.setText("");
		this.passWord.setText("");
	}
}

MainFrm.java

package view;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JDesktopPane;
import java.awt.Color;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.TextField;
import java.awt.Font;

/**
 * @author TongShuHao
 * @date 2020年12月15日 
 * @version 1.0  
 * Description: 系统主界面
 */
public class MainFrm extends JFrame {

	 private JPanel contentPane;
	 private JDesktopPane tables=null;
	 private JDesktopPane table;

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

	/**
	 * Create the frame.
	 */
	public MainFrm() {
		setTitle("\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrm.class.getResource("/img/Ltitle.jpg")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 525);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("\u4FE1\u606F\u5F55\u5165");
		menuBar.add(mnNewMenu);
		
		JMenuItem menuItem = new JMenuItem("\u5F55\u5165");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					StudentInformationAddFrm studentInformationAddFrm = new StudentInformationAddFrm();
					studentInformationAddFrm.setVisible(true);
					table.add(studentInformationAddFrm);
			}
		});
		mnNewMenu.add(menuItem);
		
		JMenu menu = new JMenu("\u4FE1\u606F\u67E5\u8BE2");
		menuBar.add(menu);
		
		JMenuItem menuItem_1 = new JMenuItem("\u67E5\u8BE2");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				StudentInformationSelectFrm studentInformationSelectFrm = new StudentInformationSelectFrm();
				studentInformationSelectFrm.setVisible(true);
				table.add(studentInformationSelectFrm);
			}
		});
		menu.add(menuItem_1);
		
		JMenu menu_1 = new JMenu("\u4FE1\u606F\u66F4\u65B0");
		menuBar.add(menu_1);
		
		JMenuItem menuItem_3 = new JMenuItem("\u66F4\u65B0");
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StudentInformationAlterFrm studentInformationAlterFrm = new StudentInformationAlterFrm();
				studentInformationAlterFrm.setVisible(true);
				table.add(studentInformationAlterFrm);
			}
		});
		menu_1.add(menuItem_3);
		
		JMenu menu_2 = new JMenu("\u4FE1\u606F\u5220\u9664");
		menuBar.add(menu_2);
		
		JMenuItem menuItem_4 = new JMenuItem("\u5220\u9664");
		menu_2.add(menuItem_4);
		
		JMenu menu_3 = new JMenu("\u9000\u51FA\u7A0B\u5E8F");
		menuBar.add(menu_3);
		
		JMenuItem menuItem_2 = new JMenuItem("\u9000\u51FA\u5E94\u7528");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int result=JOptionPane.showConfirmDialog(null, "是否退出程序?");
				if (result==0) {
					dispose();//关闭程序	
				}
			}
		});
		menu_3.add(menuItem_2);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		 table = new JDesktopPane();
		table.setBackground(Color.LIGHT_GRAY);
		contentPane.add(table, BorderLayout.CENTER);
		
		TextField textField = new TextField();
		textField.setEditable(false);
		textField.setFont(new Font("Dialog", Font.BOLD, 40));
		textField.setBackground(Color.LIGHT_GRAY);
		textField.setText("\u5927\u6570\u636E1901\u7AE5\u4E66\u6D69");
		
		TextField textField_1 = new TextField();
		textField_1.setBackground(Color.LIGHT_GRAY);
		textField_1.setFont(new Font("Dialog", Font.BOLD, 40));
		textField_1.setEditable(false);
		textField_1.setText("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF");
		GroupLayout gl_table = new GroupLayout(table);
		gl_table.setHorizontalGroup(
			gl_table.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_table.createSequentialGroup()
					.addGap(119)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 341, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_table.createSequentialGroup()
					.addGap(147)
					.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 294, GroupLayout.PREFERRED_SIZE))
		);
		gl_table.setVerticalGroup(
			gl_table.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_table.createSequentialGroup()
					.addGap(106)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
					.addGap(52)
					.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
		);
		table.setLayout(gl_table);
		///设置窗口最大化
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		///设置窗口居中显示
		this.setLocationRelativeTo(null);
	}
}

StudentInformationSelectFrm.java

package view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.StudentInformationDao;
import model.StudentInformation;
import util.DBU;
import util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author TongShuHao
 * @date 2020年12月18日 
 * @version 1.0  
 * Description: 
 */
public class StudentInformationSelectFrm extends JInternalFrame {
	private JTextField snoTxt;
	private JTable StudentInformationTable;
	private DBU dbu = new DBU();
	private StudentInformationDao studentInformationDao = new StudentInformationDao();
	private JTextField SnoTxt;
	private JTextField SnameTxt;
	private JTextField SsexTxt;
	private JTextField SsiteTxt;
	private JTextField SphoneTxt;
	private JTextField SmajorTxt;

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

	/**
	 * Create the frame.
	 */
	public StudentInformationSelectFrm() {
		setFrameIcon(new ImageIcon(StudentInformationSelectFrm.class.getResource("/img/Ltitle.jpg")));
		setIconifiable(true);
		setClosable(true);
		setTitle("\u5B66\u751F\u4FE1\u606F\u67E5\u8BE2");
		setBounds(100, 100, 600, 525);
		
		JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u5E26\u67E5\u8BE2\u5B66\u751F\u7684\u5B66\u53F7\uFF1A");
		lblNewLabel.setFont(new Font("等线", Font.BOLD, 15));
		
		snoTxt = new JTextField();
		snoTxt.setColumns(10);
		
		JButton Searchbutton = new JButton("\u67E5\u8BE2");
		Searchbutton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentSnoSearchAction(e);
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u4FE1\u606F\u4FEE\u6539\u4E0E\u5220\u9664", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(59)
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(snoTxt, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(Searchbutton))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(29)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 529, GroupLayout.PREFERRED_SIZE)
								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 530, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(25, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(47)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(snoTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(Searchbutton))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
					.addGap(38))
		);
		
		JLabel lblNewLabel_1 = new JLabel("\u5B66\u53F7\uFF1A");
		
		SnoTxt = new JTextField();
		SnoTxt.setEditable(false);
		SnoTxt.setColumns(10);
		
		JLabel label = new JLabel("\u59D3\u540D\uFF1A");
		
		SnameTxt = new JTextField();
		SnameTxt.setColumns(10);
		
		JLabel label_1 = new JLabel("\u6027\u522B\uFF1A");
		
		SsexTxt = new JTextField();
		SsexTxt.setColumns(10);
		
		JLabel label_2 = new JLabel("\u5730\u5740\uFF1A");
		
		SsiteTxt = new JTextField();
		SsiteTxt.setColumns(10);
		
		JLabel label_3 = new JLabel("\u7535\u8BDD\uFF1A");
		
		SphoneTxt = new JTextField();
		SphoneTxt.setColumns(10);
		
		JLabel label_4 = new JLabel("\u4E13\u4E1A\uFF1A");
		
		SmajorTxt = new JTextField();
		SmajorTxt.setColumns(10);
		
		JButton button = new JButton("\u66F4\u65B0");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentInformationUpdateActionEvent(e);
			}
		});
		
		JButton button_1 = new JButton("\u5220\u9664");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentInformationDeleteActionEvent(e);
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(35)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(SnoTxt, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
							.addGap(72)
							.addComponent(label)
							.addGap(5)
							.addComponent(SnameTxt, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(label_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(SsexTxt, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
									.addGap(42)
									.addComponent(label_2))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(label_3)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(button)
										.addComponent(SphoneTxt))))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
								.addComponent(SsiteTxt, Alignment.LEADING)
								.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
									.addComponent(label_4)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(SmajorTxt, GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
								.addComponent(button_1))))
					.addContainerGap(61, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_1)
								.addComponent(SnoTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(14)
							.addComponent(SnameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(SsexTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2)
						.addComponent(SsiteTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_3)
						.addComponent(SphoneTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_4)
						.addComponent(SmajorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		StudentInformationTable = new JTable();
		StudentInformationTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent evt) {
				studentInformationMousePressed(evt);
			}
		});
		StudentInformationTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u5B66\u53F7", "\u59D3\u540D", "\u6027\u522B", "\u5730\u5740", "\u7535\u8BDD", "\u4E13\u4E1A"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(StudentInformationTable);
		getContentPane().setLayout(groupLayout);
		//调用初始化表格
		this.fillTable(new StudentInformation());
	}
	/**
	 * 学生信息删除
	 * @param e
	 */
	private void studentInformationDeleteActionEvent(ActionEvent e) {
		// TODO Auto-generated method stub
		String Sno=SnoTxt.getText();//获取待删除的学生学号
		 if (StringUtil.isEmpty(Sno)) {
				JOptionPane.showMessageDialog(null, "请输入待删除学生的学号");
				return;
			}
		 int n=JOptionPane.showConfirmDialog(null, "是否确认删除该学生的信息?");
		 if (n==0) {
			Connection con=null;
			try {
				con=dbu.getCon();
				int deleteNum=studentInformationDao.delete(con, Sno);
				if (deleteNum==1) {
					JOptionPane.showMessageDialog(null, "删除成功!");
					resetValue();
					fillTable(new StudentInformation());
				}else{
					JOptionPane.showMessageDialog(null, "删除失败!");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally{
				try {
					dbu.closeCon(con);//关闭数据库
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}

	/**
	 * 学生信息更新
	 * @param e
	 */
	private void studentInformationUpdateActionEvent(ActionEvent e) {
		// TODO Auto-generated method stub
		 String Sno=SnoTxt.getText();
		 String Sname=SnameTxt.getText();
		 String Ssex=SsexTxt.getText();
		 String Ssite=SsiteTxt.getText();
		 String Sphone=SphoneTxt.getText();
		 String Smajor=SmajorTxt.getText();
		 if (StringUtil.isEmpty(Sno)) {
			JOptionPane.showMessageDialog(null, "请输入待更新学生的学号");
			return;
		}
		 if (StringUtil.isEmpty(Sname)) {
				JOptionPane.showMessageDialog(null, "姓名不能为空");
				return;
			}
		 if (StringUtil.isEmpty(Ssex)) {
				JOptionPane.showMessageDialog(null, "性别不能为空");
				return;
			}
		 if (StringUtil.isEmpty(Ssite)) {
				JOptionPane.showMessageDialog(null, "地址不能为空");
				return;
			}
		 if (StringUtil.isEmpty(Sphone)) {
				JOptionPane.showMessageDialog(null, "电话不能为空");
				return;
			}
		 if (StringUtil.isEmpty(Smajor)) {
				JOptionPane.showMessageDialog(null, "专业不能为空");
				return;
			}
		 StudentInformation studentInformation=new StudentInformation(Sno,Sname,Ssex,Ssite,Sphone,Smajor);
		 Connection con=null;
		 try {
			con=dbu.getCon();
			int modifyNum=studentInformationDao.update(con, studentInformation);
			if (modifyNum==1) {
				JOptionPane.showMessageDialog(null, "更新成功!");
				resetValue();
				fillTable(new StudentInformation());
			}else {
				JOptionPane.showMessageDialog(null, "更新失败!");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally{
			try {
				dbu.closeCon(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 重置表单操作
	 */
	private void resetValue() {
		// TODO Auto-generated method stub
		this.SnoTxt.setText("");
		this.SnameTxt.setText("");
		this.SsexTxt.setText("");
		this.SsiteTxt.setText("");
		this.SphoneTxt.setText("");
		this.SmajorTxt.setText("");
		
	}

	/**
	 * 鼠标单机事件,在表单里显示信息
	 */
	private void studentInformationMousePressed(MouseEvent evt) {
		// TODO Auto-generated method stub
		int row=StudentInformationTable.getSelectedRow();
		SnoTxt.setText((String)StudentInformationTable.getValueAt(row, 0));
		SnameTxt.setText((String)StudentInformationTable.getValueAt(row, 1));
		SsexTxt.setText((String)StudentInformationTable.getValueAt(row, 2));
		SsiteTxt.setText((String)StudentInformationTable.getValueAt(row, 3));
		SphoneTxt.setText((String)StudentInformationTable.getValueAt(row, 4));
		SmajorTxt.setText((String)StudentInformationTable.getValueAt(row, 5));
	}

	/**
	 * 学生查询
	 * @param e
	 */
	private void studentSnoSearchAction(ActionEvent evt) {
		// TODO Auto-generated method stub
		String sno=this.snoTxt.getText();
		StudentInformation studentInformation=new StudentInformation();
		studentInformation.setSno(sno);
		this.fillTable(studentInformation);

	}

	/**
	 * 初始化表格
	 */
	private void fillTable(StudentInformation studentInformation){
		DefaultTableModel dtm=(DefaultTableModel)StudentInformationTable.getModel();
		dtm.setRowCount(0);
		Connection con=null;
		try {
			con=dbu.getCon();
			ResultSet rs=studentInformationDao.list(con, studentInformation);
			while (rs.next()) {
				Vector v=new Vector();
				v.add(rs.getString("Sno"));//可多添加
				v.add(rs.getString("Sname"));
				v.add(rs.getString("Ssex"));
				v.add(rs.getString("Ssite"));
				v.add(rs.getString("Sphone"));
				v.add(rs.getString("Smajor"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				dbu.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

你可能感兴趣的:(mysql,java,数据库)