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





我是以管理者的身份去做的,適合初學(xué)者去學(xué)習。
在做之前,先捋一遍思路,簡單來說分為三大步。
一、在數(shù)據(jù)庫里建Student表存放學(xué)生信息
二、用JDBC來連接、操作數(shù)據(jù)庫
三、展示Student數(shù)據(jù),實現(xiàn)增刪改查功能。
思路是非常簡單的,但是要實現(xiàn)還是有很多細節(jié)需要注意,下面我就貼上我的代碼,結(jié)合著代碼給大家一步步的分析說明。
實現(xiàn):
一、在數(shù)據(jù)庫建表:這個不用細說,直接貼圖。

二、用JDBC連接數(shù)據(jù)庫:這一塊對于剛剛學(xué)JDBC的同學(xué)來說可能比較繞,所以我把這一塊又分成了四部分(最后的db.properties跟com.student.db一起的),我會逐個說明。看圖。

(1)com.student.db包里有兩個類,一個是DBHelper 一個是DBManager,這倆類是用JDBC連接數(shù)據(jù)庫的,固定寫法。
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連接數(shù)據(jù)庫的步驟(找驅(qū)動,建連接,建通道,執(zhí)行SQL)封裝在DBHelper類里面,在DBManager里用getConnection()調(diào)用。這樣寫的目的是方便
public Connection getConnection(){
try {
return DBHelper.getConnection();//得到DBHelper類里面寫好的連接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//增刪改結(jié)果集。因為sql語句是變化的,所以設(shè)為參數(shù)比較方便。params是占位符的,沒學(xué)的可以忽略。
public int executeUpdate(String sql,Object[] params){
Connection conn=null;
PreparedStatement pst=null;
try {
conn=getConnection();//連接
pst=conn.prepareStatement(sql);//通道
if(params != null){//占位符的應(yīng)用。
for(int i=0;i<params.length;i++){
pst.setObject(i+1,params[i]);//往通道里放數(shù)據(jù),占位符下標從1開始。
}
}
return pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
//查詢結(jié)果集。比增刪改要復(fù)雜一些,慢慢看。
//這里的IMapper是將所有可能的用到的類都放進去,方便以后繼承使用。(現(xiàn)在我們寫的是Student信息,以后可能會有Teacher信息,Class信息等等)
//用接口是因為接口多繼承,方便維護升級
public List executeQuery(String sql,IMapper mapper,Object []params){
Connection conn=null;
PreparedStatement pst=null;
ResultSet rst=null;//查詢結(jié)果集
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();//把通道里的數(shù)據(jù)放入結(jié)果集
list=mapper.map(rst);//IMapper里有個map接口,里面存著結(jié)果集數(shù)據(jù)。把結(jié)果集的數(shù)據(jù)放入list集合
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public int count(String sql){//分頁查詢 count代表頁數(shù)。
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");//讀取文件數(shù)據(jù)
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);//找驅(qū)動
return DriverManager.getConnection(URL, USER, PASSWORD);//建連接
}
}
(2)com.student.vo包。這里面有一個vo類,我們是要把數(shù)據(jù)庫里的數(shù)據(jù)放到j(luò)ava里展示,用一個類對象把數(shù)據(jù)庫里的信息一一對應(yīng)起來就可以很容易的操作。數(shù)據(jù)庫里的一個列對應(yīng)類對象的一個屬性。
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包。這里面一個接口,一個實現(xiàn)類。
接口:
package com.student.mapper;
import java.sql.ResultSet;
import java.util.List;
public interface IMapper {
List map(ResultSet rst);//聲明一個方法存著結(jié)果集。
}
實現(xiàn)類:
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 {//實現(xiàn)接口方法
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"));//類對象每一個屬性對應(yīng)數(shù)據(jù)庫的每一列。
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();//實現(xiàn)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);
}
}
當把這一塊寫完之后,其實大部分就已經(jīng)完成了,JDBC連接數(shù)據(jù)庫基本上是固定的,多寫幾遍就明白了。
三、展示Student信息,實現(xiàn)增刪改查??磮D:

(1)com.student.show包,展示界面:這里面內(nèi)容比較多,但是都很容易理解

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號就是關(guān)閉
setResizable(false);//不可改變窗口大小
setLocationRelativeTo(null);//默認居中顯示
setLayout(null);//采用坐標布局
StuDAO dao = new StuDAO();//前面我們已經(jīng)把增刪改查分頁寫到StuDAO里面,現(xiàn)在就直接拿出來用
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 = { "學(xué)號", "姓名", "年齡", "性別" };//從這里開始是二維數(shù)組的遍歷使用
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聯(lián)系起來
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聯(lián)系起來
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里面已經(jīng)寫好了,在用的時候就方便多了。




①添加:
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("學(xué)號:");
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();//調(diào)用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("學(xué)號:");
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相同的學(xué)號。
stu=list.get(i);
if(stu.getStuid().equals(id)){//id是參數(shù),跟前面Show聯(lián)系起來。
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()已經(jīng)寫好如何修改,這里直接用
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("學(xué)生信息");
j0.setBounds(100,20,80,30);
add(j0);
JLabel j1=new JLabel("學(xué)號:");
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("學(xué)號:");
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){//設(shè)置用戶名為abcdefj
if(pass.equals(123456)){//設(shè)置密碼為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();
}
}
寫在最后:
剛開始學(xué)的時候感覺很繞,尤其是JDBC那,后來發(fā)現(xiàn),是因為前面java基礎(chǔ)掌握的不行,我又回去好好復(fù)習了java基礎(chǔ),才發(fā)現(xiàn)JDBC是死的,固定的寫法,背過就行了。所以再做這個學(xué)生管理系統(tǒng),就感覺不復(fù)雜了。先有一個大的思路,然后順著思路往下走,逐步實現(xiàn)每個功能。
更多學(xué)習資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于SpringBoot+Redis實現(xiàn)一個簡單的限流器
在Spring?Boot中使用Redis和過濾器實現(xiàn)請求限流,過濾器將在每個請求到達時檢查請求頻率,并根據(jù)設(shè)定的閾值進行限制,這樣可以保護您的應(yīng)用程序免受惡意請求或高并發(fā)請求的影響,本文我們通過Spring?Boot?+Redis?實現(xiàn)一個輕量級的消息隊列,需要的朋友可以參考下2023-08-08
IDEA創(chuàng)建Servlet并配置web.xml的實現(xiàn)
這篇文章主要介紹了IDEA創(chuàng)建Servlet并配置web.xml的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-10-10
JVM---jstack分析Java線程CPU占用,線程死鎖的解決
這篇文章主要介紹了JVM---jstack分析Java線程CPU占用,線程死鎖的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

