如何为Equi join设置PreparedStatement占位符?

时间:2017-11-28 10:06:51

标签: oracle prepared-statement

我有一个查询,我需要传递equi join的值  我试过下面的

package com.iseva.Servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Connectionfactory.ConnectionFactory;

/**
 * Servlet implementation class GetDetailsFROutWard
 */
@WebServlet("/GetDetailsFROutWard")
public class GetDetailsFROutWard extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Connection cn=null;
    public void init()
    {
        cn=ConnectionFactory.getConnection();
        System.out.println("connection up");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String ref=request.getParameter("t2");
    System.out.println();
    try {
        PreparedStatement ps=cn.prepareStatement("select PAYMENT_DETAILS.CUSTOMER_NAME,PAYMENT_DETAILS.MOBILE_NUMBER_1,customer_data.SHOP_CITY from PAYMENT_DETAILS ,customer_data where PAYMENT_DETAILS.iseve_ref_num=?=customer_data.iseve_ref_num=?");
        ps.setString(1, ref);
        ps.setString(2, ref);
        ResultSet rs=ps.executeQuery();
        while(rs.next()) 
        {
            System.out.println(rs.getString(1));
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

}

但是给出了Exception

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:863)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3620)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
    at com.iseva.Servlet.GetDetailsFROutWard.doGet(GetDetailsFROutWard.java:37)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1519)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1475)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

如何为holders

中的联接放置PreparedStatement
PAYMENT_DETAILS.num=customer_data.num

我希望将holders and pass values置于

ps.setString(1, ref);
ps.setString(2, ref);

1 个答案:

答案 0 :(得分:0)

虽然我需要编写并运行你的Sql语句以清楚地理解异常,但通过观察我可以在你的语句中看到“ 其中PAYMENT_DETAILS.iseve_ref_num =?= customer_data.iseve_ref_num =?

你能把'And'放在2个where子句之间,请再试一次吗?像:

"where PAYMENT_DETAILS.iseve_ref_num=? And customer_data.iseve_ref_num=?"

我认为需要修改查询。您可以做的另一件事是在任何sql工具(如 Sqldeveloper )中运行Sql语句,或者确认此类异常。