標籤:style blog ar io color os sp for java
資料庫的連結的操作類
package utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class DBConnection { private static final String DBDRIVER = "com.mysql.jdbc.Driver" ; //驅動類類名 private static final String DBURL = "jdbc:mysql://localhost:3306/db_votemanage";//串連URL private static final String DBUSER = "root" ; //資料庫使用者名稱 private static final String DBPASSWORD = "root"; //資料庫密碼 public static Connection getConnection(){ Connection conn = null; //聲明一個連線物件 try { Class.forName(DBDRIVER); //註冊驅動 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); //獲得連線物件 } catch (ClassNotFoundException e) { //捕獲驅動類無法找到異常 e.printStackTrace(); } catch (SQLException e) { //捕獲SQL異常 e.printStackTrace(); } return conn; } public static void close(Connection conn) {//關閉連線物件 if(conn != null) { //如果conn連線物件不為空白 try { conn.close(); //關閉conn連線物件對象 } catch (SQLException e) { e.printStackTrace(); } } } public static void close(PreparedStatement pstmt) {//關閉預先處理對象 if(pstmt != null) { //如果pstmt預先處理對象不為空白 try { pstmt.close(); //關閉pstmt預先處理對象 } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs) {//關閉結果集對象 if(rs != null) { //如果rs結果集對象不為null try { rs.close(); //關閉rs結果集對象 } catch (SQLException e) { e.printStackTrace(); } } }}
分頁的工具類
package utils;public class Page { private int everyPage; //每頁顯示記錄數 private int totalCount; //總記錄數 private int totalPage; //總頁數 private int currentPage; //當前頁 private int beginIndex; //查詢起始點 private boolean hasPrePage; //是否有上一頁 private boolean hasNextPage; //是否有下一頁 public Page(int everyPage, int totalCount, int totalPage, int currentPage,int beginIndex, boolean hasPrePage, boolean hasNextPage) { //自訂構造方法 this.everyPage = everyPage; this.totalCount = totalCount; this.totalPage = totalPage; this.currentPage = currentPage; this.beginIndex = beginIndex; this.hasPrePage = hasPrePage; this.hasNextPage = hasNextPage; } public Page(){} //預設建構函式 public int getEveryPage() { //獲得每頁顯示記錄數 return everyPage; } public void setEveryPage(int everyPage) {//設定每頁顯示記錄數 this.everyPage = everyPage; } public int getTotalCount() {//獲得總記錄數 return totalCount; } public void setTotalCount(int totalCount) {//設定總記錄數 this.totalCount = totalCount; } public int getTotalPage() {//獲得總頁數 return totalPage; } public void setTotalPage(int totalPage) {//設定總頁數 this.totalPage = totalPage; } public int getCurrentPage() {//獲得當前頁 return currentPage; } public void setCurrentPage(int currentPage) {//設定當前頁 this.currentPage = currentPage; } public int getBeginIndex() {//獲得查詢起始點 return beginIndex; } public void setBeginIndex(int beginIndex) {//設定查詢起始點 this.beginIndex = beginIndex; } public boolean isHasPrePage() {//獲得是否有上一頁 return hasPrePage; } public void setHasPrePage(boolean hasPrePage) {//設定是否有上一頁 this.hasPrePage = hasPrePage; } public boolean isHasNextPage() {//獲得是否有下一頁 return hasNextPage; } public void setHasNextPage(boolean hasNextPage) {//設定是否有下一頁 this.hasNextPage = hasNextPage; }}-------------------------------------------------------------------------------package utils;/* * 分頁資訊輔助類 */public class PageUtil { public static Page createPage(int everyPage,int totalCount,int currentPage) {//建立分頁資訊對象 everyPage = getEveryPage(everyPage); currentPage = getCurrentPage(currentPage); int totalPage = getTotalPage(everyPage, totalCount); int beginIndex = getBeginIndex(everyPage, currentPage); boolean hasPrePage = getHasPrePage(currentPage); boolean hasNextPage = getHasNextPage(totalPage, currentPage); return new Page(everyPage, totalCount, totalPage, currentPage, beginIndex, hasPrePage, hasNextPage); } public static int getEveryPage(int everyPage) { //獲得每頁顯示記錄數 return everyPage == 0 ? 10 : everyPage; } public static int getCurrentPage(int currentPage) { //獲得當前頁 return currentPage == 0 ? 1 : currentPage; } public static int getTotalPage(int everyPage,int totalCount) {//獲得總頁數 int totalPage = 0; if(totalCount != 0 &&totalCount % everyPage == 0) { totalPage = totalCount / everyPage; } else { totalPage = totalCount / everyPage + 1; } return totalPage; } public static int getBeginIndex(int everyPage,int currentPage) {//獲得起始位置 return (currentPage - 1) * everyPage; } public static boolean getHasPrePage(int currentPage) {//獲得是否有上一頁 return currentPage == 1 ? false : true; } public static boolean getHasNextPage(int totalPage, int currentPage) { //獲得是否有上一頁 return currentPage == totalPage || totalPage == 0 ? false : true; }}
web常用的工具類總結