最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Connecting to Oracle database with JavaScript - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question edited Aug 17, 2019 at 19:58 Dharman 33.5k27 gold badges101 silver badges149 bronze badges asked Jul 18, 2018 at 12:37 Sairam YadavSairam Yadav 111 silver badge5 bronze badges 7
  • ActiveXObjects are basically deprecated. Those won't work (reliably) any more on modern browsers. – Cerbrus Commented Jul 18, 2018 at 12:40
  • Yeah, i came to know that. is there any solution to connect with database through javascript? – Sairam Yadav Commented Jul 18, 2018 at 12:56
  • With only JavaScript? I doubt it. – Cerbrus Commented Jul 18, 2018 at 12:57
  • 2 Set up Oracle REST data services and connect to that. – MT0 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? – MT0 Commented Jul 18, 2018 at 13:09
 |  Show 2 more ments

2 Answers 2

Reset to default 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 pletely 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.

发布评论

评论列表(0)

  1. 暂无评论