Отправляю запрос на поиск в MySQL в следующем коде:
public void findPosts(String search, int page, HashMap<Long, Post> map){
try{
openConnections();
resultSet = statement.executeQuery("select p.id, p.title, p.subtitle, p.image, p.views, p.date from posts p" +
" order by p.date" +
" where p.title like '%"+search.trim().replace(" ", "%%")+"%'" +
" limit " + (page - 1) * 5 + ", 5");
while (resultSet.next()){
generatePost(map, resultSet);
}
ResultSet res = statement.executeQuery("select count(id) from posts where id > "+lastRow +
" and title like '%"+search.trim().replace(" ", "%%")+"%'");
res.next();
this.notDefaultRows = (page-1) * 5 + res.getInt(1);
}catch (SQLException e){
e.printStackTrace();
}finally {
closeConnections();
}
}
При работе кода выдается следующая ошибка MySQL Syntax:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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
'where p.title like '%а%' limit 0, 5' at line 1
Что тут не так, я не понимаю, вроде всё должно работать?
Поменяйте порядок утверждений
resultSet = statement.executeQuery("select p.id, p.title, p.subtitle, p.image, p.views, p.date from posts p" +
" where p.title like '%"+search.trim().replace(" ",
" order by p.date" +
"%%")+"%'" +
" limit " + (page - 1) * 5 + ", 5");
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости