1

I want to connect to Oracle database through JavaScript code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
    <title>Connecting to Oracle using JavaScript</title>
</head>

<body>
    <script language="JavaScript" type="text/javascript">
        <!--
        var conObj = new ActiveXObject('ADODB.Connection');

        var connectionString = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.119.132.175)(PORT=1521)))(CONNECT_DATA=(SID=orcl)(SERVER=DEDICATED)));User Id=mdm;Password=admin;"

        conObj.Open(connectionString);
        var rs = new ActiveXObject("ADODB.Recordset");
        sql = "SELECT SYSDATE FROM DUAL"

        rs.Open(sql, conObj);
        alert(rs(0));
        rs.close;
        conObj.close;
        //-->
    </script>
</body>

</html>

I am getting ActiveXObject is not defined error ActiveXObject doesn't work for chrome browser it seems!

7
  • ActiveXObjects are basically deprecated. Those won't work (reliably) any more on modern browsers. Commented Jul 18, 2018 at 12:40
  • Yeah, i came to know that. is there any solution to connect with database through javascript? Commented Jul 18, 2018 at 12:56
  • With only JavaScript? I doubt it. Commented Jul 18, 2018 at 12:57
  • 2
    Set up Oracle REST data services and connect to that. Commented Jul 18, 2018 at 13:08
  • 1
    Are you really putting an full connection details including username and password into a script that is going to be sent your users? Commented Jul 18, 2018 at 13:09

2 Answers 2

2

There are several issues to take into account

  • You need a driver in the client to deal with a database
  • Since you want to connect from client your credentials must be present in client side.
  • Your database port and url must be accessible from a browser

All of those issues implies that your database will be completely exposed to anyone.

To avoid some of the risks, in my opinion the best approach (if you still want to avoid server code) is using web services provided by oracle. There are several examples in oracle docs using SOAP and REST, here an example using REST. Once you have the resource created, you call that resource using ajax. To prevent the restriction made by browser when you attempt to perform a cross origin ajax, you should set a Access-Control-Allow-Origin header in the database server. In this way you could access to the database without server code.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't connect directly from the browser, but you can connect using javascript in NodeJS, using the npm module "oracledb". I have been using it in production for about 3 years now -- it works really well.

1 Comment

Hi Nick, I was trying to connect to an oracle instance using nodejs code and followed foll steps: download and unzip instant client, set the path in LD_LIBRARY_PATH, installed node-oracledb, wrote test connection code and ran it, however, I am not able to connect to the database. The request is timing out without any error. Are there any more steps according to you to connect to oracle from linux machine with nodejs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.