求登录和分页的方法和servlet的写法,有自己的Dao类,要根据提供的Dao类写的。
下面是提供的Dao类,菜鸟求登录和分页查询的方法和servlet的写法。在线等答案。
package cn.poul.dao;
import java.util.*;
import java.sql.*;
import javax.servlet.jsp.jstl.sql.*;
public class BaseDAO {
private Connection conn;
private String sql;
private List param;
/**
* 设定连接类
*/
public void setConnection(Connection conn) {
this.conn = conn;
}
/**
* 设定SQL语句.
*/
public void setSql(String sql) {
this.sql = sql;
}
/**
* 设定SQL语句的参数
*/
public void setParam(List param) {
this.param = param;
}
/**
* 执行查询
*
* @return a javax.servlet.jsp.jstl.sql.Result object
* @exception SQLException
*/
public Result executeQuery() throws SQLException {
Result result = null;
ResultSet rs = null;
PreparedStatement ps = null;
Statement stmt = null;
try {
conn=DbConn.getConn();
if (param != null && param.size() > 0) {
ps = conn.prepareStatement(sql);
setParam(ps, param);
rs = ps.executeQuery();
}
else {
// Use a regular Statement
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
result = ResultSupport.toResult(rs);
}
finally {
DbConn.closeAll(rs, ps, stmt, conn);
}
return result;
}
/**
* 执行Update语句
*
* @return the number of rows affected
* @exception SQLException
*/
public int executeUpdate() {
int noOfRows = 0;
ResultSet rs = null;
PreparedStatement ps = null;
Statement stm = null;
try {
conn=DbConn.getConn();
if (param != null && param.size() > 0) {
ps = conn.prepareStatement(sql);
setParam(ps, param);
noOfRows = ps.executeUpdate();
}
else {
// Use a regular Statement
stm = conn.createStatement();
noOfRows = stm.executeUpdate(sql);
}
}catch(Exception e){e.printStackTrace();}
finally {
DbConn.closeAll(rs, ps, stm, conn);
}
return noOfRows;
}
/**
* 设定语句的参数.
*
* @param ps the PreparedStatement
* @param param a List with objects
* @exception SQLException
*/
private void setParam(PreparedStatement ps, List param)
throws SQLException {
for (int i = 0; i < param.size(); i++) {
Object v = param.get(i);
// Set the value using the method corresponding to the type.
// Note! Set methods are indexed from 1, so we add 1 to i
ps.setObject(i + 1, v);
}
}
}