我试图连接到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))";
它再次给出了同样的错误。
答案 0 :(得分:1)
尽量不要在命令末尾使用分号:
String queryPtyRole = "select * from emp";
这应该有用。