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





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

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

(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信息,實現增刪改查??磮D:

(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是死的,固定的寫法,背過就行了。所以再做這個學生管理系統,就感覺不復雜了。先有一個大的思路,然后順著思路往下走,逐步實現每個功能。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。