String condition="select*from employee1 where"+ziduan+"Like'%"+keyWord+"%'"; 这句有问题改为: String condition="select * from employee1 where "+ziduan+" like '%"+keyWord+"%'";原因:where "+ziduan+" like '%"+keyWord+"%'";之间少了空格,导致sql语句连起来了。。报错了
既然提示语法错误,那就先看看没变量的写法有没报错。 String condition="select * from employee1 where number Like '%1%'"; 如果没报错,就加回一个变量 String condition="select * from employee1 where "+ziduan+" Like '%1%'"; 如果报错,就debug看ziduan是否等于""……不会debug就找身边的人问…… 或者再笨点 System.out.ptintln("ziduan: [" + ziduan + "]"); System.out.ptintln("keyWord: [" + keyWord + "]"); 运行看看终端输出什么。
String condition="select*from employee1 where"+ziduan+"Like'%"+keyWord+"%'";
这是我的sql 连接 不知道是不是这出的问题
import java.sql.*;
public class KeyWordQuery {
String ziduan="",keyWord="";
StringBuffer queryResult;
public KeyWordQuery(){
queryResult=new StringBuffer();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(Exception e){}
}
public void setKeyWord(String s){
keyWord=s.trim();
try{
byte bb[]=keyWord.getBytes("ISO-8859-1");
keyWord=new String(bb,"GB2312");
}
catch(Exception e){}
}
public String getKeyWord(){
return keyWord;
}
public void setZiduan(String s){
ziduan=s.trim();
try{
byte bb[]=ziduan.getBytes("ISO-8859-1");
ziduan=new String(bb,"GB2312");
}
catch(Exception e){}
}
public String getZiduan(){
return ziduan;
}
public StringBuffer getQueryResult(){
String condition="select*from employee1 where"+ziduan+"Like'%"+keyWord+"%'";
StringBuffer str=f(condition);
return str;
}
public StringBuffer f(String condition){
StringBuffer str=new StringBuffer();
Connection con;
Statement sql;
ResultSet rs;
try{
String
uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=factory";
con=DriverManager.getConnection(uri,"sa","123321");
sql=con.createStatement();
rs=sql.executeQuery(condition);
str.append("<table border=1>");
str.append("<th width=80>"+"雇员号");
str.append("<th width=80>"+"姓名");
str.append("<th width=80>"+"出生日期");
str.append("<th width=80>"+"薪水");
while(rs.next()){
str.append("<tr>");
str.append("<td>"+rs.getString(1)+"</td>");
str.append("<td>"+rs.getString(2)+"</td>");
str.append("<td>"+rs.getDate(3)+"</td>");
str.append("<td>"+rs.getString(4)+"</td>");
str.append("</tr>");
}
str.append("<table border=1>");
con.close();
}
catch(SQLException e){
System.out.println(e);
}
return str;
}}
这句有问题改为:
String condition="select * from employee1 where "+ziduan+" like '%"+keyWord+"%'";原因:where "+ziduan+" like '%"+keyWord+"%'";之间少了空格,导致sql语句连起来了。。报错了
按你的改了
但出现:com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'like' 附近有语法错误。
public StringBuffer getQueryResult(){
String condition="select*from employee1 where"+ziduan+"Like'%"+keyWord+"%'";
StringBuffer str=f(condition);
return str;
}那是因为上句执行时,你的两个参数ziduan和keyword根本没有赋值吧?所以就出现问题了。。你debug调试一下,看看。我怀疑ziduan="",keyword=""。
<%@ page import="database.operation.*" %>
<jsp:useBean id="base" class="database.operation.KeyWordQuery" scope="request"></jsp:useBean>
<jsp:setProperty property="ziduan" name="base" param="ziduan"/>
<jsp:setProperty property="keyWord" name="base" param="keyWord"/>
<html><body bgcolor=pink><font size=2>
<form action="" method=post name=form>
选择:<select name="ziduan">
<option value="name">姓名
<option value="number">雇员号
</select>
含有:<input type="text" name="keyWord">
<input type=submit name="g" value="提交">
</form>
<jsp:getProperty property="ziduan" name="base"/>含有关键字
<jsp:getProperty property="keyWord" name="base"/>的记录:
<br><jsp:getProperty property="queryResult" name="base"/>
</font>
</body>
</html>这是我的jsp代码
keyWord=s.trim();
try{
byte bb[]=keyWord.getBytes("ISO-8859-1");
keyWord=new String(bb,"GB2312");
}
catch(Exception e){}
}
public String getKeyWord(){
return keyWord;
}
public void setZiduan(String s){
ziduan=s.trim();
try{
byte bb[]=ziduan.getBytes("ISO-8859-1");
ziduan=new String(bb,"GB2312");
}
catch(Exception e){}
}你这两个类是转换中文乱码的所以你可以先用String zd=request.getParameter("ziduan")或者String kw=request.getParameter("keyWord");
然后设置一下:setZiduan(zd);setKeyWord(kw);看行不? 你debug看到错误根据错误改就行了
你提交到后台啊。。后台在action中才能用String zd=request.getParameter("ziduan")接收参数。。
你public class KeyWordQuery {}类根本不是strust的action类,当然是得不到参数的
action为空 是页面本身的意思吧
多加点空格嘛
按你这样粘贴了但出现 了com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'Like' 附近有语法错误。
String condition="select * from employee1 where number Like '%1%'";
如果没报错,就加回一个变量
String condition="select * from employee1 where "+ziduan+" Like '%1%'";
如果报错,就debug看ziduan是否等于""……不会debug就找身边的人问……
或者再笨点
System.out.ptintln("ziduan: [" + ziduan + "]");
System.out.ptintln("keyWord: [" + keyWord + "]");
运行看看终端输出什么。