【发布时间】:2016-08-09 07:49:15
【问题描述】:
我正在尝试连接到 oracle 数据库并获取一些记录。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectionTest {
static final String DB_URL = "jdbc:oracle:thin:@//connctionString";
static final String USER = "usr";
static final String PASS = "pwd";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try
{
System.out.println("Connecting");
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Connected");
String queryPtyRole = "select * from emp;";
System.out.println(conn);
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(queryPtyRole);
while (rs.next()) {
String emp_id = rs.getString("emp_id");
System.out.println("emp_id: " + emp_id);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
与数据库的连接正常,但它提供了以下堆栈跟踪:
java.sql.SQLException: ORA-00933: SQL command not properly ended
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
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:743)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:207)
at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:790)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1038)
at oracle.jdbc.driver.T4CStatement.executeMaybeDescribe(T4CStatement.java:830)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1133)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1273)
at testPackage.ConnectionTest.main(ConnectionTest.java:38)
编辑
现在,如果我将查询更改为:
String queryPtyRole = "select emp_id from emp where address_id in (select address_id from add where state_id in (1,2,3))";
它再次给出同样的错误。
【问题讨论】:
-
只需从查询中删除分号即可。请点击以下链接:stackoverflow.com/questions/28544688/…
-
请更正您的第一个错误 - “分号”(如其中一个答案中所述) - 因为它具有误导性。重述第二个问题的问题。确保您在进行更改后正确地重新编译(清理 - 构建)您的代码。经过上述所有操作后,您仍然遇到相同的错误吗?
-
感谢 Plirkee ...清理项目后,错误消失了。