作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到 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);
开始
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;
结束登录;/
最佳答案
正如 skaffman 提到的,Oracle JDBC 驱动程序不支持 PL/SQL boolean 类型。所以我将返回类型更改为varchar。我的 PL/SQL 代码中缺少的另一件关键的事情是返回 0 行时的异常处理,即 NO_DATA_FOUND
在 PL/SQL 函数登录中添加了该异常,并将 Returning 'False' 的位置更改为异常部分如果没有找到数据,那么 返回“假”;
关于java - 使用 jdbc 从 Web 应用程序连接到 Oracle 11g 时出现 SQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5707398/
我是一名优秀的程序员,十分优秀!