2011-04-18 95 views
0

我收到SQL異常。使用jdbc從Web應用程序連接到Oracle 11g時的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) 返回字符串 AS

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

BEGIN

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登錄; /

回答

1

您在PL/SQL函數login聲明的參數類型與您在Java中設置的類型之間存在不匹配。 boolean似乎是最有可能的候選人。確保返回類型login真的與java布爾值兼容。

+0

在檢查堆棧跟蹤之後,我也這麼認爲,但函數返回布爾型 – Aniket 2011-04-18 19:14:06

+1

@acoolguy:啊,好的。我似乎記得Oracle的JDBC驅動程序實際上並不支持PL/SQL BOOLEAN類型。你的錯誤表明他們還沒有解決這個問題。 – skaffman 2011-04-18 19:23:26

+0

現在我已經更改了我的login.sql,現在它返回一個字符串,我將編輯上面的文件。現在錯誤是不同的,雖然我在user_account表中插入了一行,但錯誤並未找到數據 – Aniket 2011-04-18 20:14:04

2

正如skaffman所述,Oracle JDBC驅動程序不支持PL/SQL布爾類型。 所以我改變了返回類型爲varchar。 我的PL/SQL代碼中缺少一個更重要的事情是在返回0行的情況下異常處理,即NO_DATA_FOUND

在PL/SQL函數login中添加了異常,並將返回'False'的位置更改爲異常部分as IF NO_DATA_FOUND THEN Return'False';

相關問題