当我尝试使用以下代码从Oracle数据库读取数据时,出现异常
ResultSet res=stmt.executeQuery("select * from food");
但是,当我直接在命令提示符下使用此命令时,此表实际上存在于我的数据库中。并且对于数据库中表中的一个表,此代码也可以正常工作,但对于其他表名,其代码无法正常工作。有人请解释为什么会这样。
java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
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.T4CStatement.doOall8(T4CStatement.java:193)
at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:852)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1477)
at oracle.jdbc.driver.OracleStatementWrapper.executeQuery(OracleStatementWrapper.java:392)
at connecttooracle.ConnectOracle.main(ConnectOracle.java:67)
我正在从Toad访问食物表中的数据。为什么我在Java中遇到此错误?
我的完整代码是:
public class ConnectOracle {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver"; //
String serverName = "10.11.201.84";
String portNumber = "1521";
String db = "XE";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
+ db; // connectOracle is the data
// source name
String user = "ORAP"; // username of oracle database
String pwd = "ORAP"; // password of oracle database
Connection con = null;
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
Class.forName(driver);// for loading the jdbc driver
System.out.println("JDBC Driver loaded");
con = DriverManager.getConnection(url, user, pwd);// for
// establishing
// connection
// with database
Statement stmt = (Statement) con.createStatement();
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
while (true) {
try {
socket = serverSocket.accept();
System.out.println("Connection Created");
dataInputStream = new DataInputStream(
socket.getInputStream());
dataOutputStream = new DataOutputStream(
socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
// System.out.println("message: " +
// dataInputStream.readUTF());
ResultSet res=stmt.executeQuery("select * from food");
while(res.next()){
System.out.println(res.getString(1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最佳答案
如果您的表位于模式A下:
select * from A.food
编辑
如果您可以使用用户ORAP通过TOAD登录并执行相同的查询(从food中选择*),那么您肯定会将表包含在ORAP模式中。我没有理由“从ORAP.food中选择*”失败。