import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
public class welcome extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/test";
String user = "root";
String passWord = "java";
out.println("<html>");
out.println("<body>");
out.println("<h1>");
out.println("<center>");
out.println("Welcome to my Web!");
out.println("</center>");
out.println("</h1>");
out.println("</body>");
out.println("</html>");
int pageSize = 3;
int pageNow = 1;
int rowCount = 0;
int pageCount = 0;
String sPageNow = request.getParameter("pageNowOk");
if(sPageNow != null) {
pageNow = Integer.parseInt(sPageNow);
}
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passWord);
ps = conn.prepareStatement("select count(*) from user");
rs = ps.executeQuery();
if(rs.next()) {
rowCount = rs.getInt(1);
}
if(rowCount % pageSize == 0) {
pageCount = rowCount / pageSize;
}
else {
pageCount = rowCount / pageSize + 1;
}
ps = conn.prepareStatement("select * from user limit " + pageSize * (pageNow - 1) + ", " + pageSize);
rs = ps.executeQuery();
out.println("<html>");
out.println("<body>");
out.println("<center>");
out.println("<img src = imgs/1.jpg>");
out.println("<table border = 1>");
out.println("<tr><th>ID</th><th>NAME</th><th>PW</th><th>UNIT</th><th>ROLE</th></tr>");
while(rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getInt(1) +"</td>");
out.println("<td>" + rs.getString(2) +"</td>");
out.println("<td>" + rs.getString(3) +"</td>");
out.println("<td>" + rs.getInt(4) +"</td>");
out.println("<td>" + rs.getString(5) +"</td>");
out.println("<tr>");
}
out.println("</table>");
if(pageNow != 1) {
out.println("<a href=wel?pageNowOk=" + (pageNow - 1) + ">上一页</a>");
}
for(int i = 1; i <= pageCount; i++) {
if(pageNow != i) {
out.println("<a href=wel?pageNowOk=" + i + ">" + i + "</a>");
}
else {
out.println("<a>" + i + "</a>");
}
}
if(pageNow != pageCount) {
out.println("<a href=wel?pageNowOk=" + (pageNow + 1) + ">下一页</a>");
}
out.println("</center>");
out.println("</body>");
out.println("<body background = imgs/2.jpg>");
out.println("</html>");
}catch(Exception ex) {
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
this.doGet(request, response);
}
}
转载于:https://my.oschina.net/are1OfBlog/blog/181862