81,090
社区成员
发帖
与我相关
我的任务
分享
Statement stmt = (Statement)application.getAttribute("stmt");//从ServletContext对象获得stmt
Class.forName("org.postgresql.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/。。。","。。。。","。。。。");
Statement stmt = conn.createStatement();
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.sql.*" %>
<html>
<head><title>杨笑天官网</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
.textarea{font-size:12px; height:20px;}
.STYLE1 {font-size: 12px}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
-->
</style>
<script language="javascript">
function checkForm()
{
if(words.words.value=="")
{
alert("留言内容不能为空");
words.words.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<p><img src="images/top.gif" > </p>
<%
try{
Class.forName("org.postgresql.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/filedun","此处保密","此处保密");
Statement stmt = conn.createStatement();
//Statement stmt = (Statement)application.getAttribute("stmt");
String name = (String)session.getAttribute("name");
ResultSet rs1 = (ResultSet) stmt.executeQuery("SELECT count(*) totalCount FROM wordsdetails");
rs1.next();
int count=rs1.getInt("totalCount");
int i,pages=0,n=1;
if(count>10 && count%10==0) pages=count/10; //如果是10的倍数
else if(count>10) pages=count/10+1;
String strPage = request.getParameter("page");
if(strPage!=null) n = Integer.parseInt(strPage);
if(name!=null){%>
<p class="STYLE1">你好,<%=name%></p>
<form name="words" method="post" action="insert.do" onSubmit="return checkForm();">
<label>
<input name="words" type="text" size="8" maxlength="200">
</label>
<label>
<input type="submit" name="Submit" value="留言">
</label>
</form>
<%}else{%>
<p class="STYLE1"><a href="register.jsp">注册</a>|<a href="login.jsp">登录</a> →<a href="http://stdlib.gicp.net">PC版</a></p>
<%}%>
<p class="STYLE1">
<%
if(n>1) {%>
<a href="./showPage.do?page=<%=n-1%> ">上页</a>
<%
}
for(i=1;i<=pages;i++)
{
if(n==i) {
%>
<%=i%>
<%
}else {
%>
<a href="./showPage.do?page=<%=i%> "><%=i%></a>
<%
}}
if(n<pages){%>
<a href="./showPage.do?page=<%=n+1%> ">下页</a>
<%}%>
</p>
<%
String query= "select * from wordsdetails order by speakdate DESC limit 10";
ResultSet rs=null;
rs = (ResultSet)request.getAttribute("rs");
if(rs==null)
rs = (ResultSet)stmt.executeQuery(query);
%>
<%while(rs.next()){
%>
<div style= "width:200px "><p class="STYLE1"><%=rs.getString("speakdate")%>【<%=rs.getString("name")%>】说:<%=rs.getString("words")%><%if(name!=null&&name.equals("管理员")){%><form action="deleteWords.do" method="post"><input type="hidden" name="ID" value=<%=rs.getString("ID")%>><input type="submit" value="删除"></form><%}%></p></div>
<p class="STYLE1"> <img src="images/fenge.gif" height="1" width="200"></p>
<%}}catch(Exception e){} %>
</body>
</html>
package com.servlets;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowPage extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int page = Integer.parseInt(request.getParameter("page"));
ServletContext context = request.getServletContext();
Statement stmt = (Statement) context.getAttribute("stmt");
ResultSet rs=null;
try{
String query = "SELECT * FROM wordsdetails ORDER BY speakdate DESC LIMIT 10 OFFSET " + (page-1)*10;
rs= (ResultSet) stmt.executeQuery(query);
request.setAttribute("rs",rs);
RequestDispatcher view = request.getRequestDispatcher("index.jsp");
view.forward(request,response);
}catch(SQLException e){}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}