Paging Query Advanced paging query can be blurred query the source of the

Source: Internet
Author: User

The technology comes from me and my friend, the great god of 25th, the Super Bull.

The following creates three tool classes an entity class to call him. You can also create your own database by querying the

Package org.lange.page.base;


Import java.util.ArrayList;
Import java.util.List;

Import Org.apache.catalina.security.SecurityUtil;

public class Basepageso {


/**
* Current Page
*/
private int pagenumber = 1;
/**
* Number of records per page pages size
*/
private int objectsperpage = 50;

Public Basepageso () {
}
public int Getpagenumber () {
return pagenumber;
}
public void Setpagenumber (int pagenumber) {
if (pagenumber <= 0) {
PageNumber = 1;
}
This.pagenumber = pagenumber;
}
public int getobjectsperpage () {
return objectsperpage;
}
public void setobjectsperpage (int objectsperpage) {
This.objectsperpage = Objectsperpage;
}



Public Integer GetOffset () {
Return (Getpagenumber ()-1) * getobjectsperpage ();
}

Public Integer Getlimit () {
return Getobjectsperpage ();

}
}

The above is the class of Penbean

Package org.lange.page.entity;

Import Org.lange.page.base.BasePageSO;

public class Dome1 extends basepageso{


private String name;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
private int age;


}

The above is the Dome1 class

Package Org.lange.page.javabean;


Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import javax.management.RuntimeErrorException;

Import org.lange.page.entity.Dome1;

public class Dome2

{
Private String URL = "JDBC:MYSQL://LOCALHOST:3306/DAY15";
Private String user= "root";
Private String password= "root";

private int pagenumber;
private int numperpage;
private String name;
Private Long age;

Public Dome2 (int pagenumber, int. numperpage, String name, Long age) {
Super ();
This.pagenumber = pagenumber;
This.numperpage = Numperpage;
THIS.name = name;
This.age = age;
}

public void Querybypage ()
{//Driver registration Program
Java.sql.Statement stmt = null;
Connection conn = null;

try {
1: Driver Registration Program
Class.forName ("Com.mysql.jdbc.Driver");

Get linked object

Conn=drivermanager.getconnection (URL, user, password);

3: Create a statement
Stmt= conn.createstatement ();

4: Create SQL statement
String sql = "CREATE TABLE along (id INT PRIMARY KEY auto_increment,name varchar (), gender varchar (2))";

5: Send SQL statement to get SQL statement return result return int value
int count = stmt.executeupdate (SQL);
6: Output SQL statement
SYSTEM.OUT.PRINTLN ("result" +count+ "line");
7: Can't let him keep the link yes that server memory does not crash: So to close it I

Dome1 d = new Dome1 ();
D.setpagenumber (pagenumber);
D.setobjectsperpage (Numperpage);

int offset = D.getoffset ();
int limit = D.getlimit ();
String sql = "SELECT * from Chaxun where 1=1";

if (name!=null && name.length ()! = 0)
SQL + = "and name like '%" + name + "% '";
if (age! = null)
SQL + = "and age =" + age;

SQL + = "Limit" + offset + "," + limit;

String sqlcount = "SELECT count (*) from Chaxun where 1 = 1";
if (name!=null && name.length ()! = 0)
SqlCount + = "and name like '%" + name + "% '";
if (age! = null)
SqlCount + = "and age =" + age;

SYSTEM.OUT.PRINTLN (SQL);


ResultSet rs = stmt.executequery (sql);//
while (Rs.next ()) {
System.out
. println (rs.getstring (1) + "T" + rs.getstring (2));//
}





} catch (Error e) {

E.printstacktrace ();
throw new Runtimeerrorexception (e);
} catch (ClassNotFoundException e) {

E.printstacktrace ();
} catch (SQLException e) {

E.printstacktrace ();
}finally{

if (stmt!=null)
try {
Stmt.close ();
} catch (Error e) {

E.printstacktrace ();
throw new Runtimeerrorexception (e);
} catch (SQLException e) {

E.printstacktrace ();
}
if (conn!=null)
try {
Conn.close ();
} catch (Error e) {

E.printstacktrace ();
throw new Runtimeerrorexception (e);
} catch (SQLException e) {

E.printstacktrace ();
}
}

}

The Dome2 class is used to connect the database

Package org.lange.page.test;
Import Org.junit.Test;
Import org.lange.page.javabean.Dome2;


public class Testpage {

@Test
public void Pagetest () {
Dome2 d = new Dome2 (0,0, "Li", null);
D.querybypage ();
}

The above three methods are called by the entity class.

Big Brother said that JSP is a little difficult to get back, but this direct copy and paste with the line

Paging Query Advanced paging query can be blurred query the source of the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.