java.sql.SQLException:ORA-00933:SQL命令未正确结束

时间:2012-09-24 23:20:48

标签: java oracle jdbc

我想要完成的是能够编写SQL语句并验证结果。 SQL语句中包含变量,如:

String sql = "Select  zoneid from zone where zonename = myZoneName";

从count +

生成myZoneName的位置

注意:我使用Apache POI来解析Excel电子表格。

这是代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.junit.Test;

public class VerifyDBSingleDomain {

    static Logger log = Logger.getLogger(VerifyDBSingleDomain.class.getName());

    String url = "jdbc:oracle:thin:@a.b.c.d:1521:mname";

    @Test
    public void VerifyDBSingleDomainTest() throws SQLException {

        Properties props = new Properties();
        props.setProperty("user", "user");
        props.setProperty("password", "password");

        String sql = "Select  zoneid from zone where zonename = 99224356787.tv";
        //String sql = "Select * from zone";

        Connection conn;
        //try {
            conn = DriverManager.getConnection(url, props);
            PreparedStatement preStatement = conn.prepareStatement(sql);
            ResultSet result = preStatement.executeQuery();
            System.out.println(result);

            /*while (result.next()) {
                System.out.println(result.toString());
            }
        } catch (SQLException e) {

            e.printStackTrace();
        }*/

    //  }
    //}

    }
}

我收到错误:

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

2 个答案:

答案 0 :(得分:4)

您应该在WHERE子句中使用单引号,假设myZoneName是文本类型:

String sql = "Select zoneid from zone where zonename = '99224356787.tv'";

使用以下内容显示zoneid,假设它是INTEGER类型:

while (result.next()) { 
   System.out.println(result.getInt("zoneid");
}

答案 1 :(得分:0)

如果myZoneName是Varchar类型,你必须将它包装在像这样的引号

        String sql = "Select  zoneid from zone where zonename = '99224356787.tv'";

获取表格内容:

ResultSet result = preStatement.executeQuery();
        System.out.println(result);

        while (result.next()) {
            System.out.println(result.getInt("zoneid")); // if zoneid is int in the db table
        }

请参阅Java resultSet API了解更多信息:)