大家好我是尋覓:
最近過年,不知道大家多得如何,硬件搞少了點,書看多了些,時間也就多了些,寫了些東西,就發上來,同大家一起探討。
下面是一個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
小尋 閱讀(3268)
評論(5) 編輯 收藏 所屬分類:
j2se/j2ee/j2me