做一个查询数据库的功能,表tellist中分别有name,sex,age,tel,mail,adr字段,查询页面有一个下拉框,是按哪个字段查询,一个表单输入查询内容,代码如下:
public String Query(HttpServletRequest request){
String html="";
String sql="select * from tellist";
String type=request.getParameter("type");
String content=request.getParameter("content");
if(type.equals("name"))
sql+="where name=?";
if(type.equals("sex"))
sql+="where sex=?";
if(type.equals("age"))
sql+="where age=?";
if(type.equals("tel"))
sql+="where tel=?";
if(type.equals("mail"))
sql+="where mail=?";
if(type.equals("adr"))
sql+="where adr=?";
try{
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,content);
ResultSet rs1=pstmt.executeQuery(sql);
html+="
姓名 | 性别 | 年龄 | 电话 | 邮箱 | 住址 |
"; html+=rs1.getString("name"); html+=" | "; html+=rs1.getString("sex"); html+=" | "; html+=rs1.getString("age"); html+=" | "; html+=rs1.getString("tel"); html+=" | "; html+=rs1.getString("mail"); html+=" | "; html+=rs1.getString("adr"); html+=" |
}
总报错,如下:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=?' at line 1
;但是我没发现我的SQL语句有错啊,求大神解答!
没学过Java,不过你这报错是 =?这个?是什么鬼,Java的写法吗
String sql="select * from tellist"; 这个语句,最后加一个空格:String sql="select * from tellist ";
where 条件前面需要加空格
这个写法 有点小看不懂啊
输出一下SQL语句 看拼接的时候是不是连在一起了 第一个SQL语句后要加个空格