粘贴自己用
public List<Message> getMessagepart(int start,int size){
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
List<Message> lmDp=new ArrayList<Message>();
try {
conn=ds.getConnection();
String sql="select * from message_tbl limit ?,?";
st=conn.prepareStatement(sql);
st.setInt(1, start);
st.setInt(2, size);
rs=st.executeQuery();
while(rs.next())
{
Message message=new Message();
message.setId(rs.getInt(1));
message.setName(rs.getString(2));
message.setEmail(rs.getString(3));
message.setQq(rs.getString(4));
message.setTitle(rs.getString(5));
message.setContent(rs.getString(6));
message.setTime(rs.getString(7));
message.setIp(rs.getString(8));
lmDp.add(message);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lmDp;
} --------------------编程问答-------------------- <%@page import="com.niit.j2ee.guestbook.Message"%>
<%@page import="java.util.List"%>
<%@page import="com.niit.j2ee.guestbook.MessageDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@include file="header.jsp" %>
<%
String p=request.getParameter("page");
if(p == null||p=="")
{
p="0";
}
List<Message> lm=dbmessage.getMessage();
int count=lm.size();
int size=10;
int pageCount=count/size;
if(Integer.parseInt(p)<0||Integer.parseInt(p)>pageCount)
{
p="0";
}
int start=Integer.parseInt(p)*size;
List<Message> lmDp=dbmessage.getMessagepart(start,size);
if((count%size)>0)
{
pageCount++;
}
if(lmDp!=null)
{
session.setAttribute("Count",count);
%>
共有${sessionScope.Count}记录
<%
for(int i=0;i<pageCount;i++)
{
%>
第<a href="getmessagelist.jsp?page=<%=i %>"><%=i+1 %></a>页
<%
}
for(Message m:lmDp)
{
//显示留言信息
%>
<div style="border:Ipx solid black; margin-bottom:20px">
<div>标题:<a href="getMessageInfo.jsp?id=<%=m.getId()%>"><%=m.getTitle() %></a></div>
<div>内容:<%=m.getContent() %></div>
<label>作者:<%=m.getName()%></label>
</div>
<%
}
}
%>
</body>
</html> --------------------编程问答-------------------- import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
public class MessageDBBean {
private DataSource ds;
public MessageDBBean() throws NamingException {
// TODO Auto-generated constructor stub
Context ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java://comp/env/guestbook");
}
public Connection getConnection() throws SQLException{
return ds.getConnection();
}
public void close(Connection conn,Statement st,ResultSet rs)
{
try {
if(rs!=null)
{
rs.close();
}
if(st!=null)
{
st.close();
}
if(conn!=null)
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean addMessage(Message message)
{
if(message==null)
{
return false;
}
Connection conn=null;
PreparedStatement pst=null;
try {
conn=getConnection();
String sql="insert into message_tbl(name,email,qq,title,content,time,ip) values (?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, message.getName());
pst.setString(2, message.getEmail());
pst.setString(3, message.getQq());
pst.setString(4, message.getTitle());
pst.setString(5, message.getContent());
pst.setString(6, message.getTime());
pst.setString(7, message.getIp());
int result=pst.executeUpdate();
if(result>0)
{
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
close(conn, pst, null);
}
return false;
}
public List<Message> getMessage()
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
List<Message> lm=new ArrayList<Message>();
try {
conn=ds.getConnection();
st=conn.createStatement();
String sql="select * from message_tbl";
rs=st.executeQuery(sql);
while(rs.next())
{
Message message=new Message();
message.setId(rs.getInt(1));
message.setName(rs.getString(2));
message.setEmail(rs.getString(3));
message.setQq(rs.getString(4));
message.setTitle(rs.getString(5));
message.setContent(rs.getString(6));
message.setTime(rs.getString(7));
message.setIp(rs.getString(8));
lm.add(message);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lm;
}
}
--------------------编程问答-------------------- 不优雅 --------------------编程问答-------------------- 粘贴自己用 --------------------编程问答-------------------- 自己用!
补充:Java , 非技术区