Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

Tags:

java

oracle

when i was trying to read data from Oracle database using the following code i was getting exception

ResultSet res=stmt.executeQuery("select * from food");

But this table is actually exist in my database when i use this command directly in the command prompt its working fine.And also for one table among the tables in database this code is working fine ,but for other table names its not working properly.So someone please explain why this is happening.

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)

I am accessing data from food table by Toad . THen why am I getting this error in java ?

My full code is :

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();
  }
 }
}
like image 754
osimer pothe Avatar asked Apr 28 '16 10:04

osimer pothe


3 Answers

If your table is located under schema A:

select * from A.food

EDIT

If you can login via TOAD with user ORAP and execute the same query (select * from food) then you definitely have the table in ORAP schema. I see no reason for "select * from ORAP.food" to fail.

like image 72
Y.E. Avatar answered Nov 19 '22 19:11

Y.E.


Some of the possible solutions are:

You can create connection in the database by the schema under which database is located.

For example: My original database has admin SYSTEM and then I created an user Toad and under this Toad schema I have created the table Food.

So I will create connection to the database by:

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "Toad", "PasswordOfToad");

Then the query ResultSet res = stmt.executeQuery("select * from Food"); will work fine.

Alternatively you could specify the table by SchemaName.DatabaseName, which in this case is Toad and Food respectively.

So the query would look like:

ResultSet res = stmt.executeQuery("select * from Toad.Food");
like image 2
Pran Kumar Sarkar Avatar answered Nov 19 '22 19:11

Pran Kumar Sarkar


I had the same problem and something like this works for me:

ResultSet res=stmt.executeQuery("select * from \"food\"");
like image 1
Sandra Kuc Avatar answered Nov 19 '22 19:11

Sandra Kuc