replace java loop with query in postgresql - java

I used postgresql 10 and spring boot
I try with success to load tree using loop in java.
in the loop I call each time the query
but it consumes cpu in application server and cpu in database server and takes time to load the tree which contain 5000 unit.
I want using only one query to load the tree without loop in java.
the result from java is ResponseEntity<List<UnitDTO>>
this is my code :
#GetMapping("/unitsBook")
#Timed
public ResponseEntity<List<UnitDTO>> getAllUnitsBook(Pageable pageable, #RequestParam(name="lang", required=false) String lang,
#RequestParam(name="emp", required=false) String empID) {
log.debug("REST request to get a page of Units");
Page<UnitDTO> page = unitService.findAllUnitBook(pageable, lang,empID);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/unitsBook");
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
the java code which contains the loop is :
public List<UnitDTO> getUnitBookList(Pageable pageable, String lang,String empID) {
List<UnitDTO> list=unitRepository.findUnitList(pageable, lang,empID);
List<UnitDTO> unitChildList=getChildrenUnitList(list,lang,pageable,empID);
return unitChildList;
}
private List<UnitDTO> getChildrenUnitList(
List<UnitDTO> unitList, String lang,Pageable pageable,String empID) {
for(UnitDTO UnitDTO : unitList) {
List<UnitDTO> childrenListEntity = unitRepository.findUnitByParentId(pageable, lang,UnitDTO.getValue(),empID);
UnitDTO.setChildren(getChildrenUnitList(childrenListEntity,lang,pageable,empID));
}
return unitList;
}
and the code which call query is :
public List<UnitDTO> findUnitList(Pageable pageable, String lang,String empID) {
String querystr = "SELECT ";
querystr += " unl.name AS text ,";
querystr += " un.id AS value ,";
querystr += " ,cast( 0 as varchar(10) ) as favoriteNbr,cast( null as varchar(10) ) as favoriteId ";
querystr += " FROM public.unit un ";
querystr += " LEFT OUTER JOIN public.unitlang unl ON unl.unit_id = un.id ";
querystr += " Where unl.lang = :lang parentid is null order by app_order asc";
log.debug("-- Query:" + querystr);
Query query = em.createNativeQuery(querystr, "UnitDTOMap");
query.setParameter("lang", lang);
List<UnitDTO> unitDTOs = query.getResultList();
if (pageable.isUnpaged()) {
return unitDTOs;
}
return unitDTOs;
}
#Override
public List<UnitDTO> findUnitByParentId(Pageable pageable, String lang, String idParent,String empID) {
log.debug("-- pageable:" + pageable.getPageNumber() + ", Size:" + pageable.getPageSize() + ", isUnpaged:" + pageable.isUnpaged() + ", lang:" + lang);
lang = lang.toUpperCase();
String querystr = "SELECT ";
querystr += " unl.name AS text ,";
querystr += " un.id AS value ,";
querystr += " (case when cast((select count(*) from employee where employee.unit_id = un.id) as varchar(10)) != '0' then cast(1 as Boolean) else cast(0 as BOOLEAN) end) as disabled";
querystr += " ,cast( 0 as varchar(10) ) as favoriteNbr,cast( null as varchar(10) ) as favoriteId ";
querystr += " FROM unit un ";
querystr += " LEFT OUTER JOIN unitlang unl ON unl.unit_id = un.id ";
querystr += " Where unl.lang = :lang and un.parentid = :idParent order by app_order asc ";
log.debug("-- Query:" + querystr);
Query query = em.createNativeQuery(querystr, "UnitBookDTOMap");
query.setParameter("lang", lang);
query.setParameter("idParent", idParent);
List<UnitDTO> unitDTOs = query.getResultList();
log.debug("-- unitDTOs Size:" + unitDTOs.size());
if (pageable.isUnpaged()) {
return unitDTOs;
}
return unitDTOs;
}
Updated :
I try to use the recursive query but the problem is that the tree does not display correctly.
all the unit are in same level.
I think the problem in this line WHERE id = :idParent I comment it because I didn't find how can I send it
public List<UnitDTO> getUnitBookList(Pageable pageable, String lang) {
List<UnitDTO> list=unitRepository.findUnitList(pageable, lang);
// List<UnitBookDTO> unitChildList=getChildrenUnitList(list,lang,pageable);
return list;
}
#Override
public List<UnitDTO> findUnitList(Pageable pageable, String lang) {
log.debug("-- pageable:" + pageable.getPageNumber() + ", Size:" + pageable.getPageSize() + ", isUnpaged:" + pageable.isUnpaged() + ", lang:" + lang);
lang = lang.toUpperCase();
String querystr = "WITH RECURSIVE un_id AS ( ";
querystr += " SELECT id";
querystr += " FROM unit ";
// querystr += " WHERE id = :idParent";
querystr += " UNION";
querystr += " SELECT unit.id";
querystr += " FROM unit JOIN un_id ON unit.parentid = un_id.id ) ";
querystr += " SELECT unl.name AS text, un.id AS value, (case when cast((select count(*) from employee where employee.unit_id = un.id) as varchar(10)) != '0' ";
querystr += " then cast(1 as Boolean) else cast(0 as BOOLEAN) end) as disabled , cast(0 as varchar(10)) as favoriteNbr,";
querystr += " cast(null as varchar(10)) as favoriteId FROM un_id JOIN unit un USING (id) LEFT OUTER JOIN unitlang unl ON unl.unit_id = un.id Where unl.lang = :lang order by app_order asc";
log.debug("-- Query:" + querystr);
Query query = em.createNativeQuery(querystr, "UnitDTOMap");
query.setParameter("lang", lang);
List<UnitDTO> unitDTOs = query.getResultList();
log.debug("-- unitDTOs Size:" + unitDTOs.size());
if (pageable.isUnpaged()) {
return unitDTOs;
}
return unitDTOs;
}

The usual way to fetch a tree in SQL is to use a recursive Common Table Expression (CTE):
WITH RECURSIVE un_id AS (
SELECT id
FROM unit
WHERE id = :idParent -- or parentid if you want to exclude the parent itself
UNION
SELECT unit.id
FROM unit
JOIN un_id
ON unit.parentid = un_id.id
)
SELECT unl.name AS text,
un.id AS value,
(case
when cast((select count(*) from employee where employee.unit_id = un.id) as varchar(10)) != '0'
then cast(1 as Boolean)
else cast(0 as BOOLEAN) end) as disabled
,
cast(0 as varchar(10)) as favoriteNbr,
cast(null as varchar(10)) as favoriteId
FROM un_id -- reference to the CTE
JOIN unit un
USING (id)
LEFT OUTER JOIN unitlang unl ON unl.unit_id = un.id
Where unl.lang = :lang
order by app_order asc
Recursively fetch all the IDs and then join the rest of the needed data.

Related

Use recursive parent child query traitement instead of java traitement

Usng java I can make a recursive tree parent child .
But usint this java traitement it consumes too much process and memory.
I want to simplify this method using a recursive postgres method and use the result in the java method.
I work with Spring boot version 2 and I use JPA and hibernate version 5.2
the #RestController class
#GetMapping("/collegeBook")
#Timed
public ResponseEntity<List<CollegeDTO>> getAllCollegeBook(Pageable pageable, #RequestParam(name="lang", required=false) String lang) {
Page<CollegeDTO> page = collegeService.findAllCollege(pageable, lang);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/collegeBook");
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
and this is the method in service class
public Page<CollegeDTO> findAllCollege(Pageable pageable, String lang) {
List<CollegeDTO> list=collegeRepository.findCollegeList(pageable, lang);
List<CollegeDTO> unitChildList=getChildrenList(list,lang,pageable);
return new PageImpl<CollegeDTO>(unitChildList);
}
private List<CollegeDTO> getChildrenList(
List<CollegeDTO> collegeList, String lang,Pageable pageable) {
for(CollegeDTO collegeDTO : collegeList) {
List<CollegeDTO> childrenListEntity = collegeRepository.findCollegeByParentId(pageable, lang,CollegeDTO.getValue());
collegeDTO.setChildren(getChildrenList(childrenListEntity,lang,pageable));
}
return collegeList;
}
this is the Repository method
public List<CollegeDTO> findCollegeList(Pageable pageable, String lang) {
String querystr = "SELECT ";
querystr += " collegelang.name AS text ,";
querystr += " college.id AS value ,";
querystr += " (select count(*) from student where student.college_id = college.id) as nbr ";
querystr += " FROM college college ";
querystr += " LEFT OUTER JOIN collegelang collegel ON collegelang.college_id = college.id ";
querystr += " Where collegelang.lang = :lang and college.status ='1' and parentid is null order by app_order asc";
log.debug("-- Query:" + querystr);
Query query = em.createNativeQuery(querystr, "CollegeDTOMap");
query.setParameter("lang", lang);
List<CollegeDTO> collegeDTOs = query.getResultList();
return collegeDTOs;
}
public List<CollegeDTO> findCollegeByParentId(Pageable pageable, String lang, String idParent) {
String querystr = "SELECT ";
querystr += " collegelang.name AS text ,";
querystr += " college.id AS value ,";
querystr += " (select count(*) from student where student.college_id = college.id) as nbr";
querystr += " FROM college college ";
querystr += " LEFT OUTER JOIN collegelang collegelang ON collegelang.college_id = college.id ";
querystr += " Where collegelang.lang = :lang and college.status ='1' and un.parentid = :idParent ";
log.debug("-- Query:" + querystr);
Query query = em.createNativeQuery(querystr, "CollegeDTOMap");
query.setParameter("lang", lang);
query.setParameter("idParent", idParent);
List<CollegeDTO> collegeDTOs = query.getResultList();
return collegeDTOs;
}
CollegeDTO class
public class CollegeDTO{
private String text;
private String value;
private String nbr;
private List<CollegeDTO> children;
public CollegeDTO(String text,String value,String nbr, List<CollegeDTO> children) {
super();
this.text = text;
this.value = value;
this.nbr = nbr;
this.children = children;
}
//getter and setter .......
}
College class
#SqlResultSetMapping(
name = "CollegeDTOMap",
classes = #ConstructorResult(
targetClass = CollegeDTO.class,
columns = {
#ColumnResult(name = "text"),
#ColumnResult(name = "value"),
#ColumnResult(name = "nbr")
}
)
)
public class College {
..............
..............
}
Updated :
I try with this query but I didn't find the way to use the result in java traitement .
My goals is to return Page<CollegeDTO>
which contain child having CollegeDTO as a type
with recursive nodes as (
select college.id AS value, CAST(collegelang.name AS varchar) AS text,
(select count(*) from student where student.college_id = college.id) as nbr,
ARRAY[college.id,collegelang.name,(select count(*) from student where student.college_id = college.id)]
as parents,college.app_order
FROM college college
LEFT OUTER JOIN collegelang collegelang ON collegelang.college_id = college.id
Where collegelang.lang = 'AR' and college.status ='1' and parentid is null
union all
select o.id AS value, CAST(collegelang.name AS varchar) AS text,(select count(*) from student where student.college_id = college.id) as nbr
,parents || ARRAY[o.id,collegelang.name,(select count(*) from student where student.college_id = college.id)]
,o.app_order
FROM college o
join nodes n on n.value = o.parentid
LEFT OUTER JOIN collegelang collegelang ON collegelang.unit_id = o.id and collegelang.lang='AR'
where o.status ='1'
)
select CAST(text AS varchar) as text,value as value,nbr as nbr, parents as children from nodes
order by app_order asc
I try to modify College class
#SqlResultSetMapping(
name = "CollegeDTOMap",
classes = #ConstructorResult(
targetClass = CollegeDTO.class,
columns = {
#ColumnResult(name = "text"),
#ColumnResult(name = "value"),
#ColumnResult(name = "nbr"),
#ColumnResult(name = "children") // add this line
}
)
)
public class College {
..............
..............
}
but I have this error using the previous query
Caused by: org.hibernate.HibernateException: Could not resolve column name in result set [children]
Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2003

