高手帮我看看这段代码错在哪了,该怎么改?想了一整天了,郁闷阿
这个JSP页面是做多条件查询的,包含翻页。我在加了下面两段内容,以实现多条件查询后,翻页功能就出问题了,只能显示第一页,按下一页就显示异常页面了!请大家帮忙看看
------------------------------------------------------------------
//加了这段翻页就有问题了
strSQL = " where 1=1 ";
if(id.length()!=0){
strSQL = strSQL + "and id = '"+id+"' ";
}
if(name.length()!=0){
strSQL = strSQL + "and name = '"+name+"' ";
}
if(joindate.length()!=0){
strSQL = strSQL + "and joindate = '"+joindate+"' ";
}
if(dep.length()!=0){
strSQL = strSQL + "and dep = '"+dep+"' ";
}
strSQL1= strSQL1+strSQL;
---------------------------------------------------------------------
-------------------------------------------------
//还有这句
strSQL2 = strSQL2 + strSQL;
-------------------------------------------------
源代码:
<%@ page contentType="text/html; charset=Shift_JIS"%>
<%request.setCharacterEncoding("Shift_JIS");%>
<%@ page import="java.sql.*"%>
<%
Connection sqlCon; //数据??接?象
Statement sqlStmt; //SQL?句?象
ResultSet rs; //?果集?象
String strCon; //数据??接字符串
String strSQL1,strSQL2,strSQL; //SQL?句
String id,name,joindate,dep;
id = request.getParameter("mg_id");
name = request.getParameter("mg_name");
joindate = request.getParameter("mg_joindate");
dep = request.getParameter("department");
int intPageSize; //一??示的??数
int intRowCount; //???数
int intPageCount; //??数
int intPage; //待?示??
java.lang.String strPage;
int i,j,k; //?置一??示的??数
intPageSize = 20; //取得待?示??
strPage = request.getParameter("page");
if(strPage==null){
//表明在QueryString中没有page?一个参数,此??示第一?数据
intPage = 1;
}
else{
//将字符串??成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1;
}
//装?JDBC-ODBC??程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//?置数据??接字符串
strCon = "jdbc:odbc:test";
//?接数据?
sqlCon =DriverManager.getConnection(strCon);
//?建SQL?句?象
sqlStmt = sqlCon.createStatement();
//?取???数
strSQL1 = "select count(*) from test ";//后?空格一定要
------------------------------------------------------------------
//加了这段翻页就有问题了
strSQL = " where 1=1 ";
if(id.length()!=0){
strSQL = strSQL + "and id = '"+id+"' ";
}
if(name.length()!=0){
strSQL = strSQL + "and name = '"+name+"' ";
}
if(joindate.length()!=0){
strSQL = strSQL + "and joindate = '"+joindate+"' ";
}
if(dep.length()!=0){
strSQL = strSQL + "and dep = '"+dep+"' ";
}
strSQL1= strSQL1+strSQL;
---------------------------------------------------------------------
rs = sqlStmt.executeQuery(strSQL1);
//?行SQL?句并取得?果集
rs.next(); //??集?打?的?候,指?位于第一条??之前
intRowCount = rs.getInt(1);
rs.close(); //???果集
//?算??数
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//?整待?示的??if(intPage>intPageCount) intPage = intPageCount;
//?置?取数据SQL?句
strSQL2 = "select * from test ";
-------------------------------------------------
//这句也是
strSQL2 = strSQL2 + strSQL;
-------------------------------------------------
//?行SQL?句并取得?果集
rs = sqlStmt.executeQuery(strSQL2);
//将??指?定位到待?示?的第一条??上
i = (intPage-1) * intPageSize;
k = i+1;
for(j=0;j<i;j++) rs.next();
%>
<html>
<head>
<title>社員情報一覽</title>
<link rel="stylesheet" href="WUB_styleL.css" type="text/css">
</head>
<body>
<div align="center">
<font size="5"> 社員情報一覽 </font>
</div><p>
<table width="100%" class="pDspMainTable" align="center">
<tr class="pDspLabel2">
<td width="3%" align="center"></td>
<td width="10%" align="center">社員コード</td>
<td width="10%" align="center">社員名</td>
<td width="10%" align="center">入社日</td>
<td width="8%" align="center">部門</td>
<td width="10%" align="center">誕生日</td>
<td width="49%" align="center">その他</td>
</tr>
</table>
<div style="overflow:auto;height:305;">
<table width="100%" class="pDspMainTable" align="center">
<%
//?示数据
i = 0;
while(i<intPageSize && rs.next()){ %>
<tr <%if ((k%2)==0 ) { %> class="pRowEven2" <% } else { %> class="pRowOdd2" <% } %> >
<td width="3%" align="center"><%=k%></td>
<td width="10%" align="center"><%=rs.getString("id")%></td>
<td width="10%" align="center"><%=rs.getString("name")%></td>
<td width="10%" align="center"><%=rs.getString("joindate")%></td>
<td width="8%" align="center"><%=rs.getString("dep")%></td>
<td width="10%" align="center"><%=rs.getString("birthday")%></td>
<td width="48.5%" align="center"><%=rs.getString("beizhu")%></td>
</tr>
<%
i++;
k++;
}
%>
</table>
</div>
<table width=100%>
<tr>
<td width=10% align=left>
<%if(intPage>1){%>
<a href="test.jsp?page=<%=intPage-1%>">上一頁</a>
<% } %>
</td>
<td width=80% align="center">
第<%=intPage%>頁 共<%=intPageCount%>頁
</td>
<td align=right>
<%if(intPage<intPageCount){%>
<a href="test.jsp?page=<%=intPage+1%>">下一頁</a>
<% } %>
</td>
</tr>
</table>
</body>
</html>
<%
//???果集
rs.close();
//??SQL?句?象
sqlStmt.close();
//??数据?
sqlCon.close();
%>
问题点数:20、回复次数:4Top
1 楼chaucer518(我爱我家的思思)回复于 2005-08-02 22:20:07 得分 20
首先,你这样处理不妥当,逻辑处理应该封装到BEAN里面处理在这里顺便贴上我的办法参考吧,呵呵,希望对你有用,今晚喝多了点,暂时就不帮你看代码了:)
import java.util.*;
import java.sql.*;
import upload.*;
public class databaseBean{
private String dbName;
private String dbUser;
private String dbPass;
private Vector memoVector;
Connection connection;
public databaseBean(){
dbName=new String("com.microsoft.jdbc.sqlserver.SQLServerDriver");
dbUser=new String("bn");
dbPass=new String("bn");
String connectionUrl="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connection=DriverManager.getConnection(connectionUrl,dbUser,dbPass);
}
catch(Exception e){
System.out.println(e.toString());
}
}
//select * from resource where ResourceName LIKE'%计算机%'
public void dbQueryMemos(String name){
String sql=new String ("select * from resource");
if(name!=null)
sql = sql+" where ResourceName Like'%"+name+"%'";
try{
memoVector=new Vector();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
memoBean temp=new memoBean();
temp.setResourceName(rs.getString(1));
temp.setResourceSize(rs.getString(2));
temp.setType(rs.getString(3));
temp.setUploadTime(rs.getDate(4));
temp.setTeacherName(rs.getString(5));
memoVector.add(temp);
}
}
catch(SQLException e){
System.out.println(e.toString());
}
}
public void setDbName(String s){
this.dbName=s;
}
public void setDbUser(String s){
this.dbUser=s;
}
public void setDbPass(String s){
this.dbPass=s;
}
public String getDbName(){
return this.dbName;
}
public String getDbUser(){
return this.dbUser;
}
public String getDbPass(){
return this.dbPass;
}
public Vector getMemoVector(){
return this.memoVector;
}
}
Top
2 楼benbenjjf54()回复于 2005-08-03 06:07:32 得分 0
upTop
3 楼mylove160(小菜)回复于 2005-08-03 18:53:38 得分 0
谢谢chaucer518(喜欢凤晓的男生) ,刚开始学JSP,还没学BEAN呢,呵呵!今天早上又查了遍代码,终于找到了原因,原来是翻页时,id,name,dep,joindate变量没有值了!Top
4 楼hdkdly(黑暗人间)回复于 2005-08-03 19:13:18 得分 0
靠,给小日本作事情Top




