Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very weird PreparedStatement in java?

I found a very odd PreparedStatement in a program today do to an error that I received. I tracked it down and found some perplexing SQL. Now, I am no master of SQL or PreparedStatements, but this seems very wrong. I should also mention that this works on a coworker's computer but not mine.

static final String SELECT_UNCOMPLETE_TASKS_FOR_UPGRADE =
"SELECT i.employeeid, i.taskid, i.itptaskarchivecd, i.itptaskstartdt, i.itptaskcompletiondt,t.taskobsoletecd, "
+ "t.taskknowledgetx, t.taskProductid, t.taskrfrncid, t.taskcorecd, t.taskwartimecd, t.parentheaderid, "
    + "t.taskparentindcd, i.itptaskstatuscd, i.itptaskarchivedt, t.certified "
    + "FROM itptask i,task t "
    + "WHERE (i.itptaskcompletiondt is NULL "
        + "AND i.employeeid = ?1 "
        + "AND i.taskid = t.taskid "
        + "AND i.itptaskarchivecd = ?2 "
        + "AND t.taskproductcd = ?3 "
        + "AND t.taskobsoletecd = ?4 "
        + "AND t.taskcorecd = ?5) "
    + "OR (i.employeeid = ?6 "
        + "AND i.taskid = t.taskid "
        + "AND 'T' = t.taskparentindcd "
        + "AND t.taskproductcd = ?7)";

My question is simple, what are the numbers after the parameter placeholders (Question marks) for? If this is completely incorrect syntax, what would allow it to work in another workspace/environment? Any and all help would be greatly appreciated. Thanks.

like image 333
Justin Avatar asked Jul 01 '14 19:07

Justin


1 Answers

That particular syntax looks to be specific to JPA syntax. In JPA, you could specify ?# to specify the parameter index via a Query object.

Query myQuery = entityManager.createNativeQuery("select * from my_table where foo = ?1 and bar = ?2");

q.setParameter(1,myFoo);
q.setParameter(2,myBar);
like image 181
Nick Avatar answered Nov 09 '22 23:11

Nick