sql for paging notice board in jsp

I'm trying to make a notice board and before apply paging function it showed posts well.
but after i applied paging fuction it shows only one post and paging function shows pages well.
so i think this is a sql problem.
when i try to show count it shows 1. even i tried with this shows 1.
sql = "select * from mvc_board ";
this is my sql
String sql = "SELECT ROWNUM, bId, bName, bTitle, bContent," +
"bDate, bHit, bGroup, bStep, bIndent " +
"from(select * from mvc_board order by bGroup DESC , bStep asc)";
i tried to make mine from this sql. but couldn't understand.
sql = "select *, (select u_name from user where idx = writer_fk) writer, (select idx from answer where idx = answer_fk) answer from board order by idx desc limit "+startRow+", "+endRow;
ref. http://queserasera.tistory.com/14
this is DAO
public ArrayList<BDto> getBoardList(int startRow, int endRow, String keyField, String keyWord) {
ArrayList<BDto> dtos = new ArrayList<BDto>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = "SELECT ROWNUM, bId, bName, bTitle, bContent," +
"bDate, bHit, bGroup, bStep, bIndent " +
"from(select * from mvc_board order by bGroup DESC , bStep asc)";
System.out.println(sql);
try{
if(keyWord != null && !keyWord.equals("") && !keyWord.equals("null")) {
sql += " WHERE " + keyField.trim() +" LIKE '%"+keyWord.trim()+"%'";
}
connection = dataSource.getConnection();
//특정행부터 레코드를 가져오기 위해서 옵션 설정
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
int count=0;
int ROWNUM = resultSet.getInt("ROWNUM");
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
BDto dto = new BDto(ROWNUM, bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
dtos.add(dto);
count++;
System.out.println(count);
//while문끝
}//if문끝
}catch (Exception e) {
System.out.println(e+"=> getBoardList fail");
}finally {
try {
if(resultSet != null) resultSet.close();
if(preparedStatement != null) preparedStatement.close();
if(connection != null) connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
return dtos;
}
=============added sql on sql developer==================================
select rn, bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent
from ( select b.*, row_number() over (order by bGroup DESC, bStep asc) rn
from mvc_board b %s )
where rn between 1 and 10
order by rn;
The main problem in your getBoardList() method was the usage of if (resultSet.next()) instead of while (resultSet.next()) when iterating through the result set; that caused the code to only ever return a single DTO.
There are other things you should also consider. For example, starting from Java 7, it's preferrable to use the try-with resources statement instead of traditional try-finally for JDBC resource handling.
An important thing to note is that injecting field names like keyField from the UI is a really bad practice because it makes the query vulnerable to SQL injection.
You should at least create a static whitelist of allowed field names to sanitize the input, if you cannot use static WHERE conditions.
Finally, paging queries in Oracle 11g are traditionally done with an inline view that uses the row_number analytic function. Note that row-numbering starts from 1. Starting from Oracle 12c, the row-limiting clause can be used and the inline view is no longer needed.
An example implementation that considers the above points could look like the following:
public List<BDto> getBoardList(int startRow, int endRow, String keyField, String keyWord) {
List<BDto> dtos = new ArrayList<BDto>();
// the %s in the template will be replaced with a
// WHERE condition when a keyword is present
final String sqlTemplate = "select rn, bId, bName, bTitle, "
+ "bContent, bDate, bHit, bGroup, bStep, bIndent "
+ "from ( "
+ " select b.*, row_number() over (order by bGroup DESC, bStep asc) rn"
+ " from mvc_board b %s "
+ " ) "
+ " where rn between ? and ? "
+ "order by rn";
boolean whereCondition = false;
String sql = null;
if (keyWord != null && !keyWord.equals("") && !keyWord.equals("null")) {
sql = String.format(sqlTemplate,
" WHERE " + keyField.trim() + " LIKE '%' || ? || '%'");
whereCondition = true;
} else {
sql = String.format(sqlTemplate, "");
}
System.out.println(sql);
try (Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int parameterIndex = 1;
if (whereCondition) {
preparedStatement.setString(parameterIndex++, keyWord);
}
preparedStatement.setInt(parameterIndex++, startRow);
preparedStatement.setInt(parameterIndex, endRow);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
int count = 0;
while (resultSet.next()) {
int ROWNUM = resultSet.getInt("rn");
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
dtos.add(new BDto(ROWNUM, bId, bName, bTitle,
bContent, bDate, bHit, bGroup, bStep, bIndent));
count++;
}
System.out.println(count);
}
} catch (SQLException e) {
System.out.println(e + "=> getBoardList fail");
}
return dtos;
}
In the example you gave us the paging is added by the limit clause, but ORACLEhas it's own syntax for these. Try:
String sql = "SELECT rown, bId, bName, bTitle, bContent," +
"bDate, bHit, bGroup, bStep, bIndent " +
"from(select rownum rown, mvc_board.* from mvc_board order by bGroup DESC , bStep asc) " +
"WHERE rown between ? and ? ";
Bind startRow and endRow before executing the query:
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,startRow);
preparedStatement.setInt(2,endRow);
resultSet = preparedStatement.executeQuery();
Change the WHERE to AND:
sql +=" and " + keyField.trim()+" LIKE '%"+keyWord.trim()+"%'" ;
And finally change the if(resultSet.next()){ back to a while loop to get more then one row as result.

sql Query builder in JPA query?

I've made a search tool in java.
String query = "SELECT * FROM Customer WHERE 1 = 1 ";
if (!firstname.isEmpty()) query += "AND cName = '" + firstname + "' ";
if (!lastname.isEmpty()) query += "AND cLastName = '" + lastname + "' ";
if (!epost.isEmpty()) query += "AND cEpost = '" + epost + "' ";
if (!phonenumber.isEmpty()) query += "AND cPhonenumber '" + phonenumber + "' ";
That ouput this if all of those paramerets has values:
SELECT * FROM Customer WHERE 1 = 1
AND cName = 'test'
AND cLastName = 'test1'
AND cEpost = 'test2'
AND cPhonenumber 'test3'
This way I can get better results by filling in more data, but i can still choose to not do.. I need a solution for JPA for this.. any tips?
Thanks!
EDIT: End result based on the answer below:
public static List<Customer> searchCustomersByParameters(String firstname, String lastname,
String epost, String phonenumber) {
String sql = "SELECT c FROM Customer c WHERE 1 = 1 ";
if (!firstname.isEmpty()) sql += "AND c.cName = :firstname ";
if (!lastname.isEmpty()) sql += "AND c.cLastName = :lastname ";
if (!epost.isEmpty()) sql += "AND c.cEpost = :epost ";
if (!phonenumber.isEmpty()) sql += "AND c.cPhonenumber = :phonenumber";
Query q = em.createQuery(sql);
if (!firstname.isEmpty()) q.setParameter("firstname", firstname);
if (!lastname.isEmpty()) q.setParameter("lastname", lastname);
if (!epost.isEmpty()) q.setParameter("epost", epost);
if (!phonenumber.isEmpty()) q.setParameter("phonenumber", phonenumber);
return q.getResultList();
}
While it is of course possible to create dynamic SQL using string concatenation as suggested in this answer, a more type safe and less risky (in terms of SQL injection) approach is to use the JPA criteria API
public static List<Customer> searchCustomersByParameters(String firstname, String lastname,
String epost, String phonenumber) {
var qb = em.getCriteriaBuilder();
var query = qb.createQuery(Customer.class);
var root = query.from(Customer.class);
query.select(root);
if (!firstname.isEmpty()) query.where(qb.equal(root.get("cName"), firstName));
if (!lastname.isEmpty()) query.where(qb.equal(root.get("cLastName"), lastname));
if (!epost.isEmpty()) query.where(qb.equal(root.get("cEpost "), epost ));
if (!phonenumber.isEmpty()) query.where(qb.equal(root.get("cPhonenumber "), phonenumber));
return em.createQuery(query).getResultList();
}
... or if you don't strictly need to use JPQL you could also use a third party SQL builder like jOOQ:
public static List<Customer> searchCustomersByParameters(String firstname, String lastname,
String epost, String phonenumber) {
return
ctx.selectFrom(CUSTOMER)
.where(!firstname.isEmpty() ? CUSTOMER.CNAME.eq(firstname) : noCondition())
.and(!lastname.isEmpty() ? CUSTOMER.CLASTNAME.eq(lastname) : noCondition())
.and(!epost.isEmpty() ? CUSTOMER.CEPOST.eq(epost) : noCondition())
.and(!phonenumber.isEmpty() ? CUSTOMER.CPHONENUMBER.eq(phonenumber) : noCondition())
.fetchInto(Customer.class);
}
Disclaimer: I work for the company behind jOOQ
use ? and set Parameters for preventing sql injection and in JPA you can use native sql as old way you do and also JPQL.Generate your sql by conditions and set your parameters.I use here where 1=1 condition to easy append next conditions by and.Otherwise you will have difficulties for appending "where" to your sql.
by native:
public static List<YourEntity> getFromTable(String name,String surname) {
EntityManager em = PersistenceManager.instance().createEntityManager();
try {
String sql = " select * from table where 1=1 ";
if(name!=null && !name.trim().isEmpty()){
sql +=" and name = :name";
}
if(surname!=null && !surname.trim().isEmpty()){
sql +=" and surname = :surname";
}
Query q = em.createNativeQuery(sql);
if(name!=null && !name.trim().isEmpty()){
q.setParameter("name", name);
}
if(surname!=null && !surname.trim().isEmpty()){
q.setParameter("surname", surname);
}
List<YourEntity> l = q.getResultList();
return l;
} finally {
em.close();
}
}
By jpql:
public static List<YourEntity> getFromTable(String name,String surname) {
EntityManager em = PersistenceManager.instance().createEntityManager();
try {
String sql = " select e from YourEntity e where 1=1 ";
if(name!=null && !name.trim().isEmpty()){
sql +=" and e.name = :name";
}
if(surname!=null && !surname.trim().isEmpty()){
sql +=" and e.surname = :surname";
}
Query q = em.createQuery(sql);
if(name!=null && !name.trim().isEmpty()){
q.setParameter("name", name);
}
if(surname!=null && !surname.trim().isEmpty()){
q.setParameter("surname", surname);
}
List<YourEntity> l = q.getResultList();
return l;
} finally {
em.close();
}
}

Hibernate Query whit function count

I am studying Hibernate and in a exercise I must do this query
"SELECT count(id) as numero_utenti, imc
FROM Utenti
WHERE azienda = '" + id + "' GROUP BY imc"
the problem is that, I don't know as view the result and to save the result in a string.
Thanks.
This is the function
public String getStat(int id) {
String stat = "";
int count = 0;
try {
Query query = session.createQuery("SELECT count(id) as numero_utenti, imc FROM Utenti WHERE azienda = '" + id + "' GROUP BY imc");
// as I extract the values​​?
tx.commit();
} catch (HibernateException he) {
throw he;
}
return stat;
}
If you are looking how to execute query using hibernate session.
query = session.createQuery("semect * from temp");
and query instance
Long.valueOf(query.fetchCountOfRows()).intValue();
It'll give you no. row count.
If they are not unique, get the list.
List<String> imcs= null;
int count = 0;
Query query = session.createQuery("SELECT imc FROM Utenti WHERE azienda = '" + id + "' GROUP BY imc");
imcs = query.list();
count = imcs.size();

Parameter index out of range (1 > number of parameters, which is 0).; nested exception is java.sql.SQLException: [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm getting this error:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3840)
at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3784)
at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:4052)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:816)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:1)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:812)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:868)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:876)
When i'm trying to execute this class:
public List<CandidateDO> getCandidateList(final JobDO currentJob) {
List<CandidateDO> appliedCandidateList = new ArrayList<CandidateDO>();
CandidateDO candidate = new CandidateDO();
String query = "SET #sql = NULL;";
jdbcTemplate.execute(query);
query = "SELECT\n"
+ " GROUP_CONCAT(DISTINCT\n"
+ " CONCAT(\n"
+ " 'MAX(IF(`skillId` = ', `skillId`, ',levelId,NULL)) AS `',\n"
+ " `skillId`, '`'\n"
+ " )\n"
+ " ) INTO #sql\n"
+ "FROM tblCandidateToSkill,candidateToJob,tblCandidate\n"
+ "where tblCandidateToSkill.candidateId = candidateToJob.candidateId and tblCandidate.id = candidateToJob.candidateId;";
jdbcTemplate.execute(query);
query = "SET #sql = CONCAT('SELECT tblCandidateToSkill.candidateId,name,email,dob,phoneNumber,alternateNumber,addressLine, ', #sql, ' \n"
+ " FROM tblCandidateToSkill,candidateToJob,tblCandidate\n"
+ " where tblCandidateToSkill.candidateId = candidateToJob.candidateId and tblCandidate.id = candidateToJob.candidateId\n"
+ " and candidateToJob.jobId= ? \n"
+ " GROUP BY candidateId');";
jdbcTemplate.update(query, new Object[]{currentJob.getId()});
query = "PREPARE stmt FROM #sql;";
jdbcTemplate.execute(query);
query = "EXECUTE stmt;";
appliedCandidateList = jdbcTemplate.query(query, new RowMapper<CandidateDO>() {
#Override
public CandidateDO mapRow(ResultSet rs, int i) throws SQLException {
CandidateDO candidate = new CandidateDO();
candidate.setId(rs.getInt(1));
candidate.setName(rs.getString("name"));
candidate.setDob(rs.getDate("dob"));
candidate.setPhoneNumber(rs.getString("phoneNumber"));
candidate.setAlternateNumber(rs.getString("alternateNumber"));
candidate.setAddressLine(rs.getString("addressLine"));
for (Iterator<SkillDO> it = currentJob.getSkills().iterator(); it.hasNext();) {
SkillDO skill = it.next();
public List<CandidateDO> getCandidateList(final JobDO currentJob) {
List<CandidateDO> appliedCandidateList = new ArrayList<CandidateDO>();
CandidateDO candidate = new CandidateDO();
String query = "SET #sql = NULL;";
jdbcTemplate.execute(query);
query = "SELECT\n"
+ " GROUP_CONCAT(DISTINCT\n"
+ " CONCAT(\n"
+ " 'MAX(IF(`skillId` = ', `skillId`, ',levelId,NULL)) AS `',\n"
+ " `skillId`, '`'\n"
+ " )\n"
+ " ) INTO #sql\n"
+ "FROM tblCandidateToSkill,candidateToJob,tblCandidate\n"
+ "where tblCandidateToSkill.candidateId = candidateToJob.candidateId and tblCandidate.id = candidateToJob.candidateId;";
jdbcTemplate.execute(query);
query = "SET #sql = CONCAT('SELECT tblCandidateToSkill.candidateId,name,email,dob,phoneNumber,alternateNumber,addressLine, ', #sql, ' \n"
+ " FROM tblCandidateToSkill,candidateToJob,tblCandidate\n"
+ " where tblCandidateToSkill.candidateId = candidateToJob.candidateId and tblCandidate.id = candidateToJob.candidateId\n"
+ " and candidateToJob.jobId= ? \n"
+ " GROUP BY candidateId');";
jdbcTemplate.update(query, new Object[]{currentJob.getId()});
query = "PREPARE stmt FROM #sql;";
jdbcTemplate.execute(query);
query = "EXECUTE stmt;";
appliedCandidateList = jdbcTemplate.query(query, new RowMapper<CandidateDO>() {
#Override
public CandidateDO mapRow(ResultSet rs, int i) throws SQLException {
CandidateDO candidate = new CandidateDO();
candidate.setId(rs.getInt(1));
candidate.setName(rs.getString("name"));
candidate.setDob(rs.getDate("dob"));
candidate.setPhoneNumber(rs.getString("phoneNumber"));
candidate.setAlternateNumber(rs.getString("alternateNumber"));
candidate.setAddressLine(rs.getString("addressLine"));
for (Iterator<SkillDO> it = currentJob.getSkills().iterator(); it.hasNext();) {
SkillDO skill = it.next();
SkillLevelDO CandidateSkillLevel = new SkillLevelDO();
CandidateSkillLevel.setId(rs.getInt(String.valueOf(skill.getId())));
candidate.getSkillLevel().add(CandidateSkillLevel);
candidate.getSkills().add(skill);
}
return candidate;
}
});
query = "DEALLOCATE PREPARE stmt;";
jdbcTemplate.execute(query);
return appliedCandidateList;
} SkillLevelDO CandidateSkillLevel = new SkillLevelDO();
CandidateSkillLevel.setId(rs.getInt(String.valueOf(skill.getId())));
candidate.getSkillLevel().add(CandidateSkillLevel);
candidate.getSkills().add(skill);
}
return candidate;
}
});
query = "DEALLOCATE PREPARE stmt;";
jdbcTemplate.execute(query);
return appliedCandidateList;
}
Check out the reason for the error here.
Looks like the following query is having an issue.
query = "SET #sql = CONCAT('SELECT tblCandidateToSkill.candidateId,name,email,dob,phoneNumber,alternateNumber,addressLine, ', #sql, ' \n"
+ " FROM tblCandidateToSkill,candidateToJob,tblCandidate\n"
+ " where tblCandidateToSkill.candidateId = candidateToJob.candidateId and tblCandidate.id = candidateToJob.candidateId\n"
+ " and candidateToJob.jobId= ? \n"
+ " GROUP BY candidateId');";
Your single quotes (') is suspicious. The (?) PLACEHOLDER inside the query is within the Single quotes which is just considered as STRING rather than a (?) PLACEHOLDER.

Categories

Resources