to establish database connection using UNIX shell script

Neethu Shaji

I am new to shell scripting and have less or no idea on this. I have to read a db.properties file which has the database connection details i.e. to which db to connect. Then i have to establish a connection to that database and perform an operation to check the current time.

Below is my db.properties file :-

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@171.01.187.94:1532:DEV
userName=abc
password=abc

Below is my script to call the db.properties file :-

#!/bin/bash

file="./database.properties"

if [ -f "$file" ]
then
    echo "$file found."
 . $file

  echo "User Id       = " ${userName}
  echo "user password = " ${password}
  echo "url  = " ${url}

  sqlplus -S ${userName}/${password}@${url}

else
    echo "$file not found."
fi

But i am getting the below error :-

ERROR: ORA-12154: TNS:could not resolve the connect identifier specified

Could anyone please help on the above issue ?

Daniel Vukasovich

Don't worry about tnsnames.ora definition, you have all information needed to establish a connection using sqlnet.

Modify your database.properties file as follows:

driverClassName=oracle.jdbc.driver.OracleDriver
url='(description=(address_list=(address=(protocol=TCP)(host=171.01.187.94)(port=1532)))(connect_data=(service_name=DEV)))'
userName=abc
password=abc

And that's it. You don't need to change your script.

#!/bin/bash

file="./database.properties"

if [ -f "$file" ]
then
    echo "$file found."
 . $file

  echo "User Id       = " ${userName}
  echo "user password = " ${password}
  echo "url  = " ${url}

sqlplus -S ${userName}/${password}@${url}

else
    echo "$file not found."
fi

Note: I assumed that DEV is the database service name, if it's the database SID just modify configuration string as:

driverClassName=oracle.jdbc.driver.OracleDriver
url='(description=(address_list=(address=(protocol=TCP)(host=171.01.187.94)(port=1532)))(connect_data=(sid=DEV)))'
userName=abc
password=abc

Regards

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to establish a connection to a database using TCP?

From Dev

Establish connection to a database

From Dev

Establish a connection to another database only in a block?

From Dev

How to establish the connection to database in PHP Laravel Lumen?

From Dev

Unable to establish a connection to a database of SQL Server

From Dev

Java - Trying to establish a connection with a remote SQL Database

From Dev

Establish MySQL database connection from netbeans 7.4

From Dev

Ofbiz: Unable to establish a connection with the database for helperName [localmysql]

From Dev

Cannot establish an ActiveRecord connection in my ruby script

From Dev

Arithmetic in Unix shell Script

From Dev

Unix Shell Script

From Dev

Shell Script - Unix

From Dev

How to connect to postgresql database using shell script

From Dev

unable to connect the mysql database using shell script

From Dev

Shell script, linux, unix, shell

From Dev

UNIX - formatting another shell script - using awk in sed

From Dev

How to first login using sudo and then run rest of Unix Shell script

From Dev

How to establish socket connection using the command line

From Dev

How to establish MySQL connection using JDBC?

From Dev

Establish a Connection with a Server using Android app

From Dev

vlookup using UNIX shell

From Dev

Unix Shell Script: While loop

From Dev

Unix shell script - purpose of colon

From Dev

Beginner Unix shell script issues

From Dev

unix shell script to check for EOF

From Dev

Beginner Unix shell script issues

From Dev

missing `]' error in unix shell script

From Dev

Nested expression in a UNIX shell script

From Dev

How to establish secure connection string from on premises to SQL Azure Database

Related Related

HotTag

Archive