oracledb_npm包的使用
1.使用需求
通过Node.js搭建服务端应用,直接操作oracle数据库,实现数据库的连接以及增删改查等操作。
2.开发环境
- Windows 10
- Oracle服务端
11g
- Oracle客户端
instantclient-basic-windows.x64-12.1.0.2.0
- Node.js
v14.2.0-x64
- oracledb
V4.2.0
- Visual Studio 2010 Redistributable
3.环境搭建
参考链接:https://oracle.github.io/node-oracledb/INSTALL.html#instwin
- 安装Node.js,默认安装即可;
- 在某一文件夹下(以Node_OracleDB为例),使用
npm init
初始化项目;
- 通过命令
npm install oracledb --save
,安装oracledb包; - 下载并配置免安装版的oracle客户端,**注意:**要和Node.js架构匹配,目前安装的node.js为64位,因此要配置64位oracl客户端,另外注意要选择与oracle服务端版本匹配的客户端版本;
In summary, Oracle Client 19, 18 and 12.2 can connect to Oracle Database 11.2 or greater. Oracle Client 12.1 can connect to Oracle Database 10.2 or greater. Oracle Client 11.2 can connect to Oracle Database 9.2 or greater. The technical restrictions on creating connections may be more flexible. For example Oracle Client 12.2 can successfully connect to Oracle Database 10.2.
针对Oracle服务端11g下载的是instantclient-basic-windows.x64-12.1.0.2.0
,之后配置环境变量,在path
环境变量中添加客户端文件路径,注意放到path
的最前面。
- 根据Oracle客户端版本选择合适的
Visual Studio Redistributables
进行安装
4.示例程序测试
4.1 下载示例程序
到https://github.com/oracle/node-oracledb
克隆整个项目,将其中的examples
文件夹拷贝至环境搭建时的Node_OracleDB
文件夹下
4.2 修改数据库连接配置
在examples
文件夹下有dbconfig.js
文件,修改其中的用户名、密码、连接字符串等配置
4.3 运行
执行命令node .\examples\example.js
,看到控制台输出下面数据说明数据库操作成功。
5.参考链接
6. 资源下载
代码文件已上传到: https://download.csdn.net/download/wml00000/12464941