The connection operation Oracle database instance in JavaScript

  • 2020-05-24 05:12:50
  • OfStack

preface

In both b/s and c/s development, javascript is largely not used to operate on databases. I have confirmed my idea that a large amount of news information needs to be added to the database. Therefore, I want to get information from various rss sites and import the information into the database. I could have chosen to use a compiled language like java, c++, or c #, but using javascript was the most efficient way to do this. So why shouldn't I?

The environment

Operating system: winxp sp2
Tools to use: cscript.exe, batch file
Database: oracle 10g as the target database (other databases can also be used, such as sqlserver, access, mysql, etc.)
Data access method: ADO (other data access methods can also be used, such as odbc, jdbc, etc.)

Code:

File name: rss.js


// Create the database connection object
var conn = new ActiveXObject("ADODB.Connection");
// Create a data set object
var rs = new ActiveXObject("ADODB.Recordset"); try{
// Database connection string, please refer to: http://www.connectionstrings.com/
// If you don't know how to configure the connection string, can you configure it UDL Open the file with a text editor to get it
var connectionstring = "Provider=OraOLEDB.Oracle.1;Password=pwd;Persist Security Info=True;User ID=username;Data Source=ORA"; // Open the connection
conn.open(connectionstring);
 
// The query
var sql = " select * from tb_col "; // Open the dataset (that is, execute the query)
rs.open(sql,conn); // Traverse all records
while(!rs.eof){
  //WScript is Windows The script host object, please refer to the details in windows Look it up in help.
  //WScript.Echo Output the contents of the record
  WScript.Echo(rs.Fields("id") + "\t" + rs.Fields("name") + "\n");
 
  // Under the 1 records
  rs.moveNext();
} // Close recordset
rs.close();
// Close the database connection
conn.close();
} catch(e){
// Abnormality report
WScript.Echo(e.message);
} finally{
//
}

Document 2: run.bat

This file is a batch file, which is used to run the rss.js file. Although it is possible to run the rss.js file directly, it is activated by Windows. This has the disadvantage that a window will pop up for each record. So I used the command line to activate the rss.js file and the batch command to simplify the input of the command.


cscript.exe rss.js
pause

Run run.bat and you'll see something like 1:


1        The column 1 2        The column 2 3        The column 3 4        The column 4


Related articles: