每日日报8月18日
1.今天学习
package javabean_package;
import java.sql.*;
publicclass t_conn {
//连接对象
private Connection con=null;
//获取连接的方法
public Connection getcon()
{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test","sa","");
}catch(Exception e)
{
e.printStackTrace();
}
returncon;
}
}
4. 新建javabean(userbean)
package javabean_package;
publicclass userbean {
public userbean()
{}
privateintid;
private String username;
private String userpwd;
private String useremail;
private String usergrade;
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
returnuserpwd;
}
publicvoid setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getUseremail() {
returnuseremail;
}
publicvoid setUseremail(String useremail) {
this.useremail = useremail;
}
public String getUsergrade() {
returnusergrade;
}
publicvoid setUsergrade(String usergrade) {
this.usergrade = usergrade;
}
}
5. 新建一个usercl类
introwcount;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
intpagesize=10;//每页显示行数
intpagecount=0;//页面数量
l 编写关闭数据方法
//关闭方法
privatevoid close()
{
try{
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
l 查询表中记录数方法
publicint getrowcount()
{
t_conn t=new t_conn();
conn=t.getcon();
try{
stmt=conn.createStatement();
rs=stmt.executeQuery("select count(*) from usertest");
if(rs.next())
{
rowcount=rs.getInt(1);
}
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
this.close();
}
returnrowcount;
}
l 计算分页数方法
publicint getpagecount()
{
if(rowcount%pagesize==0)
{
pagecount=rowcount/pagesize;
}
else
{
pagecount=rowcount/pagesize+1;
}
returnpagecount;
}
l 封装Arraylist方法
//查询数据库封装到arraylist中
public ArrayList getcontent(int pagenow)
{
//根据传入内容查询
t_conn t=new t_conn();
conn=t.getcon();
ArrayList al=new ArrayList();
//rowcount=this.getrowcount();
try{
stmt=conn.createStatement();
String sql="select top "+pagesize+" * from usertest where userid not in (select top "+pagesize*(pagenow-1)+" userid from usertest)";
rs=stmt.executeQuery(sql);
while(rs.next())
{ //将数据封装到al中:通过javabean
userbean ub=new userbean();
ub.setId(rs.getInt(1));
ub.setUsername(rs.getString(2));
ub.setUserpwd(rs.getString(3));
ub.setUseremail(rs.getString(4));
ub.setUsergrade(rs.getString(5));
al.add(ub);//arraylist获取javabean内容添加一条记录
}
}catch(Exception e)
{
e.printStackTrace();
}
return al;
}
6. 在index中调用
<%
int rowcount=0;//表中记录数
int pagenow=1;//用户点击页码
int pagecount=0;
usercl c=new usercl();
rowcount=c.getrowcount();
pagecount=c.getpagecount();
//获取pagenow
String s=request.getParameter("pagenow");
//out.println(s);
if(s!=null)
{
pagenow=Integer.parseInt(s);
}
//out.println(pagecount);
//out.println(rowcount);
//调用arraylsit(usercl中的getconetnt方)获取内容并显示
ArrayList al=c.getcontent(pagenow);
%>
循环显示
<%out.println(al.size());
for(int i=0;i<al.size();i++)
{
userbean ub=(userbean)al.get(i);
%>
<tr>
<td><%=ub.getId()%></td>
<td><%=ub.getUsername() %></td>
<td><%=ub.getUserpwd() %></td>
<td><%=ub.getUseremail() %></td>
<td><%=ub.getUsergrade() %></td></tr>
<%} %>
</tbody></table><br>