-1
     public int CalableStatement(String query,Login login,String type) throws  ClassNotFoundException, SQLException
        {    int  i =0;
         try {
    Connection con = getConnection();   
    //String q = query+"(?,?,?)";
    CallableStatement cst = con.prepareCall("call loginPlan_k(?,?,@result)");

    //Login login = null ;
        //String type=lassType.getType();
        /*if(type.equals("login"))
        {
        login = (Login)o;
        }*/
        System.out.println(login.getUserName()+"DataAccess");
        cst.setString(1, login.getUserName());
        cst.setString(2, login.getPassword());
        cst.registerOutParameter(3,java.sql.Types.INTEGER);

        ResultSet rs = cst.executeQuery(query);

     i = cst.getInt(1);  yyy
}catch (Exception e) {
    // TODO: handle exception  
    e.printStackTrace();
}              


    return i ;


 }






java.lang.ArrayIndexOutOfBoundsException: 20outputfromdb
status0

    at com.mysql.jdbc.CallableStatement.checkIsOutputParam(CallableStatement.java:680)
    at com.mysql.jdbc.CallableStatement.registerOutParameter(CallableStatement.java:2015)
    at Plan__K_Utilities.DataBaseAccess.CalableStatement(DataBaseAccess.java:72)
    at PLan__K_MYSQL_DAL.LoginClass.Authenticate(LoginClass.java:22)
    at ServletPackage.Login.doPost(Login.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我正在尝试 sql 实用程序类 CallableStatement 该过程在 DB 中执行良好,但 arrayindex 超出范围错误是我得到的我试图调试它似乎 callablestatement 的语法有问题

public int CalableStatement(String query,Login login,String type) throws ClassNotFoundException, SQLException
{    int  i =0;
    try {
    Connection con = getConnection();   
    //String q = query+"(?,?,?)";
    CallableStatement cst = con.prepareCall("call loginPlan_k(?,?,?)");

    //Login login = null ;
    //String type=lassType.getType();
    /*if(type.equals("login"))
    {
        login = (Login)o;
    }*/
    System.out.println(login.getUserName()+"DataAccess");
    cst.setString(1, login.getUserName());
    cst.setString(2, login.getPassword());
    cst.registerOutParameter(3,java.sql.Types.INTEGER);

    ResultSet rs = cst.executeQuery(query);
     if(rs.next())
     { i = cst.getInt(1);}
}catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}
    return i ;


}


}

这是我收到的错误

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 '?,?,?)' at line 1
0outputfromdb
status0
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2768)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612)
    at Plan__K_Utilities.DataBaseAccess.CalableStatement(DataBaseAccess.java:74)
    at PLan__K_MYSQL_DAL.LoginClass.Authenticate(LoginClass.java:22)
    at ServletPackage.Login.doPost(Login.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

2 回答 2

0

没有第三个参数,而是一些奇怪的@result 文本。用问号替换它。

  CallableStatement cst = con.prepareCall("{call loginPlan_k(?,?,?)}");

请参阅 Oracle 示例:http ://www.literak.cz/2013/08/working-with-complex-database-types-in-weblogic/

于 2014-01-25T21:33:47.760 回答
0

输出参数应由占位符表示,因为它们用于输入参数。你还需要用CallableStatements大括号括起来{}

CallableStatement cst = con.prepareCall("{call loginPlan_k(?,?,?)}");
于 2014-01-25T21:34:08.940 回答