<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了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 可以配合%_兩個符號查詢
這裡筆者為了簡單,沒有精雕細琢,讀者可以根據自己的需要修改即可。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45