1. 程式人生 > >用JDBC+GUI做的一個簡單的學生管理系統

用JDBC+GUI做的一個簡單的學生管理系統

剛學完JDBC不久,做了一個簡單的學生管理系統,可能還有不完善的地方,望各路大神見諒。廢話不多說,我先貼個圖讓大家讓大家瞅瞅,覺得是你想要的再看下去吧。



我是以管理者的身份去做的,適合初學者去學習。

在做之前,先捋一遍思路,簡單來說分為三大步。

一、在資料庫裡建Student表存放學生資訊

二、用JDBC來連線、操作資料庫

三、展示Student資料,實現增刪改查功能。

思路是非常簡單的,但是要實現還是有很多細節需要注意,下面我就貼上我的程式碼,結合著程式碼給大家一步步的分析說明。

實現:

一、在資料庫建表:這個不用細說,直接貼圖。


二、用JDBC連線資料庫:這一塊對於剛剛學JDBC的同學來說可能比較繞,所以我把這一塊又分成了四部分(最後的db.properties跟com.student.db一起的),我會逐個說明。看圖。


(1)com.student.db包裡有兩個類,一個是DBHelper 一個是DBManager,這倆類是用JDBC連線資料庫的,固定寫法。

DBManager類

package com.student.db;

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.student.mapper.IMapper;
public class DBManager {
//這裡把JDBC連線資料庫的步驟(找驅動,建連線,建通道,執行SQL)封裝在DBHelper類裡面,在DBManager裡用getConnection()呼叫。這樣寫的目的是方便
	public Connection getConnection(){
		try {
			return DBHelper.getConnection();//得到DBHelper類裡面寫好的連線
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
//增刪改結果集。因為sql語句是變化的,所以設為引數比較方便。params是佔位符的,沒學的可以忽略。
	public int executeUpdate(String sql,Object[] params){
		Connection conn=null;
		PreparedStatement pst=null;
		try {
			conn=getConnection();//連線
			pst=conn.prepareStatement(sql);//通道
			if(params != null){//佔位符的應用。
				for(int i=0;i<params.length;i++){
					pst.setObject(i+1,params[i]);//往通道里放資料,佔位符下標從1開始。
				}
			}
			return pst.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
//查詢結果集。比增刪改要複雜一些,慢慢看。
//這裡的IMapper是將所有可能的用到的類都放進去,方便以後繼承使用。(現在我們寫的是Student資訊,以後可能會有Teacher資訊,Class資訊等等)
//用介面是因為介面多繼承,方便維護升級
	public List executeQuery(String sql,IMapper mapper,Object []params){
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rst=null;//查詢結果集
		List list=new ArrayList();//用一個集合存放Student資訊
		try {
		    conn=getConnection();
			pst=conn.prepareStatement(sql);
			if(params != null){
				for(int i=0;i<params.length;i++){
					pst.setObject(i+1,params[i]);
				}
			}
			rst=pst.executeQuery();//把通道里的資料放入結果集
			list=mapper.map(rst);//IMapper裡有個map介面,裡面存著結果集資料。把結果集的資料放入list集合
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	public int count(String sql){//分頁查詢 count代表頁數。
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rst=null;
		try {
			conn=getConnection();
			pst=conn.prepareStatement(sql);
			rst=pst.executeQuery();
			while(rst.next()){
				return rst.getInt(1);//sql語句是select count(*) from stu,顯示的是count值,就是一行。
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;		
	}
	
}

DBHelper類。在寫之前先建一個properties檔案,名字為db.properties(如圖),注意不要建在包裡面。


package com.student.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBHelper {
	private static String DRIVER;
	private static String URL;
	private static String USER;
	private static String PASSWORD;
	static{
		Properties pro=new Properties();
		InputStream in=DBHelper.class.getClassLoader()
				.getResourceAsStream("db.properties");//讀取檔案資料
		try {
			pro.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		DRIVER=pro.getProperty("DRIVER");
		URL=pro.getProperty("URL");
		USER=pro.getProperty("USER");
		PASSWORD=pro.getProperty("PASSWORD");
		
	}
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException{
		Class.forName(DRIVER);//找驅動
		return DriverManager.getConnection(URL, USER, PASSWORD);//建連線
	}

}

(2)com.student.vo包。這裡面有一個vo類,我們是要把資料庫裡的資料放到java裡展示,用一個類物件把資料庫裡的資訊一一對應起來就可以很容易的操作。資料庫裡的一個列對應類物件的一個屬性。

package com.student.vo;

public class Student {
	private String stuid;
	private String name;
	private String age;
	private String sex;
	
	public String getStuid(){
		return stuid;
	}
	public void setStuid(String stuid){
		this.stuid=stuid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Student(String stuid,String name,String sex,String age){
		super();
		this.stuid=stuid;
		this.name=name;
		this.age=age;
		this.sex=sex;
	}
	public Student(){
		super();
	}
}

(3)com.student.mapper包。這裡面一個介面,一個實現類。

介面:

package com.student.mapper;

import java.sql.ResultSet;
import java.util.List;

public interface IMapper {
	List map(ResultSet rst);//宣告一個方法存著結果集。
}

實現類:

package com.student.mapper;

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

import com.student.vo.Student;
public class StuMapper implements IMapper {//實現介面方法
	public List map(ResultSet rst) {
		List<Student> list=new ArrayList<Student>();//建一個集合,裡面是Student類裡的資訊。
		try {
			while(rst.next()){//
				Student stu=new Student();
				stu.setStuid(rst.getString("STUID"));//類物件每一個屬性對應資料庫的每一列。
				stu.setName(rst.getString("STUNAME"));
				stu.setAge(rst.getString("AGE"));
				stu.setSex(rst.getString("SEX"));
				list.add(stu);//把類物件放到集合裡
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}

(4)com.student.dao包:這裡面的StuDAO類放著增刪改查分頁等功能

package com.student.dao;

import java.util.List;

import com.student.db.DBManager;
import com.student.mapper.IMapper;
import com.student.mapper.StuMapper;
import com.student.vo.Student;

public class StuDAO {
	public List<Student> check(){//檢視
		String sql="select * from STUDENT";//sql語句
		DBManager db=new DBManager();
		IMapper mapper=new StuMapper();//實現StuMapper
		List<Student> list=db.executeQuery(sql, mapper,null);//null是指佔位符為null,因為檢視的是所有資訊
		return list;
	}
	public boolean add(Student stu){//新增
		String sql="insert into STUDENT values(?,?,?,?)";
		Object[] params={stu.getStuid(),stu.getName(),stu.getAge(),stu.getSex()};
		DBManager db=new DBManager();
		int i=db.executeUpdate(sql, params);
		if(i>=0){
			System.out.println("成功");
		}else{
			System.out.println("失敗");
		}
		return true;
	}
	public boolean update(Student stu){//修改
		String sql="update STUDENT set stuname=?,age=?,sex=? where stuid=?";
		Object params[]={stu.getName(),stu.getAge(),stu.getSex(),stu.getStuid()};
		DBManager db=new DBManager();
		int i=db.executeUpdate(sql, params);
		if(i>=0){
			System.out.println("成功");
		}else{
			System.out.println("失敗");
		}
		return true;
	}
	public boolean delete(Student stu){//刪除
		String sql="delete from STUDENT where stuid=?";
		Object params[]={stu.getStuid()};
		DBManager db=new DBManager();
		int i=db.executeUpdate(sql, params);
		if(i>=0){
			System.out.println("成功");
		}else{
			System.out.println("失敗");
		}
		return true;
	}
	public List<Student> findPage(int pagesize,int pagenow){//分頁
		String sql="select * from (select rownum rn ,stu .* from stu) "
				+ "where rownum<=? and rn>?";//分頁公式
		Object []params={pagesize,(pagenow-1)*pagesize};
		DBManager db=new DBManager();
		IMapper mapper=new StuMapper();
		return db.executeQuery(sql, mapper, params);
	}
	public int findcount(){
		String sql="select count(*) from stu";
		DBManager db=new DBManager();
		return db.count(sql);
	}
	
}


當把這一塊寫完之後,其實大部分就已經完成了,JDBC連線資料庫基本上是固定的,多寫幾遍就明白了。

三、展示Student資訊,實現增刪改查。看圖:


(1)com.student.show包,展示介面:這裡面內容比較多,但是都很容易理解

package com.student.show;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;


import com.student.add.Add;
import com.student.check.Check;
import com.student.dao.StuDAO;
import com.student.delete.Delete;
import com.student.update.Update;
import com.student.vo.Student;


public class Show extends JFrame {
	public static int pagesize=5;//每頁顯示5條資訊
	public static int pagenow=1;//當前頁為第一頁
	public Show() {
		setSize(500, 430);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);//點X號就是關閉
		setResizable(false);//不可改變視窗大小
		setLocationRelativeTo(null);//預設居中顯示
        	setLayout(null);//採用座標佈局

		StuDAO dao = new StuDAO();//前面我們已經把增刪改查分頁寫到StuDAO裡面,現在就直接拿出來用
		List<Student> list =dao.findPage(pagesize, pagenow);
		Student stu = new Student();
		for (int i = 0; i < list.size(); i++) {
			stu = list.get(i);
		}
		String[] rowName = { "學號", "姓名", "年齡", "性別" };//從這裡開始是二維陣列的遍歷使用
		Object[][] data = new Object[list.size()][4];
		for (int i = 0; i < list.size(); i++) {
			Student s = list.get(i);
			Object st[] = { s.getStuid(), s.getName(), s.getAge(), s.getSex() };
			data[i] = st;
		}
		final JTable table = new JTable(data,rowName);
		JScrollPane JSP=new JScrollPane(table);//這一步不能省去,否則顯示不出列名
		JSP.setBounds(20, 10, 400, 200);
		add(JSP);
		
		
		JButton jb11=new JButton("首頁");
		jb11.setBounds(40,220,80,30);
		add(jb11);
		JButton jb22=new JButton("上一頁");
		jb22.setBounds(130,220,80,30);
		add(jb22);
		JButton jb33=new JButton("下一頁");
		jb33.setBounds(220,220,80,30);
		add(jb33);
		JButton jb44=new JButton("尾頁");
		jb44.setBounds(310,220,80,30);
		add(jb44);


		JButton jb1 = new JButton("檢視資訊");
		jb1.setBounds(50, 270, 100, 30);
		add(jb1);
		JButton jb2 = new JButton("修改資訊");
		jb2.setBounds(280, 270, 100, 30);
		add(jb2);
		JButton jb3 = new JButton("新增資訊");
		jb3.setBounds(50, 320, 100, 30);
		add(jb3);
		JButton jb4 = new JButton("刪除資訊");
		jb4.setBounds(280, 320, 100, 30);
		add(jb4);
		JButton jb5 = new JButton("退出");
		jb5.setBounds(280, 360, 100, 30);
		add(jb5);


		jb1.addActionListener(new ActionListener() {//檢視 
			public void actionPerformed(ActionEvent event) {
				int row = table.getSelectedRow();//選中第幾行
				int index = 0;
				if(row==-1){
					JOptionPane.showMessageDialog(null,"您沒有選中資訊");
					return;
				}
				String id = (String) table.getValueAt(row, index);// 跟Check聯絡起來
				Check check=new Check(id);
				check.setVisible(true);
				setVisible(false);	
			}
		});


		jb2.addActionListener(new ActionListener() {//修改
			public void actionPerformed(ActionEvent event) {
				int row = table.getSelectedRow();
				int index = 0;
				if(row==-1){
					JOptionPane.showMessageDialog(null,"您沒有選中資訊");
					return;
				}
				String id = (String) table.getValueAt(row, index);// 跟Update聯絡起來
				Update up=new Update(id);
				up.setVisible(true);
				setVisible(false);


			}
		});


		jb3.addActionListener(new ActionListener() {//新增
			public void actionPerformed(ActionEvent event) {
				Add add = new Add();
				add.setVisible(true);
				setVisible(false);
			}
		});
		jb4.addActionListener(new ActionListener() {//刪除
			public void actionPerformed(ActionEvent event) {
				int row = table.getSelectedRow();
				int index = 0;
				if(row==-1){
					JOptionPane.showMessageDialog(null,"您沒有選中資訊");
					return;
				}
				String num=(String) table.getValueAt(row, index);
				Delete d=new Delete(num);
				d.setVisible(true);
				setVisible(false);
				
			}
		});
		jb11.addActionListener(new ActionListener() {//首頁
			public void actionPerformed(ActionEvent event) {
				pagenow=1;
				Show show=new Show();
				setVisible(false);
				show.setVisible(true);
			}
		});
		jb22.addActionListener(new ActionListener() {//上一頁
			public void actionPerformed(ActionEvent event) {
				if(pagenow != 1){
					pagenow=pagenow-1;
				}else{
					return;
				}
				Show show=new Show();
				setVisible(false);
				show.setVisible(true);
			}
		});
		jb33.addActionListener(new ActionListener() {//下一頁
			public void actionPerformed(ActionEvent event) {
				StuDAO dao=new StuDAO();
				int count=dao.findcount();
				int pageCount=(count-1)/pagesize+1;//pageCount表示最後一頁
				if(pagenow != pageCount){
					pagenow=pagenow+1;
				}else{
					return;
				}
				Show show=new Show();
				setVisible(false);
				show.setVisible(true);
			}
		});
		jb44.addActionListener(new ActionListener() {//尾頁
			public void actionPerformed(ActionEvent event) {
				StuDAO dao=new StuDAO();
				int count=dao.findcount();
				int pageCount=(count-1)/pagesize+1;
				pagenow=pageCount;
				Show show=new Show();
				setVisible(false);
				show.setVisible(true);
			}
		});
	}


	public static void main(String args[]) {
		Show s = new Show();
	}
}


(2)增刪改查:大同小異,因為我們在StuDAO裡面已經寫好了,在用的時候就方便多了。


①新增:

package com.student.add;


import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.student.dao.StuDAO;
import com.student.db.DBManager;
import com.student.show.Show;
import com.student.vo.Student;


public class Add extends JFrame{
	public Add(){
		setSize(300,400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		
		JLabel j0=new JLabel("新增資訊");
		j0.setBounds(100,20,80,30);
		add(j0);
		
		JLabel j1=new JLabel("學號:");
		j1.setBounds(30,70,50,30);
		add(j1);
		
		final JTextField jt1=new JTextField();
		jt1.setBounds(100,70,130,30);
		add(jt1);
		
		JLabel j2=new JLabel("姓名:");
		j2.setBounds(30,120,50,30);
		add(j2);
		
		final JTextField jt2=new JTextField();
		jt2.setBounds(100,120,130,30);
		add(jt2);
		
		JLabel j3=new JLabel("性別:");
		j3.setBounds(30,170,50,30);
		add(j3);
		
		final JTextField jt3=new JTextField();
		jt3.setBounds(100,170,130,30);
		add(jt3);
		
		JLabel j4=new JLabel("年齡:");
		j4.setBounds(30,220,50,30);
		add(j4);
		
		final JTextField jt4=new JTextField();
		jt4.setBounds(100,220,130,30);
		add(jt4);
		
		JButton jb1=new JButton("新增");
		jb1.setBounds(50,280,80,30);
		add(jb1);
		
		JButton jb2=new JButton("返回");
		jb2.setBounds(150,280,80,30);
		add(jb2);
		
		jb1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				String a=jt1.getText();//獲取輸入的資訊
				String b=jt2.getText();
				String c=jt3.getText();
				String d=jt4.getText();
				Student stu=new Student(a,b,c,d);
				StuDAO dao=new StuDAO();
				List<Student> list=dao.check();//呼叫StuDAO裡面的check()方法
				for(Student st:list){//遍歷集合
					if(st.getStuid().equals(a)){
						JOptionPane.showMessageDialog(null,"該賬號存在");
						return;
					}
				}
				dao.add(stu);
				JOptionPane.showMessageDialog(null,"新增成功");
				Show show=new Show();
				show.setVisible(true);
				setVisible(false);
			}
		});
		jb2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Show s=new Show();
				s.setVisible(true);
				setVisible(false);
			}
		});
	}
	public static void main(String []args){
		Add add=new Add();
	}
}

②修改:

package com.student.update;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.student.dao.StuDAO;
import com.student.db.DBManager;
import com.student.show.Show;
import com.student.vo.Student;

public class Update extends JFrame{
	public Update(final String id){
		setSize(300,400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		
		JLabel j0=new JLabel("修改資訊");
		j0.setBounds(100,20,80,30);
		add(j0);
		
		JLabel j1=new JLabel("學號:");
		j1.setBounds(30,70,50,30);
		add(j1);
		
		final JLabel jt1=new JLabel();
		jt1.setBounds(100,70,130,30);
		add(jt1);
		
		JLabel j2=new JLabel("姓名:");
		j2.setBounds(30,120,50,30);
		add(j2);
		
		final JTextField jt2=new JTextField();
		jt2.setBounds(100,120,130,30);
		add(jt2);
		
		JLabel j3=new JLabel("年齡:");
		j3.setBounds(30,170,50,30);
		add(j3);
		
		final JTextField jt3=new JTextField();
		jt3.setBounds(100,170,130,30);
		add(jt3);
		
		JLabel j4=new JLabel("性別:");
		j4.setBounds(30,220,50,30);
		add(j4);
		
		final JTextField jt4=new JTextField();
		jt4.setBounds(100,220,130,30);
		add(jt4);
		
		JButton jb1=new JButton("修改");
		jb1.setBounds(50,280,80,30);
		add(jb1);
		
		JButton jb2=new JButton("返回");
		jb2.setBounds(150,280,80,30);
		add(jb2);
		
		StuDAO dao=new StuDAO();
		List<Student> list=dao.check();
		Student stu=new Student();
		for(int i=0;i<list.size();i++){//遍歷,找到與id相同的學號。
			stu=list.get(i);
			if(stu.getStuid().equals(id)){//id是引數,跟前面Show聯絡起來。
				break;
			}
		}
		jt1.setText(stu.getStuid());
		jt2.setText(stu.getName());
		jt3.setText(stu.getAge());
		jt4.setText(stu.getSex());
		
		jb1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				StuDAO dao=new StuDAO();
				Student stu=new Student();
				stu.setStuid(id);
				stu.setName(jt2.getText());
				stu.setAge(jt3.getText());
				stu.setSex(jt4.getText());
				dao.update(stu);//StuDAO裡的update()已經寫好如何修改,這裡直接用
				JOptionPane.showMessageDialog(null,"修改成功");
				Show show=new Show();
				show.setVisible(true);
				setVisible(false);
			}
		});
		jb2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Show s=new Show();
				s.setVisible(true);
				setVisible(false);
			}
		});
	}


}

③檢視:

package com.student.check;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.student.dao.StuDAO;
import com.student.show.Show;
import com.student.vo.Student;

public class Check extends JFrame{
	public Check(String id) {
		setSize(300,400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		
		JLabel j0=new JLabel("學生資訊");
		j0.setBounds(100,20,80,30);
		add(j0);
		
		JLabel j1=new JLabel("學號:");
		j1.setBounds(30,70,50,30);
		add(j1);
		
		final JLabel jt1=new JLabel();
		jt1.setBounds(100,70,130,30);
		add(jt1);
		
		JLabel j2=new JLabel("姓名:");
		j2.setBounds(30,120,50,30);
		add(j2);
		
		final JLabel jt2=new JLabel();
		jt2.setBounds(100,120,130,30);
		add(jt2);
		
		JLabel j3=new JLabel("年齡:");
		j3.setBounds(30,170,50,30);
		add(j3);
		
		final JLabel jt3=new JLabel();
		jt3.setBounds(100,170,130,30);
		add(jt3);
		
		JLabel j4=new JLabel("性別:");
		j4.setBounds(30,220,50,30);
		add(j4);
		
		final JLabel jt4=new JLabel();
		jt4.setBounds(100,220,130,30);
		add(jt4);
		
		JButton jb1=new JButton("確認");
		jb1.setBounds(50,280,80,30);
		add(jb1);
		
		JButton jb2=new JButton("返回");
		jb2.setBounds(150,280,80,30);
		add(jb2);
		
		StuDAO dao=new StuDAO();
		List<Student> list=dao.check();
		Student stu=new Student();
		for(int i=0;i<list.size();i++){
			stu=list.get(i);
			if(stu.getStuid().equals(id)){
				break;
			}
		}
		jt1.setText(stu.getStuid());
		jt2.setText(stu.getName());
		jt3.setText(stu.getAge());
		jt4.setText(stu.getSex());
		
		jb1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Show s=new Show();
				s.setVisible(true);
				setVisible(false);
			}
		});
		jb2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Show s=new Show();
				s.setVisible(true);
				setVisible(false);
			}
		});
		
	}

}

