@naxmefy/jdbc
small wrapper to work with jdbc in node.
prerequisites
tested with node.js 24 and jdk 17.
node-java
$ npm install --save javaHINT
node-java installation can fail on windows
(i never got it work on windows).I used docker on windows to get it working.
You can find my example docker image on docker hub at
https://hub.docker.com/r/naxmefy/node-java/
or
https://github.com/naxmefy/docker-node-java
OPTIONAL: node-java-maven
$ npm install --save node-java-maveninstallation
$ npm install --save @naxmefy/jdbcusage
setup jdbc driver inside package.json
{
"name": "myapp",
"version": "0.0.0",
"private": true,
"java": {
"dependencies": [
{
"groupId": "com.h2database",
"artifactId": "h2",
"version": "1.4.195"
}
]
},
"dependencies": {
"@naxmefy/jdbc": "^0.1.0",
"java": "^0.18.0",
"node-java-maven": "^0.0.12"
}
}in your main file (js)
const JDBC = require('@naxmefy/jdbc').JDBC
const myDatabase = new JDBC({
className: 'any.jdbc.Driver',
url: 'jdbc:to:any.db',
username: 'foo',
password: 'bar'
})
myDatabase.createStatement()
.then(statement => {
return statement.executeQuery('SELECT * FROM FOO')
})
.then(resultSet => {
const arrayOfResults = resultSet.fetchAllResults()
arrayOfResults.forEach(result => {
console.log(result)
})
})or with typescript (types included)
import {JDBC, ResultSet, Statement} from '@naxmefy/jdbc'
const myDatabase = new JDBC({
className: 'any.jdbc.Driver',
url: 'jdbc:to:any.db',
username: 'foo',
password: 'bar'
})
myDatabase.createStatement()
.then((statement: Statement) => {
return statement.executeQuery('SELECT * FROM FOO')
})
.then((resultSet: ResultSet) => {
const arrayOfResults = resultSet.fetchAllResults()
arrayOfResults.forEach(result => {
console.log(result)
})
})usage with Docker
HINT
The following docker config contains proxy config too.
Maybe you do not need it.
example Dockerfile
FROM naxmefy/node-java
WORKDIR /usr/app
COPY package.json .
RUN npm config set proxy $HTTP_PROXY
RUN npm config set https-proxy $HTTPS_PROXY
RUN npm install --quiet
COPY . .example docker-compose.yml
version: '2'
services:
web:
build:
context: .
args:
- http_proxy
- https_proxy
- no_proxy
- HTTP_PROXY
- HTTPS_PROXY
- NO_PROXY
command: npm start
volumes:
- .:/usr/app/
- /usr/app/node_modules
ports:
- "3000:3000"
links:
- postgres
environment:
NODE_ENV: development
DEBUG: @naxmefy/jdbc
postgres:
image: postgres
environment:
POSTGRES_USER: pguser
POSTGRES_PASSWORD: pgpass
POSTGRES_DB: pgdb
ports:
- "5432:5432"example java maven config
{
"java": {
"dependencies": [
{
"groupId": "org.postgresql",
"artifactId": "postgresql",
"version": "42.1.1"
}
]
}
}example jdbc instantiation
const myPostgresDB = new JDBC({
className: 'org.postgresql.Driver',
url: 'jdbc:postgresql://postgres:5432/pgdb',
username: 'pguser',
password: 'pgpass'
})Contributing
- open issues for bugs or whatever
- fork repo, make change, start pull request
License
MIT