-4
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    public class Main {

      public static void main(String[] args) throws Exception {
        Connection conn = getOracleConnection();
        Statement stmt = null;
        ResultSet rs = null;
        stmt = conn.createStatement();
        //only for Oracle
        rs = stmt.executeQuery("select object_name from user_objects where object_type = 'TABLE'");

        while (rs.next()) {
          String tableName = rs.getString(1);
          System.out.println("tableName=" + tableName);
        }

        stmt.close();
        conn.close();
      }

      /*private static Connection getHSQLConnection() throws Exception {
        Class.forName("org.hsqldb.jdbcDriver");
        System.out.println("Driver Loaded.");
        String url = "jdbc:hsqldb:data/tutorial";
        return DriverManager.getConnection(url, "sa", "");
      }*/

     /* public static Connection getMySqlConnection() throws Exception {
        String driver = "org.gjt.mm.mysql.Driver";
        String url = "jdbc:mysql://localhost/demo2s";
        String username = "oost";
        String password = "oost";
    */
       /* Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
      }
    */
      public static Connection getOracleConnection() throws Exception {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:Test";
        String username = "system";
        String password = "root12345";

        Class.forName(driver); // load Oracle driver
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
      }

    }

I'm going to get the list of tables from Test database but I'm getting the error like listener refused connection, Listener refused the connection with the following error as:

ORA-12505, TNS: listener does not currently know of SID given in connect descriptor

But when I change Test to Orcl then its working fine but I want to select the table from particular Database.

Where did I go wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Manju Badiger
  • 11
  • 1
  • 5
  • Pro-tip: please keep your posts succinct and free of chat and txtspk. "Plz help me" is a downvote magnet, partly because it's not English, and partly because your readers may interpret it as a form of begging and pleading. – halfer Aug 06 '18 at 14:41
  • Note we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Aug 06 '18 at 14:42
  • This has been tidied up once before, to fix case, grammar and trim chat and please-help pleading. It looks like you replaced this with an entirely new question, which would invalidate the help you received below. Please do not do that! If you have a new question, then ask a new question. If you are prevented from asking new questions (because of the poor reception of your existing questions) then you will need to take steps to remedy that. Do not destroy your old questions please. – halfer Aug 07 '18 at 09:32
  • The instructions on rescuing banned accounts [are here](https://meta.stackoverflow.com/questions/255583/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th). – halfer Aug 07 '18 at 09:38

1 Answers1

0

Best way to solve problem is first read the error or exception you got ORA-12505, TNS: listener does not currently know of SID In this error SID means In short the unique name of your DB.

When you're changing it test it simply means it does not available on current DB url or that SID(DB) does not exists you need to create new with same name

or if SID is available then check your URL for that SID

edit 1: To find SID you can use select * from global_name; it will give you one value use it as it is . I think you need to use test.world

make this changes to url

String url = "jdbc:oracle:thin:@localhost:1521/Test";

or

String url = "jdbc:oracle:thin:@localhost:1521/Test.world";
v8-E
  • 1,077
  • 2
  • 14
  • 21
  • Ok, for instance, my DB name is Test so how can i check my SID(DB) – Manju Badiger Aug 06 '18 at 10:33
  • @ManjuBadiger pls check updated answer, you need to use this query on your db – v8-E Aug 06 '18 at 10:56
  • select * from global_name; it returns "ORCL.MSHOME.NET", I have used this String url = "jdbc:oracle:thin:@localhost:1521/Test.world"; but still getting same exception – Manju Badiger Aug 06 '18 at 12:23
  • @ManjuBadiger bro that is your SID use it or create new DB with name `test` and create table in it. That query gave name of your SID(Db) name which is available – v8-E Aug 06 '18 at 12:34
  • Brother if I use SID (ORCL.MSHOME.NET")then its return with all tables from different Database but I want to list tables only from "Test" database so how can I use URL so that i can get tables which is present in test DB only. – Manju Badiger Aug 06 '18 at 12:40