Reputation: 11
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!
Upvotes: 1
Views: 7665
Reputation: 8304
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.
Upvotes: 1
Reputation: 5250
There are several issues to take into account
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.
Upvotes: 2