简体   繁体   English

使用JavaScript连接到Oracle数据库

[英]Connecting to Oracle database with JavaScript

I want to connect to Oracle database through JavaScript code. 我想通过JavaScript代码连接到Oracle数据库。

<!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! 我收到未定义ActiveXObject的错误ActiveXObject不适用于Chrome浏览器!

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 您的数据库端口和URL必须可以通过浏览器访问

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. 为了避免某些风险,我认为最好的方法(如果您仍然想避免服务器代码)是使用oracle提供的Web服务。 There are several examples in oracle docs using SOAP and REST, here an example using REST . 在oracle docs中有一些使用SOAP和REST 的示例这里是使用REST的示例 Once you have the resource created, you call that resource using ajax. 一旦创建了资源,就可以使用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. 为了防止浏览器在尝试执行跨源Ajax时造成的限制,应在数据库服务器中设置Access-Control-Allow-Origin标头。 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". 您无法直接从浏览器进行连接,但是可以使用npm模块“ oracledb”在NodeJS中使用JavaScript进行连接。 I have been using it in production for about 3 years now -- it works really well. 我已经在生产中使用了大约3年的时间-它确实运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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