jsp与oracle数据库连接

Posted

技术标签:

【中文标题】jsp与oracle数据库连接【英文标题】:connect jsp with oracle database 【发布时间】:2016-07-29 03:27:19 【问题描述】:

我编写了以下代码来从我的 oracle 数据库中获取数据并使用 JSP 显示结果:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
    <title>SELECT Operation</title>
</head>
<body>

    <sql:setDataSource var="snapshot" driver="oracle.jdbc.OracleDriver"
                       url="jdbc:oracle:thin:@localhost:1521:orcl"
                       user="hr"  password="Hrone699"/>

    <sql:query dataSource="$snapshot" var="result">
        SELECT No from Client;
    </sql:query>

    <table border="1" >
        <tr>
            <th>No</th>
            <th>Client</th>
            <th>Information Collection Date</th>
            <th>Payment Notice Date</th>
        </tr>
        <c:forEach var="row" items="$result.row">
            <tr>
                <td><c:out value="$row.No"/></td>
                <td><c:out value="$row.Client"/></td>
                <td><c:out value="$row.INFOCOLLECTINGDATE"/></td>
                <td><c:out value="$row.PAYMENTNOTICEISSUEDATE"/></td>
            </tr>
        </c:forEach>
    </table>

</body>

但我收到以下错误: 发生错误:

从客户那里选择否; : ORA-00911: 无效字符

使用以下堆栈跟踪:

javax.servlet.ServletException: 

从客户那里选择否; : ORA-00911: 无效字符

at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:951)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:886)
at org.apache.jsp.dataBaseConnect_jsp._jspService(dataBaseConnect_jsp.java:111)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:630)
at com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:363)
at com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:153)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:253)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.Abstractiostrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)

原因:java.sql.SQLException: ORA-00911: 无效字符

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:330)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:287)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:753)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:218)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:812)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1050)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:853)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1155)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3414)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3459)
at org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.doEndTag(QueryTagSupport.java:257)
at org.apache.jsp.dataBaseConnect_jsp._jspx_meth_sql_query_0(dataBaseConnect_jsp.java:172)
at org.apache.jsp.dataBaseConnect_jsp._jspService(dataBaseConnect_jsp.java:88)
... 52 more

【问题讨论】:

可能 oracle 得到一些 UTF-8 字符并且不喜欢它。 根据***.com/questions/10728377/…,问题是查询末尾的分号(;)。在 JDBC 调用中是不允许的。 【参考方案1】:

只需从 sql 语句中删除分号(“;”),因为 oracle 驱动程序不需要以分号结束 sql 语句,但是当您使用 mysql 数据库(mysql-connector 驱动程序)时,您必须提及分号(“;”) )

【讨论】:

以上是关于jsp与oracle数据库连接的主要内容,如果未能解决你的问题,请参考以下文章

jsp,用spring框架连接oracle数据库

无法连接 Tomcat 6 和 oracle 10g XE 进行 jsp 数据库连接

在 JSP 中动态建立 Oracle DB 连接

jsp简易登录和注册及JDBC连接Oracle

JDBC连接ORACLE

Java Web与数据库连接