④刪除:

package com.student.delete;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.student.dao.StuDAO;
import com.student.db.DBManager;
import com.student.show.Show;
import com.student.vo.Student;


public class Delete extends JFrame{
	public Delete(final String num){
		setSize(300,400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		
		JLabel j0=new JLabel("您確認要刪除該資訊嗎");
		j0.setBounds(100,20,200,30);
		add(j0);
		
		JLabel j1=new JLabel("學號:");
		j1.setBounds(30,70,50,30);
		add(j1);
		
		final JLabel jt1=new JLabel();
		jt1.setBounds(100,70,130,30);
		add(jt1);
		
		JLabel j2=new JLabel("姓名:");
		j2.setBounds(30,120,50,30);
		add(j2);
		
		final JLabel jt2=new JLabel();
		jt2.setBounds(100,120,130,30);
		add(jt2);
		
		JLabel j3=new JLabel("年齡:");
		j3.setBounds(30,170,50,30);
		add(j3);
		
		final JLabel jt3=new JLabel();
		jt3.setBounds(100,170,130,30);
		add(jt3);
		
		JLabel j4=new JLabel("性別:");
		j4.setBounds(30,220,50,30);
		add(j4);
		
		final JLabel jt4=new JLabel();
		jt4.setBounds(100,220,130,30);
		add(jt4);
		
		JButton jb1=new JButton("確認");
		jb1.setBounds(20,280,80,30);
		add(jb1);
		
		JButton jb2=new JButton("返回");
		jb2.setBounds(180,280,80,30);
		add(jb2);
		
		StuDAO dao=new StuDAO();
		List<Student> list=dao.check();
		Student stu=new Student();
		for(int i=0;i<list.size();i++){
			stu=list.get(i);
			if(stu.getStuid().equals(num)){
				break;
			}
		}
		
		jt1.setText(stu.getStuid());
		jt2.setText(stu.getName());
		jt3.setText(stu.getAge());
		jt4.setText(stu.getSex());
		
		jb1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				StuDAO dao=new StuDAO();
				Student stu=new Student();
				stu.setStuid(num);
				dao.delete(stu);
				JOptionPane.showMessageDialog(null,"刪除成功");
				Show show=new Show();
				show.setVisible(true);
				setVisible(false);
			}
		});
		jb2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				Show s=new Show();
				s.setVisible(true);
				setVisible(false);
			}
		});
	}

}

