JavaSwing實現小型學生管理系統
本文實例為大傢分享瞭JavaSwing實現小型學生管理系統的具體代碼,供大傢參考,具體內容如下
在項目中建立四個包,分別是com.wu.JavaBean、com.wuJavaDao、com.wu.JavaService、com.wu.JavaView
數據庫表結構
學生表隻有四個屬性:學生姓名、學生性別、學生學號(主鍵)、學生班級
管理員表隻有兩個屬性:管理員用戶名(主鍵)、管理員密碼
這裡筆者為瞭簡單,學生表隻寫瞭四個屬性,管理員表隻寫瞭兩個屬性。
在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代表超級管理員 } }
建立數據庫連接DAO層,即在JavaDao包下建立JDBC.java,該程序隻是控制數據庫的連接:
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; /** * 獲取數據庫連接對象 * @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); // 註冊驅動 sqllink = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 連接數據庫 return this.sqllink; }catch(SQLException e) { e.printStackTrace(); System.out.println("連接數據庫異常"); // 錯誤信息顯示到控制臺 return this.sqllink; } } /** * 關閉數據庫連接對象 * @throws Exception */ public void closeConnection() throws Exception{ try { if(this.sqllink != null) { this.sqllink.close(); } }catch(SQLException e) { System.out.println("關閉數據庫連接異常"); } } }
com.wu.JavaBean和com.wu.JavaDao已經全部完成瞭,接下來就是完成業務邏輯JavaService包下的實現
對於增添數據的業務方法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 學生或者管理員 * @return * @throws Exception */ public static boolean add(Object element) throws Exception{ // 多態 // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 if(element instanceof Student) { String sql = "insert into student(name,gender,id,team) values(?,?,?,?)"; // mysql插入語句 Student student = (Student) element; // 向下轉型 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(); // 執行操作 return (count == 1) ? true : false; }catch(SQLException e) { return false; }finally{ jdbc.closeConnection(); // 關閉數據庫連接 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; // 向下轉型 // 超級管理員權限 if(!root.isSuperRoot()) { return false; } try { sqlaction = sqllink.prepareStatement(sql); // 操作對象 sqlaction.setString(1,root.getUsername()); sqlaction.setString(2,root.getPassword()); int count = sqlaction.executeUpdate(); // 執行操作 return (count == 1) ? true : false; }catch(SQLException e) { return false; }finally{ jdbc.closeConnection(); // 關閉數據庫連接 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 { /** * 移除學生信息 * @param student 待移除的學生 * @param pos 移除方式 * @return * @throws Exception */ public static boolean removeStudent(Student student ,String username,int pos) throws Exception{ // 部分修改或者全部修改 // 權限判斷 隻有超級管理員才能實現全部學生刪除 if (pos == 0 && !Find.getAccess(username).equals("1")) { return false; } // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 String sql = ""; String[] info = new String[4]; /** * 0代表刪除所有學生 * 1代表刪除所有姓名為name的學生 * 2代表刪除所有性別為gender的學生 * 3代表刪除一個學號為id的學生 * 4代表刪除所有班級為team的學生 * 5代表刪除所有姓名為name性別為gender的學生 * 6代表刪除一個學號為id姓名為name的學生 * 7代表刪除所有姓名為name的班級為team的學生 * 8代表刪除性別為gender學號為id的一個學生 * 9代表刪除所有性別為gender班級為team的學生 * 10代表刪除一個學號為id班級為team的學生 * 11代表刪除一個姓名為name性別為gender學號為id的學生 * 12代表刪除所有姓名為name性別為gender班級為team的學生 * 13代表刪除刪除一個姓名為name學號為id班級為team的學生 * 14代表刪除一個性別為gender學號為id班級為team的學生 * 15代表刪除一個姓名為name性別為gender學號為id班級為team的學生 */ 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{ // 完全刪除 // 權限判斷 if(!root.isSuperRoot()) { return false; } // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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 { /** * 查找學生信息 * @param student 待查找的學生 * @param pos 查找方式 * @return * @throws Exception */ public static ArrayList<Student> findStduent(Student student,int pos) throws Exception{ // 查詢所有學生或者部分學生 // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 ResultSet result = null; // 結果集 String sql = ""; ArrayList<Student> studentlist = new ArrayList<Student>(); // 返回的結果 /** * 0 代表查詢全部 * 1 代表查詢所有姓名為name的學生 * 2 代表查詢所有性別為gender的學生 * 3 代表查詢一個學號為id的學生 * 4 代表查詢所有班級為team的學生 * 5 代表查詢...同刪除操作 */ switch(pos) { case 0: sql = "select * from student"; try { sqlaction = sqllink.prepareStatement(sql); result = sqlaction.executeQuery(); // 執行查詢操作 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)); // 添加至返回結果中 } 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(); // 執行查詢操作 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)); // 添加至返回結果中 } return studentlist; }catch(SQLException e) { e.printStackTrace(); return null; }finally { jdbc.closeConnection(); if(sqlaction != null) { sqlaction.close(); } } } /** * 超級管理員權限 * 查找所有管理員 * @param root 驗證屬性 * @return * @throws Exception */ public static ArrayList<Root> findRoot(Root root) throws Exception{ // 完全查找權限 // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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(); } } } /** * 獲取權限信息 * @param username 用戶名 * @return * @throws Exception */ public static String getAccess(String username) throws Exception{ // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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{ // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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 { /** * 更新學生信息 * @param oldstudent 待修改的學生 * @param newstudent 修改後的學生 * @param pos 修改方式 * @return * @throws Exception */ public static boolean updateStudent(Student oldstudent,Student newstudent,int pos) throws Exception{ // 部分或者完全更新模式 // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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(); //執行操作 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(); } } } /** * 超級管理員權限 * @param root 待更新的管理員 * @param info 更新信息 * @param pos 更新方式 * @return * @throws Exception */ public static boolean updateRoot(Root root ,String info,int pos) throws Exception{ // 完全更新模式 // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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(); } } } }
加上管理員登錄認證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 { /** * 管理員登錄認證 * @param root 管理員 * @return * @throws Exception */ public static boolean rootIsExist(Root root) throws Exception { // 獲取數據庫對象 JDBC jdbc = new JDBC(); Connection sqllink = jdbc.getConnection(); PreparedStatement sqlaction = null; // 創建一個數據庫操作對象 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(); // 關閉數據庫連接 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]+)"; // 匹配中文或者數字模式 String[] result = new String[5]; result[0] = "0"; // 默認為全部 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("學號")) { idflag = true; hashmap.put(3,mattcher.group(2)); }else if(mattcher.group(1).equals("班級")) { teamflag = true; hashmap.put(4,mattcher.group(2)); }else { } } } // 對應位置放置相關信息 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); // 設置寬高度 this.setTitle("登錄界面"); // 設置標題 this.setResizable(false); // 固定窗口大小 this.setUndecorated(true); // 去掉窗口的裝飾 this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); //采用指定的窗口裝飾風格 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 結束程序 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); // 登錄鍵邊框風格 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 { // 登錄認證 Root root = new Root(username,password,"0"); // 新建一個虛擬管理員對象 try { if(Exist.rootIsExist(root)) { new Menu(username); dispose(); // 關閉當前的窗口 }else { JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!", "錯誤",JOptionPane.WARNING_MESSAGE); } }catch(Exception e) { e.printStackTrace(); }finally { usernametext.setText(""); passwordtext.setText(""); } } } }); close.setBorder(border2); // 關閉鍵邊框風格 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); // 設置寬高度 Frame.setTitle("菜單界面"); // 設置標題 Frame.setResizable(false); // 固定窗口大小 Frame.setUndecorated(true); // 去掉窗口的裝飾 Frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); //采用指定的窗口裝飾風格 Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Frame.setLocationRelativeTo(null); // 主窗口的中心 Panel = this.getPanel(); this.setMenu(); // 設置菜單項 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); // 增加底部標簽 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("添加學生"); JMenuItem addmenuItem2 = new JMenuItem("添加管理員"); addmenu.add(addmenuItem1); addmenu.add(addmenuItem2); JMenu removemenu = new JMenu("刪除"); JMenuItem removemenuItem1 = new JMenuItem("刪除學生"); JMenuItem removemenuItem2 = new JMenuItem("刪除管理員"); removemenu.add(removemenuItem1); removemenu.add(removemenuItem2); JMenu findmenu = new JMenu("查找"); JMenuItem findmenuItem1 = new JMenuItem("查找學生"); JMenuItem findmenuItem2 = new JMenuItem("查找管理員"); findmenu.add(findmenuItem1); findmenu.add(findmenuItem2); JMenu updatemenu = new JMenu("修改"); JMenuItem updatemenuItem1 = new JMenuItem("修改學生"); JMenuItem updatemenuItem2 = new JMenuItem("修改管理員"); updatemenu.add(updatemenuItem1); updatemenu.add(updatemenuItem2); JMenu accessmenu = new JMenu("賬號設置"); 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); // 添加功能 監聽器實現 { addmenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Menu.Frame.remove(Menu.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = AddPanel.getPanel(Menu.this.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } // 移除功能 監聽器實現 { removemenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Menu.Frame.remove(Menu.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = RemovePanel.getPanel(Menu.this.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } // 查找功能 監聽器實現 { findmenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Menu.Frame.remove(Menu.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = FindPanel.getPanel(Menu.this.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } // 修改功能 監聽器實現 { updatemenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Menu.Frame.remove(Menu.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = UpdatePanel.getPanel(Menu.this.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } // 賬號功能 監聽器實現 { accessmenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Menu.Frame.remove(Menu.Panel); // 移除當前的佈局 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); // 移除當前的佈局 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); // 移除當前的佈局 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); // 移除當前的佈局 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; } }
筆者這裡由於增刪改查界面設計繁瑣,亦限於篇幅,隻展示‘查’的這一部分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); //設置佈局大小 Panel.setBackground(Color.LIGHT_GRAY); setMenu(); // 設置菜單項 find(flag); // 增添學生或管理員 flag 1:學生 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("添加學生"); JMenuItem addmenuItem2 = new JMenuItem("添加管理員"); addmenu.add(addmenuItem1); addmenu.add(addmenuItem2); JMenu removemenu = new JMenu("刪除"); JMenuItem removemenuItem1 = new JMenuItem("刪除學生"); JMenuItem removemenuItem2 = new JMenuItem("刪除管理員"); removemenu.add(removemenuItem1); removemenu.add(removemenuItem2); JMenu findmenu = new JMenu("查找"); JMenuItem findmenuItem1 = new JMenuItem("查找學生"); JMenuItem findmenuItem2 = new JMenuItem("查找管理員"); findmenu.add(findmenuItem1); findmenu.add(findmenuItem2); JMenu updatemenu = new JMenu("修改"); JMenuItem updatemenuItem1 = new JMenuItem("修改學生"); JMenuItem updatemenuItem2 = new JMenuItem("修改管理員"); updatemenu.add(updatemenuItem1); updatemenu.add(updatemenuItem2); JMenu accessmenu = new JMenu("賬號設置"); 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); // 添加功能 響應 { addmenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { FindPanel.Frame.remove(FindPanel.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = AddPanel.getPanel(FindPanel.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } //移除功能 響應 { removemenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { FindPanel.Frame.remove(FindPanel.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = RemovePanel.getPanel(FindPanel.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } //查看功能 響應 { findmenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { FindPanel.Frame.remove(FindPanel.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = FindPanel.getPanel(FindPanel.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } //修改功能 響應 { updatemenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { FindPanel.Frame.remove(FindPanel.Panel); // 移除當前的佈局 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); // 移除當前的佈局 Frame.repaint(); Panel = UpdatePanel.getPanel(FindPanel.username,2); // 切換佈局 Frame.add(Panel); Frame.setVisible(true); } }); } //賬號功能 響應 { accessmenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { FindPanel.Frame.remove(FindPanel.Panel); // 移除當前的佈局 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); // 移除當前的佈局 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); // 移除當前的佈局 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); // 移除當前的佈局 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("學生信息查詢"); title.setBounds(200,45,90,30); Panel.add(title); String[] Attribute = {"姓名","性別","學號","班級"}; // 構建搜索框 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); // 搜索功能 響應 { search.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String search = searchtext.getText(); String[] deal = DealString.deal(search); // 第一個位置為對應的查找方式 第二至最後一個位置為存放的相應的信息 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); // 默認為查看所有學生 model.setDataVector(dataVector, Attribute); }else if(flag == 2) { try{ if(Find.getAccess(FindPanel.username).equals("0")) { JOptionPane.showMessageDialog(null, "權限不夠,無法訪問!", "錯誤",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, "權限不夠,無法訪問!", "錯誤",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=內容1&屬性2=內容2 可以配合%_兩個符號查詢
這裡筆者為瞭簡單,沒有精雕細琢,讀者可以根據自己的需要修改即可。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。