使用jdbc从Web应用程序连接到Oracle 11g时的SQL异常

我正在获取SQL异常。

以下是我的代码,

 public DBConnect()
    {
      try {
            // Load the Oracle JDBC driver
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

            // connect through driver
             conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","system123");
           //  conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","system123");


            // create a statement object

             CallableStatement cstmt = null;
             String auth;

             try {
                 cstmt = conn.prepareCall("{? = call login(?,?)}");
                 cstmt.registerOutParameter(1, java.sql.Types.BOOLEAN);
                 cstmt.setString(2, "aniket");
                 cstmt.setString(3, "aniket");
                 cstmt.execute();
                 auth = cstmt.getString(1);
                 cstmt.close();

                 System.out.println(auth);

             } catch (SQLException e) {

                 e.printStackTrace();

             }

        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    catch(Exception e)
    {
        //system.out.println(e.printStackTrace());
    }

    }

以下是堆栈跟踪 -

java.sql.SQLException: ORA-01403: no data found

ORA-06512:在“SYSTEM.LOGIN”,第10行ORA-06512:在第1行

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:754)
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:218)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:971)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1192)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3415)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3521)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4612)
at com.me.db.DBConnect.<init>(DBConnect.java:57)
at com.me.controller.PreLoginController.handleRequestInternal(PreLoginController.java:25)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:203)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

以下是我的PL / SQL函数登录 -

CREATE OR REPLACE FUNCTION login(p_uname IN varchar2, p_pword 

IN varchar2)RETURN String AS

v_uname varchar2(30); v_pword varchar2(30);

开始

SELECT uname, pword INTO v_uname, v_pword
FROM user_account
WHERE uname = p_uname AND  pword = p_pword;

IF (v_uname IS NULL OR v_pword IS NULL) THEN

    return 'Failed';

ELSE

    return 'Success';

END IF;

END登录; /


您在login PL / SQL函数声明的参数类型与您在Java中设置的类型之间存在不匹配。 boolean似乎是最有可能的候选人。 确保返回类型的login与java布尔值真正兼容。


正如skaffman所述,Oracle JDBC驱动程序不支持PL / SQL布尔类型。 所以我改变了返回类型为varchar。 在我的PL / SQL代码中缺少一个更重要的事情是在返回0行(即NO_DATA_FOUND)的情况下处理异常

在PL / SQL函数登录中添加了该异常,并将返回'False'的位置更改为异常部分,因为IF NO_DATA_FOUND THEN返回'False';

链接地址: http://www.djcxy.com/p/36969.html

上一篇: SQL Exception while connecting to oracle 11g from web application using jdbc

下一篇: separated string but ignoring commas in quotes