List TNS entries from TNSNAMES.ORA using java

I was searching for the java program which should List TNS entries from TNSNAMES.ORA using java. I Google this but not found any satisfactory answer. So I have written the below code to list the tns entries from TNSNAMES.ORA file.

  • What is TNSNAMES.ORA? TNSNAMES.ORA is a SQL*Net configuration file that defines databases addresses for establishing connections to them.
  • Requirement to Run this Program: To successfully run this program ORACLE_HOME parameter should be set in the Environmental Variables.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TNSNamesList {
	
	public static void main(String[] args) 
	{
		
		BufferedReader br = null;
		String pattern = "^([^#()\\W ][a-zA-Z0-9.]*(?:[.][a-zA-Z]*\\s?=)?)"; //Regular Expression Pattern for TNS Alias
		Pattern r = Pattern.compile(pattern);
		 
		try 
		{
 
			String sCurrentLine; 
			br = new BufferedReader(new FileReader(System.getenv("ORACLE_HOME")+"\\NETWORK\\ADMIN\\TNSNAMES.ORA")); 
			while ((sCurrentLine = br.readLine()) != null) 
			{
				//System.out.println(sCurrentLine);
				Matcher matcher = r.matcher(sCurrentLine);
				if(matcher.find())
				{					
					System.out.println(matcher.group().toUpperCase());				
				}
			}
 
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
		finally 
		{
			try 
			{
				if (br != null)br.close();
			} 
			catch (IOException ex) 
			{
				ex.printStackTrace();
			}
		}
	}	
}
List TNS entries from TNSNAMES.ORA using java
Tagged on:     

3 thoughts on “List TNS entries from TNSNAMES.ORA using java

  • March 17, 2016 at 11:52 am
    Permalink

    tnsnames.ora-Connections can actually have _ or . in their name 🙂

    Reply
  • October 21, 2017 at 9:47 am
    Permalink

    These is a simple way without RegEx to parse TNSNAMES:
    /**
    * File TNSNAMES.ORA consist of pairs TNS_ALIAS_NAME=(DESCRIPTION=(…)))
    * and comment lines starting with #.
    * You may use Oracle driver string like this:
    * Connection conn =
    * DriverManager.getConnection(“jdbc:oracle:thin:user/pass@(DESCRIPTION=(…)))”);
    * in place of “jdbc:oracle:thin:user/pass@TNS_ALIAS_NAME”
    * that, in my opinion, doesn’t work in NetBeans 8.2 !
    *
    * @author Dmitry Motyl
    */
    public class SimpleParser {
    Map map=new TreeMap();

    public void DoIt() {
    map.clear();

    BufferedReader br = null;
    try {
    String fileName = System.getenv(“TNS_ADMIN”) + “\\TNSNAMES.ORA”;
    File file = new File(fileName);
    if (!file.exists()) {
    JOptionPane.showMessageDialog(null, fileName + ” not found!”);
    return;
    }
    int fileSize = (int) file.length();
    StringBuffer buf = new StringBuffer(fileSize);
    String S;
    br = new BufferedReader(new FileReader(file));
    while ((S = br.readLine()) != null) {
    S=S.trim();
    if ((S.length() > 0)
    && !S.substring(0, 1).equals(“#”)) { //skip comment lines
    buf.append(S.replace(” “, “”).replace(“\t”, “”)); //remove white spaces
    }
    }
    S = buf.toString(); //parse ALIAS.WORLD=((…)(…(…)))
    int iStart = 0, iEqual = S.indexOf(‘=’); // |iStart |iEqual |last paired bracket
    while (iEqual > 0) {
    String xName = S.substring(iStart, iEqual).toUpperCase();
    int iCounter = 0;
    for (int j = iEqual + 1; j < S.length(); j++) {
    char ch = S.charAt(j);
    switch (ch) { //find a paired closing parenthesis
    case '(':
    iCounter++;
    break;
    case ')':
    iCounter–;
    break;
    default:
    break;
    }
    if (iCounter == 0) {
    iStart = j + 1; //next start of pair NAME=(DESCRIPTION=(…)))
    String xDescription = S.substring(iEqual + 1, iStart);
    map.put(xName,xDescription);
    iEqual = S.indexOf('=', iStart); //next position of equal sign
    break;
    }
    }
    }

    for (Map.Entry entry : map.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+” = “+value);
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (br != null) {
    br.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }

    Reply
  • January 4, 2018 at 1:36 pm
    Permalink

    Correction regarding the above comment:

    String pattern = “^([^#()\\W ][a-zA-Z0-9.\\-_]*(?:[.][a-zA-Z]*\\s?=)?)”; //Regular Expression Pattern for TNS Alias

    –ibrahim saygili

    Reply

Leave a Reply