1
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;

}

我写更新查询

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

像这样,但它给出了以下错误。

java.lang.IndexOutOfBoundsException:索引:0,大小: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) 在 org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:164) 在 org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189) 在 org.hibernate.hql .ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130) 在 org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) 在 org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:425) 在org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:880) 在 org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:861) 在 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) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) 在 org.apache.catalina.core.StandardContextValve.invoke( StandardContextValve.java:178) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) 在 org.apache.catalina.valves.ErrorReportValve。在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) 调用(ErrorReportValve.java:105) .coyote.http11.Http11Processor.process(Http11Processor.java:825) 在 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731) 在 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket (PoolTcpEndpoint.java:526) 在 org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) 在 org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) ) 在 java.lang.Thread.run(Unknown Source)107) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825) 在 org.apache.coyote.http11.Http11Protocol $Http11ConnectionHandler.processConnection(Http11Protocol.java:731) 在 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526) 在 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)107) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825) 在 org.apache.coyote.http11.Http11Protocol $Http11ConnectionHandler.processConnection(Http11Protocol.java:731) 在 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526) 在 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)org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526) 中的 org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.runIt) 中的 Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731) java:80) 在 org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) 在 java.lang.Thread.run(Unknown Source)org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526) 中的 org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.runIt) 中的 Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731) java:80) 在 org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) 在 java.lang.Thread.run(Unknown Source)

4

2 回答 2

3

将查询更改为

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

并使用

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

于 2012-08-08T07:50:55.390 回答
1

试试这个

 query.setParameter("companyName", companyName);

您使用的参数是 companyName

于 2012-08-08T07:44:17.697 回答