jsp import mysql con_jsp的分页查询的代码(mysql数据库)
[原來沒有寫過,聽朋友說,比較難,就試一下,還是挺簡單的,只不過,具體實用方面還得自己寫,沒有什么難的,自己看看就懂了。bean文件:package bean;/**?* jsp+java bea
這個功能一共創建了兩個javabean組件和一個jsp頁面顯示分頁頁面,第一個是處理以數據庫連接的javabean,第一個javabean是處理分頁查詢結果的代碼,第三個jsp是調用第二個javabean,顯示分頁查詢結果!
//下面是連接mysql數據庫的一個javabean的代碼:
package data;
import java.sql.*;
public class LoginData{
Connection conn=null;
public LoginData(){
this.connect();
}
public Connection getConn(){
return this.conn;
}
public boolean connect(){
try{
//使用JDBC橋創建數據庫連接
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//使用DriverManager類的getConnection()方法建立連接
//第一個參數定義用戶名,第二個參數定義密碼
this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312","root","123456");
}catch(Exception ex){
ex.printStackTrace();
return false;
}
return true;
}
}
//下面是一個javabean的處理mysql數據庫的分頁顯示查詢的代碼[JSP連接mysql需要用到驅動,可在mysql官網下載mysql數據庫Java驅動:http://www.mysql.com/downloads/connector/j/(需要注冊)。下載后解壓,然后把里面的mysql-connecto
package data;
import java.sql.*;
import java.util.*;
public class strongSplitPage
{
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
private ResultSetMetaData rsmd=null;
//sql 查詢語句
private String sqlStr;
//總紀錄數目
private int rowCount;
//所分得邏輯頁數
private int pageCount;
//每頁顯示的紀錄數目
private int pageSize;
//定義表的列數目
private int columnCount;
private int irows;
public void initialize(String sqlStr,int pageSize,int showPage)
{
this.sqlStr=sqlStr;
this.irows=pageSize*(showPage-1);
this.pageSize=pageSize;
try
{
LoginData loginData=new data.LoginData();
this.conn=loginData.getConn();
this.stmt=this.conn.createStatement();
this.rs=this.stmt.executeQuery(this.sqlStr);
this.rsmd=this.rs.getMetaData();
if(this.rs!=null)
{
this.rs.last();
this.rowCount=this.rs.getRow();
this.rs.first();
this.columnCount=this.rsmd.getColumnCount();
this.pageCount=(this.rowCount-1)/this.pageSize+1;
this.rs.close();
this.stmt.close();
}
this.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;
this.stmt=this.conn.createStatement();
this.rs=this.stmt.executeQuery(this.sqlStr);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public Vector getPage()
{
Vector vData=new Vector();
try
{
if(this.rs!=null)
{
while(this.rs.next())
{
String[] sData=new String[this.columnCount];
for(int j=0;j
{
sData[j]=this.rs.getString(j+1);
}
vData.addElement(sData);
}
this.rs.close();
this.stmt.close();
this.conn.close();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return vData;
}
//獲得頁面總數
public int getPageCount()
{
return this.pageCount;
}
//獲得數據表中總紀錄數
public int getRowCount()
{
return this.rowCount;
}
}
//下面是jsp顯示分頁頁面
//顯示每頁的紀錄數
int pageSize=10;
String sqlStr="";
//當前頁
int showPage=1;
%>
sqlStr="select * from userinfo order by id ";
String strPage=null;
//獲得跳轉到的頁面
strPage=request.getParameter("showPage");
if(strPage==null){
showPage=1;
pages.initialize(sqlStr,pageSize,showPage);
}else{
try{
showPage=Integer.parseInt(strPage);
pages.initialize(sqlStr,pageSize,showPage);
}catch(NumberFormatException ex){
showPage=1;
pages.initialize(sqlStr,pageSize,showPage);
}
if(showPage<1){
showPage=1;
pages.initialize(sqlStr,pageSize,showPage);
}
if(showPage>pages.getPageCount()){
showPage=pages.getPageCount();
pages.initialize(sqlStr,pageSize,showPage);
}
}
//取得要顯示的數據集合
Vector vData=pages.getPage();
%>
分頁顯示個人基本信息
for(int i=0;i
{
//顯示數據數
String[] sData=(String[])vData.get(i);
%>
}
%>
共條?條/頁??第頁/共頁??[首頁]?
//判斷“上一頁”鏈接是否要顯示
if(showPage>1){
%>
[上一頁]?
}
else{
%>
[上一頁]?
}
//判斷“下一頁”鏈接是否顯示
if(showPage
{
%>
[下一頁]?
}
else{
%>
[下一頁]?
}
%>
[尾頁]?轉到
for(int x=1;x<=pages.getPageCount();x++)
{
%>
if(showPage==x){
out.println("selected");
}
%> >
}
%>
頁???
[分頁顯示數據庫信息分頁顯示數據庫記錄??? 用戶名??? 性別??? 電話??? E-Mailtry{//驅動程序名String driverName=sun.jdbc.odbc.JdbcOdbcDriver;//連接字符串String url=j
總結
以上是生活随笔為你收集整理的jsp import mysql con_jsp的分页查询的代码(mysql数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 产品推广系统推荐乐云seo_优化推广公司
- 下一篇: sql如何遍历几百万的表_Oracle