Skip to content

无法找到驱动名称 #99

Description

@Dr-SummerFlower

你好,我在使用这个项目的时候遇到了问题Could not find class com.mysql.jdbc.Driver

我已经排查了我这边的大部分问题(比如mysql版本,jdbc驱动包的版本,文件路径等问题,我可以确定我所使用的jdbc驱动包可以正确的连接到mysql,但是我却无法在node中使用这个驱动包连接到mysql)

java类的路径我尝试使用com.mysql.cj.jdbc.Drivercom.mysql.jdbc.Driver,可是都失败了

这是我的代码

import * as path from 'path';
import { isJvmCreated, JDBC, setupClasspath } from 'nodejs-jdbc';

const main = async () => {
    if (!isJvmCreated()) {
        setupClasspath([
            path.join(
                path.resolve(),
                './drivers',
                'mysql-connector-j-8.2.0.jar',
            ),
        ]);
    }

    const config: any = {
        url: 'jdbc:mysql://localhost:3306/',
        user: 'root',
        password: '123456',
        minpoolsize: 1,
        maxpoolsize: 5,
        maxidle: 1000 * 60,
        drivername: 'com.mysql.cj.jdbc.Driver',
        properties: {},
    };

    const db: JDBC = new JDBC(config);

    await db.initialize();
    // const connobj = await db.reserve();
    // const { conn } = connobj;
    // const statement = await conn.createStatement();
    // const result = await statement.executeQuery('SELECT * FROM test.images');
    // await db.release(connobj);
    // console.log(result);
};

main();
[nodemon] starting `ts-node ./src/main.ts`
Error: Could not find class com.mysql.cj.jdbc.Driver
java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/Driver
Caused by: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:352)

    at node:internal/util:430:7
    at new Promise (<anonymous>)
    at Java.newInstance [as newInstancePromise] (node:internal/util:416:12)
    at JDBC.initialize (C:\Users\17218\Desktop\node_jdbc\node_modules\nodejs-jdbc\src\Pool.ts:222:33)
    at main (C:\Users\17218\Desktop\node_jdbc\src\main.ts:41:14)
    at Object.<anonymous> (C:\Users\17218\Desktop\node_jdbc\src\main.ts:50:1)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module.m._compile (C:\Users\17218\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\17218\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1621:12) {
  cause: nodeJava_java_lang_NoClassDefFoundError {}
}
[nodemon] app crashed - waiting for file changes before starting...

另外我还尝试了sqlite(https://github.com/xerial/sqlite-jdbc)

/**
 * @File: main.ts
 * @Author: 夏花
 * @Date: 2024-07-28
 */

import * as path from 'path';
import { isJvmCreated, JDBC, setupClasspath } from 'nodejs-jdbc';

const main = async () => {
    if (!isJvmCreated()) {
        setupClasspath([
            path.join(path.resolve(), './drivers', 'sqlite-jdbc-3.46.0.0.jar'),
            path.join(path.resolve(), './drivers', 'slf4j-api-1.7.36.jar'),
        ]);
    }

    const config: any = {
        url: 'jdbc:sqlite:sample.db',
        user: 'SA',
        password: '',
        drivername: 'org.sqlite.JDBC',
        // user: 'root',
        // password: '123456',
        // minpoolsize: 1,
        // maxpoolsize: 5,
        // maxidle: 1000 * 60,
        // drivername: 'com.mysql.cj.jdbc.Driver',
        // properties: {},
    };

    const db: JDBC = new JDBC(config);

    await db.initialize();
    // const connobj = await db.reserve();
    // const { conn } = connobj;
    // const statement = await conn.createStatement();
    // const result = await statement.executeQuery('SELECT * FROM test.images');
    // await db.release(connobj);
    // console.log(result);
};

main();
[nodemon] starting `ts-node ./src/main.ts`
Error: Could not find class org.sqlite.JDBC
java.lang.NoClassDefFoundError: org/sqlite/JDBC
Caused by: java.lang.ClassNotFoundException: org.sqlite.JDBC
        at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:352)

    at node:internal/util:430:7
    at new Promise (<anonymous>)
    at Java.newInstance [as newInstancePromise] (node:internal/util:416:12)
    at JDBC.initialize (C:\Users\17218\Desktop\node_jdbc\node_modules\nodejs-jdbc\src\Pool.ts:222:33)
    at main (C:\Users\17218\Desktop\node_jdbc\src\main.ts:34:14)
    at Object.<anonymous> (C:\Users\17218\Desktop\node_jdbc\src\main.ts:43:1)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module.m._compile (C:\Users\17218\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\17218\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1621:12) {
  cause: nodeJava_java_lang_NoClassDefFoundError {}
}
[nodemon] app crashed - waiting for file changes before starting...

我非常希望这个项目能够正常运行(因为一些原因,我需要尝试连接到一些冷门的数据库,但是他们并没有提供nodejs的连接方法;但是正好提供了jdbc驱动,所以我找到了这个项目,尝试使用它先连接到mysql作为测试,但是我失败了。),请帮帮我。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions