gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:52 25 4
gpt4 key购买 nike

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

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

myZoneName 是从 count + 生成的

注意:我使用 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

最佳答案

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

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

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

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

关于java.sql.SQLException : ORA-00933: SQL command not properly ended 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12574249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com