jsp中sql模糊查询为什么不行?
代码如下:<%@pagecontentType="text/html;charset=gb2312"%><%@pagelanguage="java"import="jav...
代码如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.sql.*" %>
<%!
String sql="";
String name="";
String key="";
String type="";
%>
<%
request.setCharacterEncoding("gb2312");
sql="SELECT * FROM manage";
key=request.getParameter("m_key");
type=request.getParameter("m_type");
%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
out.print(e);
}try{
String strDirPath=application.getRealPath(request.getRequestURI());
strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\";
strDirPath=application.getRealPath("gyn/ODBC/gongyu.mdb");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath;
%>
<html>
<body>
<form action="Test1.jsp" method="post">
查询关键字:<input type="text" name="m_key" width="75">
请选择查找操作字段:
<select name="m_type">
<option value="name">姓名</option>
<option value="age">年龄</option>
<option value="sex">性别</option>
<option value="xueli">学历</option>
<option value="phone">电话号码</option>
<option value="email">电子邮箱</option>
</select>
<input type="submit" value="查看">
</form></div>
<table width="100%" cellpadding="0" cellspacing="0" bordercolordark="#0033FF" bordercolorlight="#0033FF">
<!--DWLayoutTable-->
<tr>
<td height="31" colspan="6" valign="top"><div align="left">工作管理人员:</div></td>
</tr>
<tr>
<td width="75.12" height="31" valign="top">姓名</td>
<td width="75.12" valign="top">年龄</td>
<td width="75.12" valign="top">性别</td>
<td width="75.12" valign="top">学历</td>
<td width="75.12" valign="top">电话号码</td>
<td width="75.12" valign="top">电子邮箱</td>
</tr>
<%
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
if(key!=null&&key.equals("")&&type!=null&&type.equals(""))
sql += "where" +type+ "like'%"+key+"%'";
ResultSet rs = stmt.executeQuery(sql);
String name="";
int age=0;
String sex="";
String xueli="";
int phone=0;
String email="";
while(rs.next()){
name=rs.getString("name");
age=rs.getInt("age");
sex=rs.getString("sex");
xueli=rs.getString("xueli");
phone=rs.getInt("phone");
email=rs.getString("email");
%>
<tr>
<td height="31" valign="top"><%=name%></td>
<td valign="top"><%=age%></td>
<td valign="top"><%=sex%></td>
<td valign="top"><%=xueli%></td>
<td valign="top"><%=phone%></td>
<td valign="top"><%=email%></td>
</tr>
<%}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex)
{
out.print(ex);
}
%></table>
</body>
</html> 展开
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.sql.*" %>
<%!
String sql="";
String name="";
String key="";
String type="";
%>
<%
request.setCharacterEncoding("gb2312");
sql="SELECT * FROM manage";
key=request.getParameter("m_key");
type=request.getParameter("m_type");
%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
out.print(e);
}try{
String strDirPath=application.getRealPath(request.getRequestURI());
strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\";
strDirPath=application.getRealPath("gyn/ODBC/gongyu.mdb");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath;
%>
<html>
<body>
<form action="Test1.jsp" method="post">
查询关键字:<input type="text" name="m_key" width="75">
请选择查找操作字段:
<select name="m_type">
<option value="name">姓名</option>
<option value="age">年龄</option>
<option value="sex">性别</option>
<option value="xueli">学历</option>
<option value="phone">电话号码</option>
<option value="email">电子邮箱</option>
</select>
<input type="submit" value="查看">
</form></div>
<table width="100%" cellpadding="0" cellspacing="0" bordercolordark="#0033FF" bordercolorlight="#0033FF">
<!--DWLayoutTable-->
<tr>
<td height="31" colspan="6" valign="top"><div align="left">工作管理人员:</div></td>
</tr>
<tr>
<td width="75.12" height="31" valign="top">姓名</td>
<td width="75.12" valign="top">年龄</td>
<td width="75.12" valign="top">性别</td>
<td width="75.12" valign="top">学历</td>
<td width="75.12" valign="top">电话号码</td>
<td width="75.12" valign="top">电子邮箱</td>
</tr>
<%
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
if(key!=null&&key.equals("")&&type!=null&&type.equals(""))
sql += "where" +type+ "like'%"+key+"%'";
ResultSet rs = stmt.executeQuery(sql);
String name="";
int age=0;
String sex="";
String xueli="";
int phone=0;
String email="";
while(rs.next()){
name=rs.getString("name");
age=rs.getInt("age");
sex=rs.getString("sex");
xueli=rs.getString("xueli");
phone=rs.getInt("phone");
email=rs.getString("email");
%>
<tr>
<td height="31" valign="top"><%=name%></td>
<td valign="top"><%=age%></td>
<td valign="top"><%=sex%></td>
<td valign="top"><%=xueli%></td>
<td valign="top"><%=phone%></td>
<td valign="top"><%=email%></td>
</tr>
<%}
rs.close();
stmt.close();
conn.close();
}
catch(Exception ex)
{
out.print(ex);
}
%></table>
</body>
</html> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询