简体   繁体   中英

Connecting to Oracle database with JavaScript

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM