我用myeclipse做了个搜索栏,但发布之后,发现只输入关键字无法实现查询的功能,但是输入价格区间可得到结果,下面是我的代码:<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>My JSP 'ss.jsp' starting page</title>
</head>
<body>
<div align="center">
<form action="ss.jsp" method="post" style="padding:10px">
<fieldset>
<label>关键字<input type="text"name="goodsname"style="width:150px;" /></label>
<label>价格从<input type="text" name="price01" style="width:70"></label>
<label>到<input type="text" name="price02" style="width:70"></label>
<button type="submit" name="search" style="font-weight:bold;width:100px" />搜 索</button> </fieldset> </form>
</div>
<%Connection conn=null;
Statement stat=null;
ResultSet rs=null;
String goodsname=null;
String price01=null;
String price02=null;
goodsname=request.getParameter("goodsname");
price01=request.getParameter("price01");
price02=request.getParameter("price02");
String sql=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
stat=conn.createStatement();
if ((price01!=null)&&(goodsname!=null)){
sql="select * from goods where g_name like '%"+goodsname+"%' and g_price between '"+price01+"' and '"+price02+"'";
}
if(price01==null)
{sql="select * from goods where g_name like '%"+goodsname+"%' ";}
if(goodsname==null)
{sql="select * from goods where g_price between '"+price01+"' and '"+price02+"'";}
rs=stat.executeQuery(sql);%>
<table cellSpacing="0" width="80%" align="center" style="font-size:10pt;border:1pt">
<tr style="border:solid 1pt">
<td align="center" style="border:solid 1pt">商品名</td>
<td align="center" style="border:solid 1pt">价格</td>
<td align="center" style="border:solid 1pt">数量</td>
</tr>
<%
while(rs.next()){
out.print("<tr style='border:solid 1pt'>");
out.print("<td style='border:solid 1pt'>"+rs.getString("g_name")+"</td>");
out.print("<td style='border:solid 1pt'>"+rs.getDouble("g_price")+"</td>");
out.print("<td style='border:solid 1pt'>"+rs.getInt("g_number")+"</td>");
out.print("</tr>");
}
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
%>
</table>
</body>
</html>我建的数据库名称是goods,其中有g_name,g_price,g_number,3个属性,请问我这错在哪里,怎么解决?
<%@ page import="java.sql.*" %>
<html>
<head>
<title>My JSP 'ss.jsp' starting page</title>
</head>
<body>
<div align="center">
<form action="ss.jsp" method="post" style="padding:10px">
<fieldset>
<label>关键字<input type="text"name="goodsname"style="width:150px;" /></label>
<label>价格从<input type="text" name="price01" style="width:70"></label>
<label>到<input type="text" name="price02" style="width:70"></label>
<button type="submit" name="search" style="font-weight:bold;width:100px" />搜 索</button> </fieldset> </form>
</div>
<%Connection conn=null;
Statement stat=null;
ResultSet rs=null;
String goodsname=null;
String price01=null;
String price02=null;
goodsname=request.getParameter("goodsname");
price01=request.getParameter("price01");
price02=request.getParameter("price02");
String sql=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
stat=conn.createStatement();
if ((price01!=null)&&(goodsname!=null)){
sql="select * from goods where g_name like '%"+goodsname+"%' and g_price between '"+price01+"' and '"+price02+"'";
}
if(price01==null)
{sql="select * from goods where g_name like '%"+goodsname+"%' ";}
if(goodsname==null)
{sql="select * from goods where g_price between '"+price01+"' and '"+price02+"'";}
rs=stat.executeQuery(sql);%>
<table cellSpacing="0" width="80%" align="center" style="font-size:10pt;border:1pt">
<tr style="border:solid 1pt">
<td align="center" style="border:solid 1pt">商品名</td>
<td align="center" style="border:solid 1pt">价格</td>
<td align="center" style="border:solid 1pt">数量</td>
</tr>
<%
while(rs.next()){
out.print("<tr style='border:solid 1pt'>");
out.print("<td style='border:solid 1pt'>"+rs.getString("g_name")+"</td>");
out.print("<td style='border:solid 1pt'>"+rs.getDouble("g_price")+"</td>");
out.print("<td style='border:solid 1pt'>"+rs.getInt("g_number")+"</td>");
out.print("</tr>");
}
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
%>
</table>
</body>
</html>我建的数据库名称是goods,其中有g_name,g_price,g_number,3个属性,请问我这错在哪里,怎么解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货