Home>

Here is a mentality of doing and remembering,Record the learning process,Learn fast and forget fast,Afraid of forgetting,For future use.

Part of the code used is to find it online,And modify it yourself,Plus your own understanding.I do n’t know if it ’s original.Just make your own learning record.

Use related:postgresql database, dom4j, jsp, servlet

First, the engineering pattern,A global view for readers and themselves to view and use

The idea is:

Record configuration information in config.xml file,To facilitate database changes,Easy to transplant and reuse.

dom4jutil.java is used to parse the xml properties file to get the required data

postgresql_util.java for data connection and database operations

pageproperties.java for table paging properties javabean

pageproperties.java encapsulates paging operations

page.java is the main operation of paging

tablepage.jsp is the effect display interface

Third-party jar packages used:

dom4j-1.6.1.jar for xml file parsing

postgresql-9.3-1101.jdbc4.jar is used by jdbc to connect to the postgresql database

The pagination effect is as follows:you can turn pages by clicking the previous page and the next page,Enter the specified page to jump (out of range to the first or last page). For specific implementation, please refer to the detailed code.I posted them all.A little rookie,At the learning stage,Of course, it is better to have a great god to point it out.Hope you can enlighten me!

Specific code implementation

1, config.xml database connection information properties file

<?xml version="1.0" encoding="utf-8"?>
<! Doctype postgres [
 <! Element postgres (driver, url, username, pwd)>
 <! Element driver (#pcdata)>
 <! Element url (#pcdata)>
 <! Element username (#pcdata)>
 <! Element pwd (#pcdata)>
]>
<postgres>
 <driver>org.postgresql.driver</driver>
 <url>jdbc:postgresql://localhost:5432/java</url>
 <username>admin</username>
 <pwd>k42jc</pwd>
</postgres>

2.dom4jutil.java

package util;
import org.dom4j.document;
import org.dom4j.documentexception;
import org.dom4j.element;
import org.dom4j.io.saxreader;
/**
 * Used to parse xml property files
 * @author johsonmuler
 *
 * /
public class dom4jutil {
 private static element root=null;
 static {//static code block
  //Create a parsing object
  saxreader sr=new saxreader ();
  //Get the current project path
//string url=system.getproperty ("user.dir");
  string url=dom4jutil.class.getresource (""). getpath ();
//system.out.println (url);
  try {
   //Get configuration file information by file path
   document doc=sr.read (url + "config.xml");
   //Get the root node
   root=doc.getrootelement ();
  } catch (documentexception e) {
   e.printstacktrace ();
  }
 }
 public static string getpostgresdata (string str) {
  //Based on the root node,Get profile data
  element e=root.element (str);
  string data=e.gettext ();
  return data;
 }
 public static void main (string [] args) {
//string url=dom4jutil.class.getresource (".."). getpath ();
//system.out.println (system.getproperty ("user.dir"));
//system.out.println (url);
  string driver=getpostgresdata ("driver");
  string url=getpostgresdata ("url");
  system.out.println (driver);
  system.out.println (url);
 }
}

3.postgresql_util.java

package util;
import java.sql.preparedstatement;
import java.sql.statement;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.list;
public class postgresql_util {
 private static dom4jutil dom=new dom4jutil ();
 private static connection c=null;
 private static resultset rs=null;
 private static string driver=dom.getpostgresdata ("driver");
 private static string url=dom.getpostgresdata ("url");
 private static string username=dom.getpostgresdata ("username");
 private static string pwd=dom.getpostgresdata ("pwd");
 public postgresql_util () {
  try {
   class.forname (driver);
   c=drivermanager.getconnection (url);
  } catch (classnotfoundexception e) {
   system.out.println ("The specified class was not found:" + e.getmessage ());
  } catch (sqlexception e) {
   system.out.println ("Get connection exception:" + e.getmessage ());
  }
 }
 /**
  * Data query method (statement)
  * @param sql
  * @return
  * @throws sqlexception
  * /
 public resultset executequery (string sql) throws sqlexception {
  statement s=c.createstatement ();
  rs=s.executequery (sql);
  return rs;
 }
 /**
  * Overloaded methods (preparedstatement)
  * @param sql
  * @param list
  * @return
  * @throws sqlexception
  * /
 public resultset executequery (string sql, list<object>list) throws sqlexception {
  preparedstatement ps=c.preparestatement (sql);
  for (int i=0;i<list.size ();i ++) {
   system.out.println (list.get (i));
   system.out.println (i + 1);
   ps.setobject (i + 1, list.get (i));
  }
  rs=ps.executequery ();
  c.close ();
  return rs;
 }
 /**
  * Data update method (add, delete, change) (statement)
  * @param sql
  * @throws sqlexception
  * /
 public int executeupdate (string sql) throws sqlexception {
  statement s=c.createstatement ();
  int i=s.executeupdate (sql);
  c.close ();
  return i;
 }
 /**
  * Overloaded methods (preparedstatement)
  * @param sql
  * @param list
  * @throws sqlexception
  * /
 public int executeupdate (string sql, list<object>list) throws sqlexception {
  preparedstatement ps=c.preparestatement (sql);
  for (int i=0;i<list.size ();i ++) {
   ps.setobject (i + 1, list.get (i));
  }
  int i=ps.executeupdate ();
  c.close ();
  return i;
 }
 /**
  * Separate Get Connection
  * @return
  * @throws classnotfoundexception
  * @throws sqlexception
  * /
 public static connection getconnection () throws classnotfoundexception, sqlexception {
  class.forname (driver);
  c=drivermanager.getconnection (url);
  return c;
 }
}

Pageproperties.java

package bean;
import java.sql.resultset;
public class pageproperties {
 private int currentpage;//Current page number
 private int totalpages;//Total pages
 private int totalrecords;//Total number of data
 private resultset rs;//dynamic result set
 public pageproperties () {
  super ();
 }
 public pageproperties (int currentpage, int totalpages, int totalrecords,   resultset rs) {
  super ();
  this.currentpage=currentpage;
  this.totalpages=totalpages;
  this.totalrecords=totalrecords;
  this.rs=rs;
 }
 public int getcurrentpage () {
  return currentpage;
 }
 public void setcurrentpage (int currentpage) {
  this.currentpage=currentpage;
 }
 public int gettotalpages () {
  return totalpages;
 }
 public void settotalpages (int totalpages) {
  this.totalpages=totalpages;
 }
 public int gettotalrecords () {
  return totalrecords;
 }
 public void settotalrecords (int totalrecords) {
  this.totalrecords=totalrecords;
 }
 public resultset getrs () {
  return rs;
 }
 public void setrs (resultset rs) {
  this.rs=rs;
 }
}

5, tablepage.java

package bean;
import java.sql.resultset;
public class pageproperties {
 private int currentpage;//Current page number
 private int totalpages;//Total pages
 private int totalrecords;//Total number of data
 private resultset rs;//dynamic result set
 public pageproperties () {
  super ();
 }
 public pageproperties (int currentpage, int totalpages, int totalrecords,   resultset rs) {
  super ();
  this.currentpage=currentpage;
  this.totalpages=totalpages;
  this.totalrecords=totalrecords;
  this.rs=rs;
 }
 public int getcurrentpage () {
  return currentpage;
 }
 public void setcurrentpage (int currentpage) {
  this.currentpage=currentpage;
 }
 public int gettotalpages () {
  return totalpages;
 }
 public void settotalpages (int totalpages) {
  this.totalpages=totalpages;
 }
 public int gettotalrecords () {
  return totalrecords;
 }
 public void settotalrecords (int totalrecords) {
  this.totalrecords=totalrecords;
 }
 public resultset getrs () {
  return rs;
 }
 public void setrs (resultset rs) {
  this.rs=rs;
 }
}

6, page.java This is the main processing class,servlet

package servlet;
import java.io.ioexception;
import java.io.printwriter;
import java.sql.resultset;
import java.sql.sqlexception;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import util.postgresql_util;
import bean.pageproperties;
import bean.tablepage;
public class page extends httpservlet {
 public void service (httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  request.setcharacterencoding ("utf-8");
  response.setcontenttype ("text/html;charset=utf-8");
  printwriter out=response.getwriter ();
  /**
   * Set paging properties through tablepage
   *
   * /
  tablepage tb=new tablepage ();
  //Get the page number displayed in the current table
  int currentpage=tb.currentpage (tb.getstrpage (request, "page"));
  system.out.println (currentpage);
  //Set the number of data displayed per page
  tb.setpagerecord (10);//Set 10 data to display per page
  /**
   * Set jdbc connection and data processing through xxsql_util
   * /
  postgresql_util postgres=new postgresql_util ();
  try {
   resultset rs_count=postgres.executequery ("select count (*) as c from student");
   rs_count.next ();
   //Get the total number of data
   int totalrecords=rs_count.getint ("c");
   //Get the total number of page display tables based on the total number of data tables
   int totalpages=tb.gettotalpages (totalrecords);
   if (currentpage>totalpages) {
    currentpage=totalpages;//Ensure that the last page does not exceed the range
   }
   //Get dynamic result set based on database table information and current page information
   resultset rs=tb.getpageresultset (postgres.executequery ("select * from student"), currentpage);
   /**
    * Add data to javabean
    * /
   pageproperties pp=new pageproperties (currentpage, totalpages, totalrecords, rs);
   /**
    * Forward javabean to the front end
    * /
   request.setattribute ("result", pp);
   request.getrequestdispatcher ("tablepage.jsp"). forward (request, response);
  } catch (sqlexception e) {
   system.out.println ("class page:" + e.getmessage ());
//e.printstacktrace ();
  }
 }
}

7, tablepage.jsp foreground display effect

<%@page language="java" import="java.util. *" Pageencoding="utf-8"%>
<%@page import="java.sql.resultset"%>
<%@page import="bean.pageproperties"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
string path=request.getcontextpath ();
string basepath=request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";
%>
<! Doctype html public "-//w3c //dtd html 4.01 transitional //en">
<html>
 <head>
 <title>Simple Data Paging</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"&​​gt;
 <meta http-equiv="keywords" content="keyword1, keyword2, keyword3">
 <meta http-equiv="description" content="this is my page">
 <!-
 <link rel="stylesheet" type="text/css" href="styles.css">
 ->
 </head>
 <body>
 <table>
  <tr>
   <td>Name</td>
   <td>Gender</td>
   <td>age</td>
   <td>Fractions</td>
  </tr>
  <%
   pageproperties pp=(pageproperties) request.getattribute ("result");
   resultset rs=pp.getrs ();
  %>
  <%
   int i=1;
   while (rs.next ()) {
   %>
  <tr>
   <td><%= rs.getobject (1)%></td>
   <td><%= rs.getobject (2)%></td>
   <td><%= rs.getobject (3)%></td>
   <td><%= rs.getobject (4)%></td>
  </tr>
  <%
   i ++;
   if (i>10)
    break;
  }
   %>
   <br />
   <span><%= pp.gettotalpages ()%>pages</span>
  <span>total<%= pp.gettotalrecords ()%>data</span>
  <span>This page<%= i-1%>bar</span>
  <span><%= pp.getcurrentpage ()%>page</span>
  <p align="center">
   <%
    if (pp.getcurrentpage ()>1)
    {
   %><a href="<%= path%>/page?page =<%= pp.getcurrentpage ()-1%>"<<Previous page</a>
   <%
    }
   %>
   <%
    if (pp.getcurrentpage ()&pp.gettotalpages ())
    {
   %><a href="<%= path%>/page?page =<%= pp.getcurrentpage () + 1%>">Next page></a>
   <%
    }
   %>
   <input type="text" name="input_text" size="1" />
   <input type="button" name="skip" value="skip" onclick="skip ();" />
   <script>
    function skip () {
     var v=document.getelementbyid ("input_text"). value;
     location.href="page?page =" + v;
    }
   </script>
  </p>
 </table>
 </body>
</html>

At first glance, I feel that the background code is really tedious.But this is considering program robustness and portability,Facilitate code reuse.To use later,Configure the relevant jdbc driver in the properties file (config.xml) according to your own needs, and obtain the forwarding result ("result") of the background servlet (page.jsp) through the request on the jsp page, combined with the page properties (pageproperties.java class), that is, Can achieve the effect.

Of course, this is also because of personal learning,Tends to use more things.

jsp
  • Previous Follow me about JavaScript functions and function expressions
  • Next Android programming method to add text content to sqlite table