//未封装前的servlet类
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
req.setCharacterEncoding("utf-8");
String command = req.getParameter("command");
String description = req.getParameter("description");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/micro_message?characterEncoding=utf-8", "root",
"root");
StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE where 1=1");
List<String> paramList = new ArrayList<String>();
if (command != null && !"".equals(command.trim())) {
sql.append(" and COMMAND=?");
paramList.add(command);
}
if (description != null && !"".equals(description.trim())) {
sql.append(" and DESCRIPTION like '%' ? '%'");
paramList.add(description);
}
PreparedStatement statement = connection.prepareStatement(sql.toString());
for (int i = 0; i < paramList.size(); i++) {
statement.setString(i + 1, paramList.get(i));
}
ResultSet resultSet = statement.executeQuery();
List<Message> messageList =new ArrayList<Message>();
while (resultSet.next()) {
Message message = new Message();
messageList.add(message);
message.setId(resultSet.getInt("ID"));
message.setCommand(resultSet.getString("COMMAND"));
message.setDescription(resultSet.getString("DESCRIPTION"));
message.setContent(resultSet.getString("CONTENT"));
}
req.setAttribute("messageList", messageList);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp);
}
这样查询有结果出现
//封装后的servlt类
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
req.setCharacterEncoding("utf-8");
String command = req.getParameter("command");
String description = req.getParameter("description");
// Class.forName("com.mysql.jdbc.Driver");
// Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/micro_message?characterEncoding=utf-8", "root",
// "root");
Connection connection=DBUtil.getConnection();
StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE where 1=1");
List<String> paramList = new ArrayList<String>();
if (command != null && !"".equals(command.trim())) {
sql.append(" and COMMAND=?");
paramList.add(command);
}
if (description != null && !"".equals(description.trim())) {
sql.append(" and DESCRIPTION like '%' ? '%'");
paramList.add(description);
}
PreparedStatement statement = connection.prepareStatement(sql.toString());
for (int i = 0; i < paramList.size(); i++) {
statement.setString(i + 1, paramList.get(i));
}
ResultSet resultSet = statement.executeQuery();
List<Message> messageList =new ArrayList<Message>();
while (resultSet.next()) {
Message message = new Message();
messageList.add(message);
message.setId(resultSet.getInt("ID"));
message.setCommand(resultSet.getString("COMMAND"));
message.setDescription(resultSet.getString("DESCRIPTION"));
message.setContent(resultSet.getString("CONTENT"));
}
req.setAttribute("messageList", messageList);
} catch (SQLException e) {
e.printStackTrace();
}
req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp);
}
//DBUtil类
public class DBUtil {
private static final String URL="jdbc:mysql://127.0.0.1:3306/micro_message?useUnicode=true&charcterEncoding=utf-8";
private static final String USER="root";
private static final String PASSWORD="root";
private static Connection conn=null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
public static void main(String[] args) {
if(conn!=null){
System.out.println("success");
}
}
}
这样查询没有结果