Get the Oracle service name list using code

Source: Internet
Author: User

/*
Obtain the Oracle service name
1. Query the registry and obtain the Oracle Installation root directory
For example, HKEY_LOCAL_MACHINE \ SOFTWARE \ oracle \ ORACLE_HOME REG_SZ E: \ oracle \ ora92
Find the location of the Oracle service name file: root directory \ Network \ admin \ tnsnames. ora

2. Parse the file. The file structure is shown in figure
#------------------------------------------------
Portal =
(Description =
(Address_list =
(Address = (Protocol = TCP) (host = 134.104.52.6) (Port = 1521 ))
)
(CONNECT_DATA =
(SERVICE_NAME = portal)
)
)

3. Key points:
Retrieve and parse a row
Skip the comment line with the # hitting Header
After spaces are filtered out, the first character is ~ The line between Z contains the Oracle service name
Truncates the string on the left of the first "=" Number of the row. After trim is processed, it is the Oracle service name.
*/
Public static string [] getoracletnsnames ()
{
Try
{
// Query the registry and obtain the path of the Oracle service file
Registrykey key = registry. localmachine. opensubkey ("software"). opensubkey ("oracle ");
String home = (string) Key. getvalue ("ORACLE_HOME ");
String file = home + @ "\ Network \ admin \ tnsnames. ora ";

// Parse the file
String line;
Arraylist arr = new arraylist ();
Streamreader sr = new streamreader (File );
While (line = Sr. Readline ())! = NULL)
{
Line = line. Trim ();
If (line! = "")
{
Char c = line [0];
If (C> = 'A' & C <= 'Z ')
Arr. Add (line. substring (0, line. indexof ('')));
}
}
Sr. Close ();

// returns a string array
return (string []) Arr. toarray (typeof (string);
}< br> catch (exception ex)
{< br> return NULL;
}< BR >}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.