问题是这样的:
我写了三个文件,一个用来提交表单,一个是下面的tag文件,用来和oracle连接,查询表Books_table中的记录,第三个用来显示ResultSet类对象中的内容,我已经用sql语句按照同样的条件查过数据库里的表了,是有记录的,可把它放到JSP中就一直没有记录,rs.next()一直是false,while循环也进不去,我仔细检查过,连接的语句没有错啊,到底是什么原因呢?———————————————inquire.tag如下———————————————————————————
<%@ tag pageEncoding="gb2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="keyWord" required="true" %>
<%@ attribute name="ziduan" required="true" %>
<%@ variable name-given="foundResult" scope="AT_END" %>
<% String condition="select title, author, publish, price, ClassNo, State from Books_table where "+ziduan+" like '%"+keyWord+"%'";
StringBuffer result = new StringBuffer();
try{
Driver Driverdbtest = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
catch(ClassNotFoundException e){}
try{
result.append("<table border=1>");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","library","library");
result.append("<tr>");
result.append("<td>书名</td>");
result.append("<td>作者</td>");
result.append("<td>出版社</td>");
result.append("<td>价格</td>");
result.append("<td>索取号</td>");
result.append("<td>状态</td>");
result.append("</tr>");
PreparedStatement sql = con.prepareStatement(condition);
ResultSet rs = sql.executeQuery();
int col_cnt = 6;
while(rs.next())
{
result.append("<tr>");
for(int k=1; k<=col_cnt; k++)
{
result.append("<td>"+rs.getString(k)+"</td>");
}
result.append("</tr>");
}
if(rs.getRow()==0 && rs.next()==false)
result.append("<tr>没有找到符合条件的图书记录<tr>");
result.append("</table>");
rs.close();
sql.close();
con.close();
}
catch(SQLException e)
{
result.append(""+e);
}
jspContext.setAttribute("foundResult",new String(result));
%>
我写了三个文件,一个用来提交表单,一个是下面的tag文件,用来和oracle连接,查询表Books_table中的记录,第三个用来显示ResultSet类对象中的内容,我已经用sql语句按照同样的条件查过数据库里的表了,是有记录的,可把它放到JSP中就一直没有记录,rs.next()一直是false,while循环也进不去,我仔细检查过,连接的语句没有错啊,到底是什么原因呢?———————————————inquire.tag如下———————————————————————————
<%@ tag pageEncoding="gb2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="keyWord" required="true" %>
<%@ attribute name="ziduan" required="true" %>
<%@ variable name-given="foundResult" scope="AT_END" %>
<% String condition="select title, author, publish, price, ClassNo, State from Books_table where "+ziduan+" like '%"+keyWord+"%'";
StringBuffer result = new StringBuffer();
try{
Driver Driverdbtest = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
catch(ClassNotFoundException e){}
try{
result.append("<table border=1>");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","library","library");
result.append("<tr>");
result.append("<td>书名</td>");
result.append("<td>作者</td>");
result.append("<td>出版社</td>");
result.append("<td>价格</td>");
result.append("<td>索取号</td>");
result.append("<td>状态</td>");
result.append("</tr>");
PreparedStatement sql = con.prepareStatement(condition);
ResultSet rs = sql.executeQuery();
int col_cnt = 6;
while(rs.next())
{
result.append("<tr>");
for(int k=1; k<=col_cnt; k++)
{
result.append("<td>"+rs.getString(k)+"</td>");
}
result.append("</tr>");
}
if(rs.getRow()==0 && rs.next()==false)
result.append("<tr>没有找到符合条件的图书记录<tr>");
result.append("</table>");
rs.close();
sql.close();
con.close();
}
catch(SQLException e)
{
result.append(""+e);
}
jspContext.setAttribute("foundResult",new String(result));
%>
-----------------------inquire.jsp-----------------
<%@ page contentType="text/html; charset=gb2312" %>
<html><body><font size="2">
<form id="form1" name="form1" method="post" action="showInquire.jsp">
<select name="ziduan">
<option value="title" />书名
<option value="author" />作者
<option value="publish" />出版社
<option value="publishTime" />出版时间
<option value="classNo" />分类号
<option value="ISBN" />ISBN
</select>
<input name="keyword" type="text" />
<input type="submit" name="Submit" value="查询" />
<input type="reset" name="Submit2" value="重置" />
</form>
</font></body></html>-------------showInquire.jsp-----------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>查询结果</title>
</head><body><font size="2">
<% String ziduan = request.getParameter("ziduan");
String keyWord = request.getParameter("keyWord");
if(ziduan == null || keyWord == null)
{
ziduan = "title";
keyWord = "keyWord";
}
byte bb[] = keyWord.getBytes("ISO-8859-1");
keyWord = new String(bb);
%>
<inquire:doInquire ziduan="<%=ziduan%>" keyWord="<%keyWord%>" />
查询结果如下:
<br /><%=foundResult%>
</body>
</html>
ResultSet rs = sql.executeQuery();
这里有问题吧,executeQuery()是要接受一个sql的参数。
而你却放在了prepareStatement中,再说了,你也没必要用prepareStatement呀!
在使用占位符?的时候才有必要用它呀!
你看的试下,把condition传入executeQuery()方法中!
万一你的条件是一些特殊字符如',?,%等等,sql就异常了