最後貼一下登入頁面,因為是以管理者的身份登入的不需要判斷,就非常簡單:


package com.student.login;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.student.show.Show;

public class Login extends JFrame{
	public Login(){
		setSize(300,250);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);
		setLayout(null);
		
		JLabel j=new JLabel("登入視窗");
		j.setBounds(100,20,80,30);
		add(j);
		
		JLabel j1=new JLabel("使用者名稱:");
		j1.setBounds(50,80,60,30);
		add(j1);
		
		final JTextField jt1=new JTextField();
		jt1.setBounds(120,80,120,30);
		add(jt1);
		
		JLabel j2=new JLabel("密    碼:");
		j2.setBounds(50,130,60,30);
		add(j2);
		
		final JPasswordField jp=new JPasswordField();
		jp.setBounds(120,130,120,30);
		add(jp);
		
		JButton jb1=new JButton("登入");
		jb1.setBounds(70,180,60,30);
		add(jb1);
		
		JButton jb2=new JButton("重置");
		jb2.setBounds(170,180,60,30);
		add(jb2);
		
		jb1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				String id=jt1.getText();
				char ch[]=jp.getPassword();
				String pass=new String(ch);
				if(id.equals(abcdefj){//設定使用者名稱為abcdefj
					if(pass.equals(123456)){//設定密碼為123456
						JOptionPane.showMessageDialog(null,"登入成功");
						Show s=new Show();//成功後跳到Show
						s.setVisible(true);
						setVisible(false);
					}else{
						JOptionPane.showMessageDialog(null,"密碼錯誤");
						jt1.setText("");
						return;
					}
				}else{
					JOptionPane.showMessageDialog(null,"您輸入的賬號有誤");
					jt1.setText("");
					jp.setText("");
					return;
				}
			}
		});
	}
	public static void main(String []args){
		Login lo=new Login();
	}
}

寫在最後:

剛開始學的時候感覺很繞,尤其是JDBC那,後來發現,是因為前面java基礎掌握的不行,我又回去好好複習了java基礎,才發現JDBC是死的,固定的寫法,背過就行了。所以再做這個學生管理系統,就感覺不復雜了。先有一個大的思路,然後順著思路往下走,逐步實現每個功能。