大家好我是寻觅:
最近过年,不知道大家多得如何,硬件搞少了点,书看多了些,时间也就多了些,写了些东西,就发上来,同大家一起探讨。
下面是一个servlet+bean分页的程序。
为什么第一个是servlet?
首先,必须清楚的是,不管是web还是桌面系统其原理都是非常之相近的。你可以看看数据库连接,其实代码是一样的,只是在数据源上可能有所不同,WEB平台上可以将数据管理委托我们的webserver替我们管理,当然在app程序中你也可以写一个数据管理的独立组件。同理,web平台上的分页,其实和APP十分之相似。目前分页有两种方法:第一,使用sql查询部分的数据,如在SQL语句中使用limit / between...and...;第二,通过控制ResultSet指针实现。二者比较,前者相对简单,但从节省资源和速度没有后者优秀。
其次,抛开一切的框架最快的web程序,莫过于servlet。
本人实在不喜欢说这些,感觉这些东西,代码写多了,自然会知道,所以,你会发现我的文章里很少写这些废话,偶尔写多了些,居然发现点击多了不少,呵呵,看来大家很少看书和写代码。这也是我喜欢发资源型文章的原因之一。希望大家,在新的一年,拥有新气象,多看书,多写代码,尽情感受学习和写代码的乐趣。
运行结果:
访问路径
http://127.0.0.1:8080/webTest/
ServletTest.lusm
控制台:
########只查询一次数据库哦#########
web界面:
代码:
web.xml
<?xml version="1.0" encoding="gbk"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>lusm.servlet.ServletTest</servlet-class>
<init-param>
<param-name>dbname</param-name>
<param-value>myTest</param-value>
</init-param>
<init-param>
<param-name>table</param-name>
<param-value>userinfo</param-value>
</init-param>
<init-param>
<param-name>dba</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>passwd</param-name>
<param-value>password</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/ServletTest.lusm</url-pattern>
</servlet-mapping>
</web-app>
ServletTest.java
package lusm.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lusm.db.inte.DbInf;
import lusm.db.inte.ViewInf;
import lusm.db.oa.Db;
import lusm.db.oa.View;
//前台显示
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 5435345395438959L;
private String dbname;
private String table;
private String dba;
private String passwd;
private String sql;
private DbInf di = null;
private ViewInf v = null;
private ResultSet rs = null;
public ServletTest() {
super();
}
public void destroy() {
super.destroy();
}
public void init(ServletConfig config) throws ServletException{
//**********************用户输入*******************************
dbname = config.getInitParameter("dbname");
table = config.getInitParameter("table");
dba = config.getInitParameter("dba");
passwd = config.getInitParameter("passwd");
sql = "select * from "+table;
di = new Db();
v = new View();
try {
rs = di.init(dbname,dba,passwd,sql).getRs();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("查询错误");
}
//**********************结束输入********************************
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
//**********************用户输入*******************************
//goto是分页的参数
int go ;
if(request.getParameter("goto") == null){
go = 1;
}
else
{
go = Integer.parseInt(request.getParameter("goto"));
}
try {
v.setView(rs, "table",go,5);//这里用于测试,实际中使用须避免像5这样的魔数
out.println(v.getView());
} catch (Exception e) {
e.printStackTrace();
out.println("打印失败");
}
//**********************结束输入********************************
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
ViewInf.java接口
package lusm.db.inte;
import java.sql.ResultSet;
import java.sql.SQLException;
public interface ViewInf {
String ERROR = "错误";
String NOSTYLE ="对不起! 没有该样式";
public void setView(ResultSet rs,String style,int go,int pagesize) throws SQLException ;
public String getView();
public int getRow(ResultSet rs)throws SQLException;
}
View.java
package lusm.db.oa;
import java.sql.ResultSet;
import java.sql.SQLException;
//分页代码
public class View implements lusm.db.inte.ViewInf {
private String view ;
public String getView() {
return view;
}
public void setView(ResultSet rs,String style,int go,int pagesize) throws SQLException {
if(style.equals("table")){
this.view = "<table border='2' bgcolor='#ff8000' width='240px' align='center'>";
int i = 0;
/**//*
* 注意:我们不能用
* rs.rs.absolute(0);
* 定位到第一条数据,这里我使用
* rs.first();
* rs.previous();
* 解决了这个问题
* */
if(go != 1)
rs.absolute(go-1);
else{
rs.first();
rs.previous();
}
while (rs.next()) {
if(++i <= pagesize){
this.view= this.view +"<tr>";
this.view= this.view +"<td>"+rs.getString(1) + "</td>" +
"<td>" + rs.getString(2)+"</td>";
this.view= this.view +"</tr>";
}else{
break;
}
}
//最后一页
if((go+pagesize) > this.getRow(rs)){
this.view = this.view +"<tr><td><a href ='/webTest/ServletTest.lusm?goto="+(go-pagesize)+"'>上一页</a></td><td>末 页</td></tr>";
}//第一页
else if(go == 1){
this.view = this.view +"<tr><td>首 页</td><td><a href ='/webTest/ServletTest.lusm?goto="+(go+pagesize)+"'>下一页</a></td></tr>";
}
else{
this.view = this.view +"<tr><td><a href ='/webTest/ServletTest.lusm?goto="+(go-pagesize)+"'>上一页</a></td><td><a href ='/webTest/ServletTest.lusm?goto="+(go+pagesize)+"'>下一页</a></td></tr>";
}
this.view= this.view +"</table>";
}else{
this.view = NOSTYLE;
}
}
public int getRow(ResultSet rs) throws SQLException {
ResultSet rsr = rs;
rsr.last();
return rsr.getRow();
}
}
DbInf.java接口
package lusm.db.inte;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import lusm.db.oa.Db;
public interface DbInf {
public Db init(String dbname,String dba,String passwd, String sql);
public Connection getConn() ;
public void setConn(String dbname,String dba,String passwd) throws Exception;
public ResultSet getRs() throws SQLException ;
public void setRs(String sql) throws SQLException ;
}
Db.java
package lusm.db.oa;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import lusm.db.inte.DbInf;
import com.mysql.jdbc.Connection;
//数据库
public class Db implements DbInf {
private Connection conn;
private ResultSet rs;
public Db init(String dbname,String dba,String passwd, String sql){
try {
this.setConn(dbname, dba, passwd);
System.out.println("########只查询一次数据库哦#########");
this.setRs(sql);
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
public Connection getConn() {
return conn;
}
public void setConn(String dbname,String dba,String passwd) throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
this.conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost" +
":3306/"+dbname+"?user="+dba+"&password="+passwd+"&useUnicode=true&characterEncoding=GBK");
}
public ResultSet getRs() throws SQLException {
return rs;
}
public void setRs(String sql) throws SQLException {
this.rs = this.getConn().createStatement().executeQuery(sql);
}
}
地震让大伙知道:居安思危,才是生存之道。
posted on 2008-02-08 00:39
小寻 阅读(3260)
评论(5) 编辑 收藏 所属分类:
j2se/j2ee/j2me