1
votes
public String passlink(String companyName, String mailName) {

    Session session = Hibernateutil.getSessionFactory().openSession();

    Transaction transaction = null;

    try {
        transaction = session.beginTransaction();

        String queryString="UPDATE account SET companyname = :companyName WHERE companyname like 'm' AND Email LIKE :mailName";
        Query query=session.createQuery(queryString);

        query.setParameter("companyName", companyName);
        query.setParameter("mailName", mailName);
        int result = query.executeUpdate();

        System.out.println("Rows affected: " + result);

            System.out.println("Successfully updated");
        transaction.commit();

    } catch (Exception e) {
        value = "FAIL";
                   e.printStackTrace();
        //System.out.println("Error--> " + e.getMessage());
    }
    return value;

}

I write update query

String queryString="UPDATE account SET companyname = :companyName WHERE companyname like 'm' AND Email LIKE :mailName";

like this but it gives following error.

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at org.hibernate.hql.ast.HqlSqlWalker.postProcessUpdate(HqlSqlWalker.java:390) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:164) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:425) at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:880) at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:861) at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89) at com.njob.dao.AccountDao.passlink(AccountDao.java:213) at com.njob.action.companylink.doPost(companylink.java:34) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Unknown Source)

2

2 Answers

3
votes

Change the query to

String queryString="UPDATE account SET companyname = :companyName WHERE companyname like 'm' AND email LIKE :mailName";

and use

query.setParameter("companyName", companyName);
query.setParameter("mailName", mailName);

See

1
votes

try this

 query.setParameter("companyName", companyName);

The parameter you use is companyName