第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

模糊查詢查不到東西怎么辦?

package?Servlet;

import?java.io.IOException;
import?java.io.PrintWriter;
import?java.sql.Connection;
import?java.sql.DriverManager;
import?java.sql.PreparedStatement;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.util.ArrayList;
import?java.util.List;

import?javax.servlet.ServletException;
import?javax.servlet.http.HttpServlet;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;

import?bean.Message;

@SuppressWarnings({?"serial",?"unused"?})
public?class?ListServlet?extends?HttpServlet?{

	private?static??String?url?=?"jdbc:MYSQL://127.0.0.1:3306/micro_message";
	private?static??String?user?=?"root";
	private?static??String?password?=?"123456";

	/**
	?*?Constructor?of?the?object.
	?*/
	public?ListServlet()?{
		super();
	}

	public?void?destroy()?{
		super.destroy();?//?Just?puts?"destroy"?string?in?log
		//?Put?your?code?here
	}

	
	public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
			throws?ServletException,?IOException?{
		try?{
			Class.forName("com.mysql.jdbc.Driver");
			Connection?conn?=?null;
			try?{
				?request.setCharacterEncoding("utf-8");
				?response.setCharacterEncoding("utf-8");
				?String?command=request.getParameter("command");
				?String?description=request.getParameter("description");
				?request.setAttribute("command",?command);
				?request.setAttribute("description",?description);
				?conn?=?DriverManager.getConnection(url,user,password);
				?StringBuilder?sql=new?StringBuilder("select?*?from?message?where?1=1");
				?List<String>?paramList=new?ArrayList<String>();
				?if(command!=null&&!"".equals(command.trim())){
					?sql.append("?and?command='"+command+"'?");
					?paramList.add(command);
				?}
				?if(description!=null&&!"".equals(description.trim())){
					?sql.append("?and?DESCRIPTION?like?'%'???'%'");
					?paramList.add(description);
				?}
				?PreparedStatement?statement=conn.prepareStatement(sql.toString());
				?for(int?i?=?1;i<paramList.size();i++){
					?statement.setString(i,?paramList.get(i));
				?}
				?ResultSet?rs=statement.executeQuery(sql.toString());
				?List<Message>?messageList=new?ArrayList<Message>();
				?while(rs.next()){
					?Message?message=new?Message();
					?message.setId(rs.getInt("id"));
					?message.setCommand(rs.getString("command"));
					?message.setContent(rs.getString("content"));
					?message.setDescription(rs.getString("description"));
					?messageList.add(message);
				?}
				?request.setAttribute("messageList",?messageList);
			}?catch?(SQLException?e)?{
				e.printStackTrace();
			}
			}?catch?(ClassNotFoundException?e)?{
				e.printStackTrace();
		}
		request.getRequestDispatcher("/WEB-INF/jsp/back/List.jsp").forward(request,?response);
	}

	public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
			throws?ServletException,?IOException?{
		this.doGet(request,?response);
	}

	public?void?init()?throws?ServletException?{
		//?Put?your?code?here
	}

}

報錯是?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

正在回答

3 回答

sql.append("?and?command='"+command+"'?");這一句寫錯了;

改成sql.append("?and?command= ? "); ?或者sql.append("?and?command="+command+" ");

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

二道橋十三太保 提問者

非常感謝!
2017-06-23 回復(fù) 有任何疑惑可以回復(fù)我~

控制for循環(huán)

1,如果存在description,不存在command,就循環(huán)一次,給參數(shù)賽值;

2,如果description,command都存在,循環(huán)一次,給第二個參數(shù)賽值;

3,如果command存在,description不存在,就不要循環(huán),不要進行賽值;

3,如果command,description都不存在,也不要進行參數(shù)賽值;

備注:這里可以不用循環(huán),可以直接取第幾個參數(shù),得到,并進行賽值;


0 回復(fù) 有任何疑惑可以回復(fù)我~

如果command為空,description為不為空,然后是不會進入for循環(huán),這時,就不會statement.setString(i,?paramList.get(i)); ? ?->然后就會報sql錯誤

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

二道橋十三太保 提問者

那要怎么處理呢?
2017-04-07 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
通過自動回復(fù)機器人學(xué)Mybatis---基礎(chǔ)版
  • 參與學(xué)習(xí)       107413    人
  • 解答問題       830    個

微信公眾號自動回復(fù)功能學(xué)習(xí)Mybatis,基礎(chǔ)教程加案例實戰(zhàn)方式學(xué)習(xí)

進入課程

模糊查詢查不到東西怎么辦?

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號