JavaSwing實(shí)現(xiàn)小型學(xué)生管理系統(tǒng)
本文實(shí)例為大家分享了JavaSwing實(shí)現(xiàn)小型學(xué)生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
在項目中建立四個包,分別是com.wu.JavaBean、com.wuJavaDao、com.wu.JavaService、com.wu.JavaView
數(shù)據(jù)庫表結(jié)構(gòu)
學(xué)生表只有四個屬性:學(xué)生姓名、學(xué)生性別、學(xué)生學(xué)號(主鍵)、學(xué)生班級
管理員表只有兩個屬性:管理員用戶名(主鍵)、管理員密碼
這里筆者為了簡單,學(xué)生表只寫了四個屬性,管理員表只寫了兩個屬性。
在JavaBean新建Student和Root類,如下:
Student.java:
package com.wu.JavaBean; /** ?*? * @date 2020年12月15日下午9:49:51 * @author 一夜星塵 ?*/ public class Student { ?? ?private String name; ?? ?private String gender; ?? ?private String id; ?? ?private String team; ?? ?public Student() {} ?? ?public Student(String name,String gender,String id,String team) { ?? ??? ?this.name = name; ?? ??? ?this.id = id; ?? ??? ?this.team = team; ?? ??? ?this.gender = gender; ?? ?} ?? ?public String getName() { ?? ??? ?return name; ?? ?} ?? ?public void setName(String name) { ?? ??? ?this.name = name; ?? ?} ?? ?public String getGender() { ?? ??? ?return gender; ?? ?} ?? ?public void setGender(String gender) { ?? ??? ?this.gender = gender; ?? ?} ?? ?public String getId() { ?? ??? ?return id; ?? ?} ?? ?public void setId(String id) { ?? ??? ?this.id = id; ?? ?} ?? ?public String getTeam() { ?? ??? ?return team; ?? ?} ?? ?public void setTeam(String team) { ?? ??? ?this.team = team; ?? ?} }
Root.java:
package com.wu.JavaBean; /** ?*? * @date 2020年12月15日下午9:50:30 * @author 一夜星塵 ?*/ public class Root { ?? ?private String username; // 賬號 ?? ?private String password; // 密碼 ?? ?private String ?superroot ; // 超級管理員身份 唯一一個 ?? ?public Root(String username) { ?? ??? ?this.username = username; ?? ?} ?? ?public Root(String username,String password,String superroot) {?? ??? ? ?? ??? ?this.username = username; ?? ??? ?this.password = password; ?? ??? ?this.superroot = superroot; ?? ?} ?? ?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 boolean isSuperRoot() { ?? ??? ?return superroot.equals("1"); // 1代表超級管理員 ?? ?} }
建立數(shù)據(jù)庫連接DAO層,即在JavaDao包下建立JDBC.java,該程序只是控制數(shù)據(jù)庫的連接:
package com.wu.JavaDao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * @date 2020年12月15日下午9:58:11 * @author 一夜星塵 */ public class JDBC { ?? ?private Connection sqllink = null; ?? ?/** ?? ? * 獲取數(shù)據(jù)庫連接對象 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public ?Connection ?getConnection() ?throws Exception{ ?? ??? ?String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";? ?? ??? ?String DATABASE_URL = "jdbc:mysql://127.0.0.1:3306/jdbc_db"+ ?? ??? ?"?charcterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";? ?? ??? ?String DATABASE_USER = "root"; ?? ??? ?String DATABASE_PASSWORD = "root"; ?? ??? ? ?? ??? ?try { ?? ??? ??? ?Class.forName(DATABASE_DRIVER); // 注冊驅(qū)動 ?? ??? ??? ?sqllink = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 連接數(shù)據(jù)庫 ?? ??? ??? ?return this.sqllink; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?System.out.println("連接數(shù)據(jù)庫異常"); // 錯誤信息顯示到控制臺 ?? ??? ??? ?return this.sqllink; ?? ??? ?} ?? ?} ?? ?/** ?? ? * 關(guān)閉數(shù)據(jù)庫連接對象 ?? ? * @throws Exception ?? ? */ ?? ?public void closeConnection() throws Exception{ ?? ??? ?try { ?? ??? ??? ?if(this.sqllink != null) { ?? ??? ??? ??? ?this.sqllink.close(); ?? ??? ??? ?} ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?System.out.println("關(guān)閉數(shù)據(jù)庫連接異常"); ?? ??? ?} ?? ?} }
com.wu.JavaBean和com.wu.JavaDao已經(jīng)全部完成了,接下來就是完成業(yè)務(wù)邏輯JavaService包下的實(shí)現(xiàn)
對于增添數(shù)據(jù)的業(yè)務(wù)方法Add.java:
package com.wu.JavaService; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.wu.JavaBean.Root; import com.wu.JavaBean.Student; import com.wu.JavaDao.JDBC; /** * @date 2020年12月15日下午9:59:09 * @author 一夜星塵 */ public class Add { ?? ?/** ?? ? * 添加信息 ?? ? * @param element 學(xué)生或者管理員 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static boolean add(Object ?element) throws Exception{ // 多態(tài) ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?if(element instanceof Student) { ?? ??? ??? ?String sql = "insert into student(name,gender,id,team) values(?,?,?,?)"; // mysql插入語句 ?? ??? ??? ?Student student ?= (Student) element; // 向下轉(zhuǎn)型 ?? ??? ??? ?try { ?? ??? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); // 操作對象 ?? ??? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ??? ?sqlaction.setString(2,student.getGender()); ?? ??? ??? ??? ?sqlaction.setString(3,student.getId()); ?? ??? ??? ??? ?sqlaction.setString(4,student.getTeam()); ?? ??? ??? ??? ? ?? ??? ??? ??? ?int count = sqlaction.executeUpdate(); // 執(zhí)行操作 ?? ??? ??? ??? ?return (count == 1) ? true : false; ?? ??? ??? ?}catch(SQLException e) { ?? ??? ??? ??? ?return false; ?? ??? ??? ?}finally{ ?? ??? ??? ??? ?jdbc.closeConnection(); // 關(guān)閉數(shù)據(jù)庫連接 ?? ??? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?}else if(element instanceof Root) { ?? ??? ??? ? ?? ??? ??? ? ?? ??? ??? ?String sql = "insert into root(username,password,superroot) values(?,?,0)"; // mysql插入語句 ?? ??? ??? ?Root root ?= (Root) element; // 向下轉(zhuǎn)型 ?? ??? ??? ?// 超級管理員權(quán)限 ?? ??? ??? ?if(!root.isSuperRoot()) { ?? ??? ??? ??? ?return false; ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?try { ?? ??? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); // 操作對象 ?? ??? ??? ??? ?sqlaction.setString(1,root.getUsername()); ?? ??? ??? ??? ?sqlaction.setString(2,root.getPassword()); ?? ??? ??? ??? ? ?? ??? ??? ??? ?int count = sqlaction.executeUpdate(); // 執(zhí)行操作 ?? ??? ??? ??? ?return (count == 1) ? true : false; ?? ??? ??? ?}catch(SQLException e) { ?? ??? ??? ??? ?return false; ?? ??? ??? ?}finally{ ?? ??? ??? ??? ?jdbc.closeConnection(); ?// 關(guān)閉數(shù)據(jù)庫連接 ?? ??? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?}else { ?? ??? ??? ?System.out.println("對象傳入錯誤"); ?? ??? ??? ?return false; ?? ??? ?} ?? ?} }
對于刪除Remove.java:
package com.wu.JavaService; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.wu.JavaBean.Root; import com.wu.JavaBean.Student; import com.wu.JavaDao.JDBC; /** * @date 2020年12月15日下午10:00:30 * @author 一夜星塵 */ public class Remove { ?? ?/** ?? ? * 移除學(xué)生信息 ?? ? * @param student 待移除的學(xué)生 ?? ? * @param pos 移除方式 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static boolean removeStudent(Student student ,String username,int pos) throws Exception{ // 部分修改或者全部修改 ?? ??? ?// 權(quán)限判斷 只有超級管理員才能實(shí)現(xiàn)全部學(xué)生刪除 ?? ??? ?if (pos == 0 && !Find.getAccess(username).equals("1")) { ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ? ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?String sql = ""; ?? ??? ?String[] info = new String[4]; ?? ??? ?/** ?? ??? ? * 0代表刪除所有學(xué)生 ?? ??? ? * 1代表刪除所有姓名為name的學(xué)生 ?? ??? ? * 2代表刪除所有性別為gender的學(xué)生 ?? ??? ? * 3代表刪除一個學(xué)號為id的學(xué)生 ?? ??? ? * 4代表刪除所有班級為team的學(xué)生 ?? ??? ? * 5代表刪除所有姓名為name性別為gender的學(xué)生 ?? ??? ? * 6代表刪除一個學(xué)號為id姓名為name的學(xué)生 ?? ??? ? * 7代表刪除所有姓名為name的班級為team的學(xué)生 ?? ??? ? * 8代表刪除性別為gender學(xué)號為id的一個學(xué)生 ?? ??? ? * 9代表刪除所有性別為gender班級為team的學(xué)生 ?? ??? ? * 10代表刪除一個學(xué)號為id班級為team的學(xué)生 ?? ??? ? * 11代表刪除一個姓名為name性別為gender學(xué)號為id的學(xué)生 ?? ??? ? * 12代表刪除所有姓名為name性別為gender班級為team的學(xué)生 ?? ??? ? * 13代表刪除刪除一個姓名為name學(xué)號為id班級為team的學(xué)生 ?? ??? ? * 14代表刪除一個性別為gender學(xué)號為id班級為team的學(xué)生 ?? ??? ? * 15代表刪除一個姓名為name性別為gender學(xué)號為id班級為team的學(xué)生 ?? ??? ? */ ?? ??? ?switch(pos) { ?? ??? ?case 0: ?? ??? ??? ?sql = "delete from student"; ?? ??? ??? ?try { ?? ??? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ??? ?sqlaction.executeUpdate(); ?? ??? ??? ??? ?return true; ?? ??? ??? ?}catch(SQLException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?return false; ?? ??? ??? ?}finally { ?? ??? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?case 1: ?? ??? ??? ?sql = "delete from student where name = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?sql = "delete from student where gender = ?"; ?? ??? ??? ?info[0] = student.getGender();?? ? ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?sql = "delete from student where id = ?"; ?? ??? ??? ?info[0] = student.getId(); ?? ??? ??? ?break; ?? ??? ?case 4:?? ? ?? ??? ??? ?sql = "delete from student where team = ?"; ?? ??? ??? ?info[0] = student.getTeam(); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?sql = "delete from student where name = ? and gender = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getGender(); ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?sql = "delete from student where name = ? and id = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getId(); ?? ??? ??? ?break; ?? ??? ?case 7: ?? ??? ??? ?sql = "delete from student where name = ? and team = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getTeam(); ?? ??? ??? ?break; ?? ??? ?case 8: ?? ??? ??? ?sql = "delete from student where gender = ? and id = ?"; ?? ??? ??? ?info[0] = student.getGender(); ?? ??? ??? ?info[1] = student.getId(); ?? ??? ??? ?break; ?? ??? ?case 9: ?? ??? ??? ?sql = "delete from student where gender = ? and team = ?"; ?? ??? ??? ?info[0] = student.getId(); ?? ??? ??? ?info[1] = student.getTeam(); ?? ??? ??? ?break; ?? ??? ?case 10: ?? ??? ??? ?sql = "delete from student where id = ? and team = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getGender(); ?? ??? ??? ?break; ?? ??? ?case 11: ?? ??? ??? ?sql = "delete from student where name = ? and gender = ? and id = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getGender(); ?? ??? ??? ?info[2] = student.getId(); ??? ??? ??? ?break; ?? ??? ?case 12: ?? ??? ??? ?sql = "delete from student where name = ? and gender = ? and team = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getGender(); ?? ??? ??? ?info[2] = student.getTeam(); ?? ??? ??? ?break; ?? ??? ?case 13: ?? ??? ??? ?sql = "delete from student where name = ? and id = ? and team = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getId(); ?? ??? ??? ?info[2] = student.getTeam(); ?? ??? ??? ?break; ?? ??? ?case 14: ?? ??? ??? ?sql = "delete from student where gender = ? and id = ? and team = ?"; ?? ??? ??? ?info[0] = student.getGender(); ?? ??? ??? ?info[1] = student.getId(); ?? ??? ??? ?info[2] = student.getTeam(); ?? ??? ??? ?break; ?? ??? ?case 15: ?? ??? ??? ?sql = "delete from student where name = ? and gender = ? and id = ? and team = ?"; ?? ??? ??? ?info[0] = student.getName(); ?? ??? ??? ?info[1] = student.getGender(); ?? ??? ??? ?info[2] = student.getId(); ?? ??? ??? ?info[3] = student.getTeam(); ?? ??? ?} ?? ? ?? ??? ?try { ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?switch(pos) { ?? ??? ??? ?case 1: ?? ??? ??? ?case 2: ?? ??? ??? ?case 3: ?? ??? ??? ?case 4: ?? ??? ??? ??? ?sqlaction.setString(1, info[0]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 5: ?? ??? ??? ?case 6: ?? ??? ??? ?case 7: ?? ??? ??? ?case 8: ?? ??? ??? ?case 9: ?? ??? ??? ?case 10: ?? ??? ??? ??? ?sqlaction.setString(1, info[0]); ?? ??? ??? ??? ?sqlaction.setString(2, info[1]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 11: ?? ??? ??? ?case 12: ?? ??? ??? ?case 13: ?? ??? ??? ?case 14: ?? ??? ??? ??? ?sqlaction.setString(1, info[0]); ?? ??? ??? ??? ?sqlaction.setString(2, info[1]); ?? ??? ??? ??? ?sqlaction.setString(3, info[2]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 15: ?? ??? ??? ??? ?sqlaction.setString(1, info[0]); ?? ??? ??? ??? ?sqlaction.setString(2, info[1]); ?? ??? ??? ??? ?sqlaction.setString(3, info[2]); ?? ??? ??? ??? ?sqlaction.setString(4, info[3]); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?sqlaction.executeUpdate(); ?? ??? ??? ?return true; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return false; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ??? ??? ??? ? ?? ?} ?? ?/** ?? ? * 刪除管理員信息 ?? ? * @param root 待刪除管理員 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static boolean removeRoot(Root root) throws Exception{ // 完全刪除 ?? ??? ?// 權(quán)限判斷 ?? ??? ?if(!root.isSuperRoot()) { ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ? ?? ??? ?String sql = "delete from root where username = ? "; ?? ??? ?try { ?? ??? ??? ?sqlaction ?= sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,root.getUsername()); ?? ??? ??? ? ?? ??? ??? ?int count = sqlaction.executeUpdate(); ?? ??? ??? ?return count == 1?true : false; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return false; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
對于查找Find.java:
package com.wu.JavaService; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.wu.JavaBean.Root; import com.wu.JavaBean.Student; import com.wu.JavaDao.JDBC; /** * @date 2020年12月15日下午10:01:05 * @author 一夜星塵 */ public class Find { ?? ?/** ?? ? * 查找學(xué)生信息 ?? ? * @param student 待查找的學(xué)生 ?? ? * @param pos 查找方式 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static ArrayList<Student> findStduent(Student student,int pos) throws Exception{ // 查詢所有學(xué)生或者部分學(xué)生 ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?ResultSet result = ?null; // 結(jié)果集 ?? ??? ?String sql = ""; ?? ??? ?ArrayList<Student> studentlist = new ArrayList<Student>(); // 返回的結(jié)果 ?? ??? ?/** ?? ??? ? * 0 代表查詢?nèi)? ?? ??? ? * 1 代表查詢所有姓名為name的學(xué)生 ?? ??? ? * 2 代表查詢所有性別為gender的學(xué)生 ?? ??? ? * 3 代表查詢一個學(xué)號為id的學(xué)生 ?? ??? ? * 4 代表查詢所有班級為team的學(xué)生 ?? ??? ? * 5 代表查詢...同刪除操作 ?? ??? ? */ ?? ??? ?switch(pos) { ?? ??? ?case 0: ?? ??? ??? ?sql = "select * from student"; ?? ??? ??? ?try { ?? ??? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ??? ?result = sqlaction.executeQuery(); // 執(zhí)行查詢操作 ?? ??? ??? ??? ?while(result.next()) { ?? ??? ??? ??? ??? ?String name = result.getString("name"); ?? ??? ??? ??? ??? ?String gender = result.getString("gender"); ?? ??? ??? ??? ??? ?String id = result.getString("id"); ?? ??? ??? ??? ??? ?String team = result.getString("team"); ?? ??? ??? ??? ??? ?studentlist.add(new Student(name,gender,id,team)); // 添加至返回結(jié)果中 ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?return studentlist; ?? ??? ??? ?}catch(SQLException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?return null; ?? ??? ??? ?}finally { ?? ??? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?case 1: ?? ??? ??? ?sql = "select * from student where name like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?sql = "select * from student where gender like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getGender()); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?sql = "select * from student where id like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getId()); ?? ??? ??? ?break; ?? ??? ?case 4:?? ? ?? ??? ??? ?sql = "select * from student where team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?sql = "select * from student where name like ? ?and gender like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getGender()); ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?sql = "select * from student where name like ? and id like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getId()); ?? ??? ??? ?break; ?? ??? ?case 7: ?? ??? ??? ?sql = "select * from student where name like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 8: ?? ??? ??? ?sql = "select * from student where gender like ? and id like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getGender()); ?? ??? ??? ?sqlaction.setString(2,student.getId()); ?? ??? ??? ?break; ?? ??? ?case 9: ?? ??? ??? ?sql = "select * from student where gender like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getGender()); ?? ??? ??? ?sqlaction.setString(2,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 10: ?? ??? ??? ?sql = "select * from student where id like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getId()); ?? ??? ??? ?sqlaction.setString(2,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 11: ?? ??? ??? ?sql = "select * from student where name like ? and gender like ? and id like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getGender()); ?? ??? ??? ?sqlaction.setString(3,student.getId()); ?? ??? ??? ?break; ?? ??? ?case 12: ?? ??? ??? ?sql = "select * from student where name like ? and gender like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getGender()); ?? ??? ??? ?sqlaction.setString(3,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 13: ?? ??? ??? ?sql = "select * from student where name like ? and id like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getId()); ?? ??? ??? ?sqlaction.setString(3,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 14: ?? ??? ??? ?sql = "select * from student where ?gender like ? and id like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getGender()); ?? ??? ??? ?sqlaction.setString(2,student.getId()); ?? ??? ??? ?sqlaction.setString(3,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 15: ?? ??? ??? ?sql = "select * from student where ?name like ? and gender like ? and id like ? and team like ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,student.getName()); ?? ??? ??? ?sqlaction.setString(2,student.getGender()); ?? ??? ??? ?sqlaction.setString(3,student.getId()); ?? ??? ??? ?sqlaction.setString(4,student.getTeam()); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ? ?? ??? ?try { ?? ??? ??? ? ?? ??? ??? ?result = sqlaction.executeQuery(); // 執(zhí)行查詢操作 ?? ??? ??? ?while(result.next()) { ?? ??? ??? ??? ?String name = result.getString("name"); ?? ??? ??? ??? ?String gender = result.getString("gender"); ?? ??? ??? ??? ?String id = result.getString("id"); ?? ??? ??? ??? ?String team = result.getString("team"); ?? ??? ??? ??? ?studentlist.add(new Student(name,gender,id,team)); // 添加至返回結(jié)果中 ?? ??? ??? ?} ?? ??? ??? ?return studentlist; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return null; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?/** ?? ? * 超級管理員權(quán)限 ?? ? * 查找所有管理員 ?? ? * @param root 驗證屬性 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static ArrayList<Root> findRoot(Root root) throws Exception{ // 完全查找權(quán)限 ?? ??? ? ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?ResultSet result = null; ?? ??? ?ArrayList<Root> rootlist = new ArrayList<Root>(); ??? ??? ?String sql = "select * from root"; ?? ??? ?try { ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ? ?? ??? ??? ?result = sqlaction.executeQuery(); ?? ??? ??? ?while(result.next()) { ?? ??? ??? ??? ?String username = result.getString("username"); ?? ??? ??? ??? ?String password = result.getString("password"); ?? ??? ??? ??? ?String superroot = result.getString("superroot"); ?? ??? ??? ??? ?rootlist.add(new Root(username,password,superroot)); ?? ??? ??? ?} ?? ??? ??? ?return rootlist; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return null; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ? ?? ?/** ?? ? * 獲取權(quán)限信息 ?? ? * @param username 用戶名 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static String getAccess(String username) throws Exception{ ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?ResultSet result = null; ?? ??? ? ?? ??? ?String sql = "select superroot from root where username = ?"; ?? ??? ?try { ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1, username); ?? ??? ??? ?result= sqlaction.executeQuery(); ?? ??? ??? ?if(result.next()) { ?? ??? ??? ??? ?return result.getString("superroot"); ?? ??? ??? ?}else { ?? ??? ??? ??? ?return "0"; ?? ??? ??? ?} ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return "0"; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ? ?? ?public static int getCount() throws Exception{ ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?ResultSet result = null; ?? ??? ? ?? ??? ?String sql = "select count(*) from student"; ?? ??? ?try { ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?result ?= sqlaction.executeQuery(); ?? ??? ??? ?if(result.next()) { ?? ??? ??? ??? ?return Integer.parseInt(result.getString(1)); ?? ??? ??? ?}else { ?? ??? ??? ??? ?return 0; ?? ??? ??? ?} ?? ??? ?}catch(Exception e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return 0; ?? ??? ?} ?? ?} ?? ? }
對于修改Update.java:
package com.wu.JavaService; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.wu.JavaBean.Root; import com.wu.JavaBean.Student; import com.wu.JavaDao.JDBC; /** * @date 2020年12月15日下午10:01:22 * @author 一夜星塵 */ public class Update { ?? ?/** ?? ? * 更新學(xué)生信息 ?? ? * @param oldstudent 待修改的學(xué)生 ?? ? * @param newstudent 修改后的學(xué)生 ?? ? * @param pos 修改方式 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static boolean updateStudent(Student oldstudent,Student newstudent,int pos) throws Exception{ ?// 部分或者完全更新模式 ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?String sql = ""; ?? ??? ? ?? ??? ?int count = 0; ?? ??? ?switch(pos) { ?? ??? ?case 0: ?? ??? ??? ?sql = "update student set name = ?,gender ?= ?,id = ?,team = ? ?where id = ?"; // id一定要存在 ?? ??? ??? ?try { ?? ??? ??? ??? ?sqlaction ?= sqllink.prepareStatement(sql); ?? ??? ??? ??? ?sqlaction.setString(1,newstudent.getName()); ?? ??? ??? ??? ?sqlaction.setString(2,newstudent.getGender()); ?? ??? ??? ??? ?sqlaction.setString(3,newstudent.getId()); ?? ??? ??? ??? ?sqlaction.setString(4,newstudent.getTeam()); ?? ??? ??? ??? ?sqlaction.setString(5,oldstudent.getId()); ?? ??? ??? ??? ? ?? ??? ??? ??? ?count = sqlaction.executeUpdate(); //執(zhí)行操作 ?? ??? ??? ??? ?return count==1?true:false; ?? ??? ??? ?}catch(SQLException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ?return false; ?? ??? ??? ?}finally { ?? ??? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?case 1: ?? ??? ??? ?sql = "update student set name = ? where name = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getName()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getName()); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?sql = "update student set name = ? where gender = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getName()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getGender()); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?sql = "update student set name = ? where id = ?"; ? ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getName()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getId()); ?? ??? ??? ?break; ?? ??? ?case 4: ?? ??? ??? ?sql = "update student set name = ? where team = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getName()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?sql = "update student set gender = ? where name = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getGender()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getName()); ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?sql = "update student set gender = ? where gender = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getGender()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getGender()); ?? ??? ??? ?break; ?? ??? ?case 7: ?? ??? ??? ?sql = "update student set gender = ? where id = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getGender()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getId()); ?? ??? ??? ?break; ?? ??? ?case 8: ?? ??? ??? ?sql = "update student set gender = ? where team = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getGender()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getTeam()); ?? ??? ??? ?break; ?? ??? ?case 9: ?? ??? ??? ?sql = "update student set id = ? where id = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getId()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getId()); ?? ??? ??? ?break; ?? ??? ?case 10: ?? ??? ??? ?sql = "update student set team = ? where name = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getTeam()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getName()); ?? ??? ??? ?break; ?? ??? ?case 11: ?? ??? ??? ?sql = "update student set team = ? where gender = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getTeam()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getGender()); ?? ??? ??? ?break; ?? ??? ?case 12: ?? ??? ??? ?sql = "update student set team = ? where id = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getTeam()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getId()); ?? ??? ??? ?break; ?? ??? ?case 13: ?? ??? ??? ?sql = "update student set team = ? where team = ?"; ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,newstudent.getTeam()); ?? ??? ??? ?sqlaction.setString(2, oldstudent.getTeam()); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ? ?? ??? ?try { ?? ??? ??? ?count = sqlaction.executeUpdate(); ?? ??? ??? ?return count >= 1 ? true:false; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace();? ?? ??? ??? ?return false; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?/** ?? ? * 超級管理員權(quán)限 ?? ? * @param root 待更新的管理員 ?? ? * @param info 更新信息 ?? ? * @param pos 更新方式 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static boolean updateRoot(Root root ,String info,int pos) throws Exception{ ?// 完全更新模式 ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?String sql = ""; ?? ??? ? ?? ??? ?switch(pos){ ?? ??? ??? ?case 1: ?? ??? ??? ??? ?sql = "update root set username = ? where username =?"; ?? ??? ??? ??? ?break; ?? ??? ??? ?case 2: ?? ??? ??? ??? ?sql = "update root set password = ? where username =?"; ?? ??? ??? ??? ?break; ?? ??? ?} ?? ??? ?try { ?? ??? ??? ?sqlaction ?= sqllink.prepareStatement(sql); ?? ??? ??? ? ?? ??? ??? ?sqlaction.setString(1,info); ?? ??? ??? ?sqlaction.setString(2, root.getUsername()); ?? ??? ??? ? ?? ??? ??? ?int count = sqlaction.executeUpdate(); ?? ??? ??? ?return count == 1?true:false; ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return false; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
加上管理員登錄認(rèn)證Exist.java:
package com.wu.JavaService; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.wu.JavaBean.Root; import com.wu.JavaDao.JDBC; /** * @date 2020年12月15日下午10:41:32 * @author 一夜星塵 */ public class Exist { ?? ?/** ?? ? * 管理員登錄認(rèn)證 ?? ? * @param root 管理員 ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?public static boolean rootIsExist(Root root) throws Exception { ?? ??? ?// 獲取數(shù)據(jù)庫對象 ?? ??? ?JDBC jdbc = new JDBC(); ?? ??? ?Connection sqllink = jdbc.getConnection(); ?? ??? ? ?? ??? ?PreparedStatement sqlaction = null; // 創(chuàng)建一個數(shù)據(jù)庫操作對象 ?? ??? ?ResultSet result = null; ?? ??? ?String sql = "select count(*) from root where username = ? and password = ?"; ?? ??? ? ?? ??? ?try { ?? ??? ??? ?sqlaction = sqllink.prepareStatement(sql); ?? ??? ??? ?sqlaction.setString(1,root.getUsername()); ?? ??? ??? ?sqlaction.setString(2,root.getPassword()); ?? ??? ??? ? ?? ??? ??? ?result = sqlaction.executeQuery(); ?? ??? ??? ?if(result.next()) { ?? ??? ??? ??? ?int count = Integer.parseInt(result.getString(1)); ?? ??? ??? ??? ?return count == 1 ? true:false; ?? ??? ??? ?}else { ?? ??? ??? ??? ?return false; ?? ??? ??? ?} ?? ??? ?}catch(SQLException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return false; ?? ??? ?}finally { ?? ??? ??? ?jdbc.closeConnection(); ?// 關(guān)閉數(shù)據(jù)庫連接 ?? ??? ??? ?if(sqlaction != null) { ?? ??? ??? ??? ?sqlaction.close(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
處理模糊查詢的DealString.java:
package com.wu.JavaService; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @date 2020年12月15日下午1:48:05 * @author 一夜星塵 */ public class DealString { ?? ?public static String[] ?deal(String search) { ?? ??? ? ?? ??? ?String[] searchs = search.split("&"); ?? ??? ?String regex = "([\\u4e00-\\u9fa5]+)=([ % _ a-z 0-9 \\u4e00-\\u9fa5]+)"; // 匹配中文或者數(shù)字模式 ?? ??? ? ?? ??? ?String[] result = new String[5]; ?? ??? ?result[0] = "0"; // 默認(rèn)為全部 ?? ??? ?boolean nameflag = false; ?? ??? ?boolean genderflag = false; ?? ??? ?boolean idflag = false; ?? ??? ?boolean teamflag = false; ?? ??? ?HashMap<Integer, String> hashmap = new HashMap<Integer, String>(); ?? ??? ?for(String str : searchs) { ?? ??? ??? ?Matcher mattcher = Pattern.compile(regex).matcher(str); ?? ??? ??? ?if(mattcher.find()) { ?? ??? ??? ??? ?if(mattcher.group(1).equals("姓名")) { ?? ??? ??? ??? ??? ?nameflag = true; ?? ??? ??? ??? ??? ?hashmap.put(1,mattcher.group(2)); ?? ??? ??? ??? ?}else if(mattcher.group(1).equals("性別")){ ?? ??? ??? ??? ??? ?genderflag = true; ?? ??? ??? ??? ??? ?hashmap.put(2,mattcher.group(2)); ?? ??? ??? ??? ?}else if(mattcher.group(1).equals("學(xué)號")) { ?? ??? ??? ??? ??? ?idflag = true; ?? ??? ??? ??? ??? ?hashmap.put(3,mattcher.group(2)); ?? ??? ??? ??? ?}else if(mattcher.group(1).equals("班級")) { ?? ??? ??? ??? ??? ?teamflag = true; ?? ??? ??? ??? ??? ?hashmap.put(4,mattcher.group(2)); ?? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ?} ?? ??? ?// 對應(yīng)位置放置相關(guān)信息 ?? ??? ?Iterator<?> iter = hashmap.entrySet().iterator();? ?? ??? ?while (iter.hasNext()) {? ?? ??? ? ? ?Map.Entry entry = (Map.Entry) iter.next();? ?? ??? ? ? ?int ?key = (int) entry.getKey();? ?? ??? ? ? ?String val =(String) entry.getValue();? ?? ??? ? ? ?result[key] = val; ?? ??? ?}? ?? ??? ?if(nameflag && !genderflag && !idflag && !teamflag) { ?? ??? ??? ?result[0] = "1"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && genderflag && !idflag && !teamflag) { ?? ??? ??? ?result[0] = "2"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && !genderflag && idflag && !teamflag) { ?? ??? ??? ?result[0] = "3"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && !genderflag && !idflag && teamflag) { ?? ??? ??? ?result[0] = "4"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && genderflag && !idflag && !teamflag) { ?? ??? ??? ?result[0] = "5"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && !genderflag && idflag && !teamflag) { ?? ??? ??? ?result[0] = "6"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && !genderflag && !idflag && teamflag) { ?? ??? ??? ?result[0] = "7"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && genderflag && idflag && !teamflag) { ?? ??? ??? ?result[0] = "8"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && genderflag && !idflag && teamflag) { ?? ??? ??? ?result[0] = "9"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && !genderflag && idflag && teamflag) { ?? ??? ??? ?result[0] = "10"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && genderflag && idflag && !teamflag) { ?? ??? ??? ?result[0] = "11"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && genderflag && !idflag && teamflag) { ?? ??? ??? ?result[0] = "12"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && !genderflag && idflag && teamflag) { ?? ??? ??? ?result[0] = "13"; ?? ??? ??? ?return result; ?? ??? ?}else if(!nameflag && genderflag && idflag && teamflag) { ?? ??? ??? ?result[0] = "14"; ?? ??? ??? ?return result; ?? ??? ?}else if(nameflag && genderflag && idflag && teamflag) { ?? ??? ??? ?result[0] = "15"; ?? ??? ??? ?return result; ?? ??? ?} ?? ??? ?return result; ?? ?} }
接下來就是可視化界面,在JavaView包下
Home.java:
package com.wu.JavaView; /** * @date 2020年12月16日下午5:09:16 * @author 一夜星塵 */ public class Hemo { ?? ?public static void main(String[] args) { ?? ??? ?try { ?? ??? ??? ?new Login(); ?? ??? ?}catch(Exception e) { ?? ??? ??? ?System.out.println("程序出錯!"); ?? ??? ?} ?? ?} }
登錄界面Login.java:
package com.wu.JavaView; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRootPane; import javax.swing.JTextField; import javax.swing.border.Border; import com.wu.JavaBean.Root; import com.wu.JavaService.Exist; /** * @date 2020年12月16日下午10:02:08 * @author 一夜星塵 */ public class Login extends JFrame{ ?? ? ?? ?private static final long serialVersionUID = 1L; ?? ?public Login() throws Exception{ ?? ??? ?this.setSize(450,350); // 設(shè)置寬高度 ?? ??? ?this.setTitle("登錄界面"); // 設(shè)置標(biāo)題 ?? ??? ?this.setResizable(false); // 固定窗口大小 ?? ??? ?this.setUndecorated(true); // 去掉窗口的裝飾? ?? ??? ?this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); //采用指定的窗口裝飾風(fēng)格 ?? ??? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 結(jié)束程序 ?? ??? ?this.setLocationRelativeTo(null); // 使之位于主窗口的中心 ?? ??? ? ?? ??? ?setBackGroundPanel(); ?? ??? ?this.setVisible(true); // 顯示 ?? ??? ? ?? ?} ?? ?public void setBackGroundPanel() throws Exception{ ?? ??? ? JPanel panel = new JPanel(); ?? ??? ?? ?? ??? ?? ?? ??? ? JButton jb=new JButton("測試按鈕"); ?? ??? ? jb.setBounds(100,100,100,100); ?? ??? ?? ?? ??? ? this.add(panel); ?? ??? ? ?? ??? ?panel.setLayout(null); // 空布局 ?? ??? ? ?? ??? ?Font font = new Font("微軟雅黑",Font.BOLD,11); ?? ??? ?Border border1 = BorderFactory.createLoweredBevelBorder(); ?? ??? ?Border border2 = BorderFactory.createLineBorder(Color.BLUE); ?? ??? ? ?? ??? ?JLabel usernamelabel = new JLabel("賬號: "); ?? ??? ?usernamelabel.setFont(font); ?? ??? ?usernamelabel.setForeground(Color.BLACK); ?? ??? ?usernamelabel.setBounds(130,100,30,15); ?? ??? ?JLabel passwordlabel = new JLabel("密碼: "); ?? ??? ?passwordlabel.setFont(font); ?? ??? ?passwordlabel.setForeground(Color.BLACK); ?? ??? ?passwordlabel.setBounds(130,150,30,15); ?? ??? ? ?? ??? ?JTextField ?usernametext = new JTextField("I am superroot"); ?? ??? ?usernametext.setBounds(160,95,150,20); ?? ??? ?usernametext.setBorder(border1); //?? ??? ?usernametext.setOpaque(false); // 透明框 ?? ??? ?JPasswordField passwordtext = new JPasswordField("password"); ?? ??? ?passwordtext.setBounds(160,145,150,20); ?? ??? ?passwordtext.setBorder(border1); ?? ??? ? ?? ??? ?JButton submit = new JButton("登錄"); ?? ??? ?JButton close = new JButton("退出"); ?? ??? ?? ? ? ?? ?? ? ? ?submit.setBorder(border2); // 登錄鍵邊框風(fēng)格 ?? ??? ?submit.setBounds(130,210,90,25); ?? ??? ?submit.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ?String username = usernametext.getText(); ?? ??? ??? ??? ?String password = new String(passwordtext.getPassword()); ?? ??? ??? ??? ?if(username.equals("")) { ?? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "用戶名不能為空!", "錯誤",JOptionPane.WARNING_MESSAGE); ?? ??? ??? ??? ?}else if(password.equals("")){ ?? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "密碼不能為空!", "錯誤",JOptionPane.WARNING_MESSAGE); ?? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ?// 登錄認(rèn)證 ?? ??? ??? ??? ??? ?Root root = new Root(username,password,"0"); // 新建一個虛擬管理員對象 ?? ??? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ??? ?if(Exist.rootIsExist(root)) {? ?? ??? ??? ??? ??? ??? ??? ?new Menu(username); ?? ??? ??? ??? ??? ??? ??? ?dispose(); ?// 關(guān)閉當(dāng)前的窗口 ?? ??? ??? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!", "錯誤",JOptionPane.WARNING_MESSAGE); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?}catch(Exception e) { ?? ??? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ??? ?}finally { ?? ??? ??? ??? ??? ??? ?usernametext.setText(""); ?? ??? ??? ??? ??? ??? ?passwordtext.setText(""); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?}); ?? ??? ? ?? ? ? ?close.setBorder(border2); // 關(guān)閉鍵邊框風(fēng)格 ?? ??? ?close.setBounds(250,210,90,25); ?? ??? ?close.addActionListener(new ActionListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ? ?? ??? ??? ??? ?int quit = JOptionPane.showConfirmDialog(null,"是否退出?","提示",JOptionPane.YES_NO_OPTION); ?? ??? ??? ??? ?if(quit == JOptionPane.YES_OPTION) { ?? ??? ??? ??? ??? ?System.exit(0); ?? ??? ??? ??? ?}?? ??? ? ?? ??? ??? ?} ?? ??? ?}); ?? ??? ? ?? ??? ?panel.add(submit); ?? ??? ?panel.add(close); ?? ??? ?panel.add(usernametext); ?? ??? ?panel.add(passwordtext); ?? ??? ?panel.add(usernamelabel); ?? ??? ?panel.add(passwordlabel); ?? ??? ? ?? ?} ?? ? }
效果如下,布局簡單:
主頁面Menu.java:
package com.wu.JavaView; import java.awt.Color; import java.awt.Font; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JRootPane; /** * @date 2020年12月16日上午10:53:39 * @author 一夜星塵 */ public class Menu{ ?? ? ?? ?private static JFrame Frame = new JFrame(); ?? ?private String username = null; ?? ?private static ?JPanel Panel ?= null; ?? ? ?? ?public Menu(String username) { ?? ??? ?this.username = username; ?? ??? ?Frame.setSize(800,600); // 設(shè)置寬高度 ?? ??? ?Frame.setTitle("菜單界面"); // 設(shè)置標(biāo)題 ?? ??? ?Frame.setResizable(false); // 固定窗口大小 ?? ??? ?Frame.setUndecorated(true); // 去掉窗口的裝飾? ?? ??? ?Frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); //采用指定的窗口裝飾風(fēng)格 ?? ??? ?Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); ?? ??? ?Frame.setLocationRelativeTo(null); // 主窗口的中心 ?? ??? ?? ? ? ?? ?? ??? ?Panel ?= this.getPanel(); ?? ??? ?this.setMenu(); // 設(shè)置菜單項 ?? ??? ?Frame.add(Panel); ?? ??? ?Frame.setVisible(true); // 可見 ?? ?} ?? ?public JPanel ?getPanel() { ?? ??? ?JPanel panel = new JPanel(); ?? ??? ?panel.setLayout(null);// 絕對布局 ?? ??? ?panel.setBackground(Color.LIGHT_GRAY); ?? ??? ? ?? ??? ?Font font = new Font("微軟雅黑",Font.BOLD,20); ?? ??? ?JLabel title = new JLabel("管理員:"+username); ?? ??? ?title.setBounds(300,500,250,40); ?? ??? ?title.setFont(font); ?? ??? ? ?? ??? ?panel.add(title); // 增加底部標(biāo)簽 ?? ??? ? ?? ??? ?return panel; ?? ?} ?? ?public void setMenu() { ?? ??? ?// 菜單條 ?? ??? ?JMenuBar menubar = new JMenuBar(); ?? ??? ?menubar.setBounds(0,0,800,40); ?? ??? ?Panel.add(menubar); ?? ??? ? ?? ??? ?//菜單項 ?? ??? ?JMenu addmenu = new JMenu("添加"); ?? ??? ?JMenuItem addmenuItem1 = new JMenuItem("添加學(xué)生"); ?? ??? ?JMenuItem addmenuItem2 = new JMenuItem("添加管理員"); ?? ??? ?addmenu.add(addmenuItem1); ?? ??? ?addmenu.add(addmenuItem2); ?? ??? ? ?? ??? ?JMenu removemenu = new JMenu("刪除"); ?? ??? ?JMenuItem removemenuItem1 = new JMenuItem("刪除學(xué)生"); ?? ??? ?JMenuItem removemenuItem2 = new JMenuItem("刪除管理員"); ?? ??? ?removemenu.add(removemenuItem1); ?? ??? ?removemenu.add(removemenuItem2); ?? ??? ? ?? ??? ?JMenu findmenu = new JMenu("查找"); ?? ??? ?JMenuItem findmenuItem1 = new JMenuItem("查找學(xué)生"); ?? ??? ?JMenuItem findmenuItem2 = new JMenuItem("查找管理員"); ?? ??? ?findmenu.add(findmenuItem1); ?? ??? ?findmenu.add(findmenuItem2); ?? ??? ? ?? ??? ?JMenu updatemenu = new JMenu("修改"); ?? ??? ?JMenuItem updatemenuItem1 = new JMenuItem("修改學(xué)生"); ?? ??? ?JMenuItem updatemenuItem2 = new JMenuItem("修改管理員"); ?? ??? ?updatemenu.add(updatemenuItem1); ?? ??? ?updatemenu.add(updatemenuItem2); ?? ??? ? ?? ??? ?JMenu accessmenu = new JMenu("賬號設(shè)置"); ?? ??? ?JMenuItem accessmenuItem1 = new JMenuItem("修改用戶名"); ?? ??? ?JMenuItem accessmenuItem2 = new JMenuItem("修改密碼");?? ? ?? ??? ?JMenuItem accessmenuItem3 = new JMenuItem("退出賬號"); ?? ??? ?JMenuItem accessmenuItem4 = new JMenuItem("注銷賬號"); ?? ??? ?accessmenu.add(accessmenuItem1); ?? ??? ?accessmenu.add(accessmenuItem2); ?? ??? ?accessmenu.add(accessmenuItem3); ?? ??? ?accessmenu.add(accessmenuItem4); ?? ??? ? ?? ??? ?// 添加功能 監(jiān)聽器實(shí)現(xiàn) ?? ??? ?{ ?? ??? ??? ?addmenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AddPanel.getPanel(Menu.this.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ??? ?addmenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AddPanel.getPanel(Menu.this.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?});?? ??? ? ?? ??? ?} ?? ??? ? ?? ??? ?// 移除功能 監(jiān)聽器實(shí)現(xiàn) ?? ??? ?{ ?? ??? ??? ?removemenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= RemovePanel.getPanel(Menu.this.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ??? ?removemenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= RemovePanel.getPanel(Menu.this.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ?} ?? ??? ?// 查找功能 監(jiān)聽器實(shí)現(xiàn) ?? ??? ?{ ?? ??? ??? ?findmenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= FindPanel.getPanel(Menu.this.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?findmenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= FindPanel.getPanel(Menu.this.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?});?? ? ?? ??? ?} ?? ??? ? ?? ??? ?// 修改功能 監(jiān)聽器實(shí)現(xiàn) ?? ??? ?{ ?? ??? ??? ?updatemenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdatePanel.getPanel(Menu.this.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ??? ?updatemenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdatePanel.getPanel(Menu.this.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ?} ?? ??? ? ?? ??? ?// 賬號功能 監(jiān)聽器實(shí)現(xiàn) ?? ??? ?{ ?? ??? ??? ?accessmenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdateUserPanel.getPanel(Menu.this.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?accessmenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdateUserPanel.getPanel(Menu.this.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?accessmenuItem3.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AccountPanel.getPanel(Menu.this.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ??? ?accessmenuItem4.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?Menu.Frame.remove(Menu.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AccountPanel.getPanel(Menu.this.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ?} ?? ??? ?menubar.add(addmenu); ?? ??? ?menubar.add(removemenu); ?? ??? ?menubar.add(findmenu); ?? ??? ?menubar.add(updatemenu); ?? ??? ?menubar.add(accessmenu); ?? ?} ?? ?public static JFrame getFrame() { ?? ??? ?return Frame; ?? ?} }
筆者這里由于增刪改查界面設(shè)計繁瑣,亦限于篇幅,只展示‘查’的這一部分FindPanel.java:
package com.wu.JavaView; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import com.wu.JavaBean.Root; import com.wu.JavaBean.Student; import com.wu.JavaService.DealString; import com.wu.JavaService.Find; /** * @date 2020年12月16日上午10:09:35 * @author 一夜星塵 */ public class FindPanel { ?? ?private static JPanel Panel = null; ?? ?private static JFrame Frame = null; ?? ?private static String username = null; ?? ?private static Icon buttonicon = new ImageIcon("src\\images\\searchbutton.png"); ?? ?public static JPanel getPanel(String username,int flag) { ?? ??? ?Frame = Menu.getFrame(); // 同一Frame ?? ??? ?FindPanel.username = username; ?? ??? ? ?? ??? ?Panel = new JPanel();?? ??? ??? ??? ?//生成新的布局 ?? ??? ?Panel.setLayout(null);?? ??? ??? ??? ?// 絕對布局 ?? ??? ?Panel.setBounds(0,0,790,567); //設(shè)置布局大小 ?? ??? ?Panel.setBackground(Color.LIGHT_GRAY); ?? ??? ? ?? ??? ?setMenu(); // 設(shè)置菜單項 ?? ??? ?find(flag); // 增添學(xué)生或管理員 flag 1:學(xué)生 2 :管理員 ?? ??? ? ?? ??? ?return Panel; ?? ?} ?? ? ?? ?public static void setMenu() { ?? ??? ?// 菜單條 ?? ??? ?JMenuBar menubar = new JMenuBar(); ?? ??? ?menubar.setBounds(0,0,800,40); ?? ??? ?Panel.add(menubar); ?? ??? ? ?? ??? ?//菜單項 ?? ??? ?JMenu addmenu = new JMenu("添加"); ?? ??? ?JMenuItem addmenuItem1 = new JMenuItem("添加學(xué)生"); ?? ??? ?JMenuItem addmenuItem2 = new JMenuItem("添加管理員"); ?? ??? ?addmenu.add(addmenuItem1); ?? ??? ?addmenu.add(addmenuItem2); ?? ??? ? ?? ??? ?JMenu removemenu = new JMenu("刪除"); ?? ??? ?JMenuItem removemenuItem1 = new JMenuItem("刪除學(xué)生"); ?? ??? ?JMenuItem removemenuItem2 = new JMenuItem("刪除管理員"); ?? ??? ?removemenu.add(removemenuItem1); ?? ??? ?removemenu.add(removemenuItem2); ?? ??? ? ?? ??? ?JMenu findmenu = new JMenu("查找"); ?? ??? ?JMenuItem findmenuItem1 = new JMenuItem("查找學(xué)生"); ?? ??? ?JMenuItem findmenuItem2 = new JMenuItem("查找管理員"); ?? ??? ?findmenu.add(findmenuItem1); ?? ??? ?findmenu.add(findmenuItem2); ?? ??? ? ?? ??? ?JMenu updatemenu = new JMenu("修改"); ?? ??? ?JMenuItem updatemenuItem1 = new JMenuItem("修改學(xué)生"); ?? ??? ?JMenuItem updatemenuItem2 = new JMenuItem("修改管理員"); ?? ??? ?updatemenu.add(updatemenuItem1); ?? ??? ?updatemenu.add(updatemenuItem2); ?? ??? ? ?? ??? ?JMenu accessmenu = new JMenu("賬號設(shè)置"); ?? ??? ?JMenuItem accessmenuItem1 = new JMenuItem("修改用戶名"); ?? ??? ?JMenuItem accessmenuItem2 = new JMenuItem("修改密碼");?? ??? ? ?? ??? ?JMenuItem accessmenuItem3 = new JMenuItem("退出賬號"); ?? ??? ?JMenuItem accessmenuItem4 = new JMenuItem("注銷賬號"); ?? ??? ?accessmenu.add(accessmenuItem1); ?? ??? ?accessmenu.add(accessmenuItem2); ?? ??? ?accessmenu.add(accessmenuItem3); ?? ??? ?accessmenu.add(accessmenuItem4); ?? ??? ? ?? ??? ? ?? ??? ?// 添加功能 響應(yīng) ?? ??? ?{ ?? ??? ??? ?addmenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AddPanel.getPanel(FindPanel.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?addmenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AddPanel.getPanel(FindPanel.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?});?? ??? ? ?? ??? ?} ?? ??? ? ?? ??? ?//移除功能 響應(yīng) ?? ??? ?{ ?? ??? ??? ?removemenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= RemovePanel.getPanel(FindPanel.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ??? ?removemenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= RemovePanel.getPanel(FindPanel.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ?} ?? ??? ?//查看功能 響應(yīng) ?? ??? ?{ ?? ??? ??? ?findmenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= FindPanel.getPanel(FindPanel.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?findmenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= FindPanel.getPanel(FindPanel.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?});?? ? ?? ??? ?} ?? ??? ?//修改功能 響應(yīng) ?? ??? ?{ ?? ??? ??? ?updatemenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdatePanel.getPanel(FindPanel.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ? ?? ??? ??? ?updatemenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdatePanel.getPanel(FindPanel.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ?} ?? ??? ?//賬號功能 響應(yīng) ?? ??? ?{ ?? ??? ??? ?accessmenuItem1.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdateUserPanel.getPanel(FindPanel.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?accessmenuItem2.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= UpdateUserPanel.getPanel(FindPanel.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?accessmenuItem3.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AccountPanel.getPanel(FindPanel.username,1); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ??? ?accessmenuItem4.addActionListener(new ActionListener() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?@Override ?? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ?FindPanel.Frame.remove(FindPanel.Panel); // 移除當(dāng)前的布局 ?? ??? ??? ??? ??? ?Frame.repaint(); ?? ??? ??? ??? ??? ?Panel ?= AccountPanel.getPanel(FindPanel.username,2); // 切換布局 ?? ??? ??? ??? ??? ?Frame.add(Panel); ?? ??? ??? ??? ??? ?Frame.setVisible(true); ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ?} ?? ??? ?menubar.add(addmenu); ?? ??? ?menubar.add(removemenu); ?? ??? ?menubar.add(findmenu); ?? ??? ?menubar.add(updatemenu); ?? ??? ?menubar.add(accessmenu); ?? ?} ?? ?public static void find(int flag) { ?? ??? ?if(flag == 1) { ?? ??? ??? ? ?? ??? ??? ? ?? ??? ??? ?DefaultTableModel model = new DefaultTableModel(); ?? ??? ??? ?JTable table = new JTable(model); ?? ??? ??? ?JScrollPane scrollpanel = new JScrollPane(table); ?? ??? ??? ?scrollpanel.setBounds(0,80,800 ,560); ?? ??? ??? ?Panel.add(scrollpanel); ?? ??? ??? ? ?? ??? ??? ?JLabel title = new JLabel("學(xué)生信息查詢"); ?? ??? ??? ?title.setBounds(200,45,90,30); ?? ??? ??? ?Panel.add(title); ?? ??? ??? ? ?? ??? ??? ?String[] Attribute = {"姓名","性別","學(xué)號","班級"}; ?? ??? ??? ?// 構(gòu)建搜索框 ?? ??? ??? ?JTextField searchtext = new JTextField(); ?? ??? ??? ?searchtext.setBounds(300,45,160,30); ?? ??? ??? ?Panel.add(searchtext); ?? ??? ??? ?JButton search = new JButton(buttonicon); ?? ??? ??? ?search.setBounds(460,45,30,30); ?? ??? ??? ?Panel.add(search); ?? ??? ??? ?// 搜索功能 響應(yīng) ?? ??? ??? ?{ ?? ??? ??? ??? ?search.addActionListener(new ActionListener() { ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?@Override ?? ??? ??? ??? ??? ?public void actionPerformed(ActionEvent arg0) { ?? ??? ??? ??? ??? ??? ?String search = searchtext.getText(); ?? ??? ??? ??? ??? ??? ?String[] deal = DealString.deal(search); // 第一個位置為對應(yīng)的查找方式 第二至最后一個位置為存放的相應(yīng)的信息 ?? ??? ??? ??? ??? ??? ?int pos = Integer.parseInt(deal[0]); ?? ??? ??? ??? ??? ??? ?Object[][] dataVector = getStudentDateVector(new Student(deal[1],deal[2],deal[3],deal[4]),pos); //?? ??? ??? ??? ??? ??? ?System.out.println(deal[1]+deal[2]+deal[3]+deal[4]+pos); ?? ??? ??? ??? ??? ??? ?model.setDataVector(dataVector, Attribute); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?}); ?? ??? ??? ?} ?? ??? ??? ?Object[][] ?dataVector = getStudentDateVector(new Student(),0); // 默認(rèn)為查看所有學(xué)生 ?? ??? ??? ?model.setDataVector(dataVector, Attribute); ?? ??? ?}else if(flag == 2) { ?? ??? ??? ? ?? ??? ??? ?try{ ?? ??? ??? ??? ?if(Find.getAccess(FindPanel.username).equals("0")) { ?? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "權(quán)限不夠,無法訪問!", "錯誤",JOptionPane.WARNING_MESSAGE); ?? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ?DefaultTableModel model = new DefaultTableModel(); ?? ??? ??? ??? ??? ?JTable table = new JTable(model); ?? ??? ??? ??? ??? ?JScrollPane scrollpanel = new JScrollPane(table); ?? ??? ??? ??? ??? ?scrollpanel.setBounds(0,80,800 ,560); ?? ??? ??? ??? ??? ?Panel.add(scrollpanel); ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?JLabel title = new JLabel("管理員信息查詢"); ?? ??? ??? ??? ??? ?title.setBounds(360,45,90,30); ?? ??? ??? ??? ??? ?Panel.add(title); ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?String[] Attribute = {"用戶名","密碼"}; ?? ??? ??? ??? ??? ?Object[][] ?dataVector = getRootDateVector(new Root(FindPanel.username,"","")); // 查看所有管理員 ?? ??? ??? ??? ??? ?model.setDataVector(dataVector, Attribute); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?}catch(Exception e) { ?? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "權(quán)限不夠,無法訪問!", "錯誤",JOptionPane.WARNING_MESSAGE); ?? ??? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ?} ?? ? ?? ?public static String[][] getStudentDateVector(Student student,int pos){ ?? ??? ? ?? ??? ?try { ?? ??? ??? ?String[][] data = new String[Find.getCount()][4]; ?? ??? ??? ?ArrayList<Student> studentlist = Find.findStduent(student, pos); ?? ??? ??? ?int i = 0; ?? ??? ??? ?for(Student s : studentlist) { ?? ??? ??? ??? ?data[i][0] = s.getName(); ?? ??? ??? ??? ?data[i][1] = s.getGender(); ?? ??? ??? ??? ?data[i][2] = s.getId(); ?? ??? ??? ??? ?data[i][3] = s.getTeam(); ?? ??? ??? ??? ?i++; ?? ??? ??? ?} ?? ??? ??? ?return data; ?? ??? ?}catch(Exception e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return null; ?? ??? ?} ?? ?} ?? ? public static String[][] getRootDateVector(Root root){ ?? ??? ? ?? ??? ?try { ?? ??? ??? ?String[][] data = new String[Find.getCount()][2]; ?? ??? ??? ?ArrayList<Root> rootlist = Find.findRoot(root); ?? ??? ??? ?int i = 0; ?? ??? ??? ?for(Root r : rootlist) { ?? ??? ??? ??? ?data[i][0] = r.getUsername(); ?? ??? ??? ??? ?data[i][1] = r.getPassword(); ?? ??? ??? ??? ?i++; ?? ??? ??? ?} ?? ??? ??? ?return data; ?? ??? ?}catch(Exception e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return null; ?? ??? ?} ?? ?} }
具體效果如下:
支持模糊查詢,即通過 屬性1=內(nèi)容1&屬性2=內(nèi)容2 可以配合%_兩個符號查詢
這里筆者為了簡單,沒有精雕細(xì)琢,讀者可以根據(jù)自己的需要修改即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(控制臺版本)
- Java版學(xué)生管理系統(tǒng)
- Java畢業(yè)設(shè)計實(shí)戰(zhàn)之學(xué)生管理系統(tǒng)的實(shí)現(xiàn)
- java控制臺實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(IO版)
- java實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng)
- Java 實(shí)現(xiàn)完整功能的學(xué)生管理系統(tǒng)實(shí)例
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程
相關(guān)文章
java springboot的概述、特點(diǎn)與構(gòu)建介紹
大家好,本篇文章主要講的是springboot的概述、特點(diǎn)與構(gòu)建介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用詳細(xì)說明
mybatis中if和where用于動態(tài)sql的條件拼接,在查詢語句中如果缺失某個條件,通過if和where標(biāo)簽可以動態(tài)的改變查詢條件,下面這篇文章主要給大家介紹了關(guān)于Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用的詳細(xì)說明,需要的朋友可以參考下2023-03-03SpringMVC+EasyUI實(shí)現(xiàn)頁面左側(cè)導(dǎo)航菜單功能
這篇文章主要介紹了SpringMVC+EasyUI實(shí)現(xiàn)頁面左側(cè)導(dǎo)航菜單功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09SpringCloud客戶端報錯:- was unable to send&nb
這篇文章主要介紹了SpringCloud客戶端報錯:- was unable to send heartbeat!的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05IntelliJ IDEA中查看文件內(nèi)所有已聲明的方法(類似eclipse的outline)
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中查看文件內(nèi)所有已聲明的方法(類似eclipse的outline),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10詳解SpringBoot構(gòu)建Docker鏡像的3種方式
這篇文章主要介紹了SpringBoot構(gòu)建Docker鏡像的3種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06