jsp/javabean分頁列出資料
來源:互聯網
上載者:User
我的第一個jsp分頁代碼
jsp檔案:pagedata.jsp //顯示資料
javabean:DataConn.java //串連資料庫
javabean:GetCount.java //獲得資料記錄數
DataConn.java檔案內容:
package aliang_org;
import java.sql.*;
public class DataConn{
public static Connection getDataConn() {
Connection conn = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=your","your","your");
}
catch(Exception ex) {
ex.printStackTrace();
}
return conn;
}
}
GetCount .java檔案內容:
package aliang_org;
import java.sql.*;
import java.util.*;
import java.io.*;
public class GetCount {
private Connection conn;
public GetCount()throws Exception{
conn = DataConn.getDataConn();
}
public int getCounter()throws Exception{
int ret = 0;
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select count(*) from UserInfos");
while(rset.next()){
ret = rset.getInt(1);
}
return ret;
}
}
pagedata.jsp 檔案內容:
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import = "aliang_org.*"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.sql.*"%>
<html>
<head>
<title>
jsp/JavaBean分頁列出資料
</title>
</head>
<body bgcolor="#ffffff">
<h1>jsp/JavaBean分頁列出資料</h1>
<hr />
<jsp:useBean id="dataConnection" class="aliang_org.DataConn" scope="page" />