Linked action classes for the database
PackageUtils;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classDBConnection {Private Static FinalString dbdriver = "Com.mysql.jdbc.Driver";//driver class class name Private Static FinalString Dburl = "Jdbc:mysql://localhost:3306/db_votemanage";//Connection URL Private Static FinalString DBUSER = "root";//Database user name Private Static FinalString Dbpassword = "root";//Database Password Public StaticConnection getconnection () {Connection conn=NULL;//declaring a Connection object Try{class.forname (dbdriver); //Registration Driverconn = Drivermanager.getconnection (Dburl,dbuser,dbpassword);//Get the Connection object}Catch(ClassNotFoundException e) {//The capture driver class could not find the exceptionE.printstacktrace (); } Catch(SQLException e) {//Capturing SQL ExceptionsE.printstacktrace (); } returnConn; } Public Static voidClose (Connection conn) {//Close the Connection object if(Conn! =NULL) {//if the Conn connection object is not empty Try{conn.close (); //close the Conn Connection object Object}Catch(SQLException e) {e.printstacktrace (); } } } Public Static voidClose (PreparedStatement pstmt) {//Close preprocessed Objects if(Pstmt! =NULL) {//if the pstmt preprocessing object is not empty Try{pstmt.close (); //Close pstmt preprocessing objects}Catch(SQLException e) {e.printstacktrace (); } } } Public Static voidClose (ResultSet rs) {//Close result Set object if(rs! =NULL) {//if the RS result set object is not NULL Try{rs.close (); //Close RS Result set object}Catch(SQLException e) {e.printstacktrace (); } } }}
Tool class for pagination
PackageUtils; Public classPage {Private intEverypage;//shows the number of records per page Private intTotalCount;//Total Record Count Private intTotalpage;//Total Pages Private intCurrentPage;//Current Page Private intBeginindex;//Query Start point Private BooleanHasprepage;//do you have a previous page Private BooleanHasnextpage;//do you have the next page ? PublicPage (intEverypage,intTotalCount,intTotalpage,intCurrentPage,intBeginindex,BooleanHasprepage,BooleanHasnextpage) {//Custom Construction Methods This. Everypage =everypage; This. TotalCount =TotalCount; This. Totalpage =Totalpage; This. currentpage =currentpage; This. Beginindex =Beginindex; This. Hasprepage =Hasprepage; This. Hasnextpage =Hasnextpage; } PublicPage () {}//default constructor Public intGeteverypage () {//get the number of records displayed per page returneverypage; } Public voidSeteverypage (intEverypage) {//set the number of records to display per page This. Everypage =everypage; } Public intGettotalcount () {//get total Record Count returnTotalCount; } Public voidSettotalcount (intTotalCount) {//set total number of records This. TotalCount =TotalCount; } Public intGettotalpage () {//Get Total Pages returnTotalpage; } Public voidSettotalpage (intTotalpage) {//Set Total pages This. Totalpage =Totalpage; } Public intGetcurrentpage () {//get the current page returncurrentpage; } Public voidSetcurrentpage (intCurrentPage) {//set the current page This. currentpage =currentpage; } Public intGetbeginindex () {//get query start point returnBeginindex; } Public voidSetbeginindex (intBeginindex) {//set query start point This. Beginindex =Beginindex; } Public BooleanIshasprepage () {//get a previous page returnHasprepage; } Public voidSethasprepage (BooleanHasprepage) {//set whether there is a previous page This. Hasprepage =Hasprepage; } Public BooleanIshasnextpage () {//get the next page returnHasnextpage; } Public voidSethasnextpage (BooleanHasnextpage) {//set whether there is a next page This. Hasnextpage =Hasnextpage; }}------------------------------------------------------------------------------- PackageUtils;/** Paging Information auxiliary class*/ Public classPageutil { Public StaticPage Createpage (intEverypage,intTotalCount,intCurrentPage) {//Create a paging information objectEverypage =geteverypage (everypage); CurrentPage=getcurrentpage (currentpage); intTotalpage =gettotalpage (Everypage, totalcount); intBeginindex =Getbeginindex (Everypage, currentpage); BooleanHasprepage =gethasprepage (currentpage); BooleanHasnextpage =gethasnextpage (Totalpage, currentpage); return NewPage (Everypage, TotalCount, Totalpage, CurrentPage, Beginindex, Hasprepage, hasnextpage); } Public Static intGeteverypage (intEverypage) {//get the number of records displayed per page returnEverypage = = 0? 10: everypage; } Public Static intGetcurrentpage (intCurrentPage) {//get the current page returnCurrentPage = = 0? 1: currentpage; } Public Static intGettotalpage (intEverypage,intTotalCount) {//Get Total Pages intTotalpage = 0; if(TotalCount! = 0 &&totalcount% Everypage = = 0) {Totalpage= TotalCount/everypage; } Else{totalpage= Totalcount/everypage + 1; } returnTotalpage; } Public Static intGetbeginindex (intEverypage,intCurrentPage) {//Get start position return(currentPage-1) *everypage; } Public Static BooleanGethasprepage (intCurrentPage) {//get a previous page returnCurrentPage = = 1?false:true; } Public Static BooleanGethasnextpage (intTotalpage,intCurrentPage) {//get a previous page returnCurrentPage = = Totalpage | | Totalpage = = 0?false:true; }}
Summary of common tools in the Web