Java - SQLite 连接(找不到适用于 JDBC:sqlite:main.db 的驱动程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43161060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:09:13  来源:igfitidea点击:

Java - SQLite connection (No suitable driver found for JDBC:sqlite:main.db)

javasqlitejdbc

提问by Jakub Gadawski

I have problem with SQLite connection on my Java project. Error looks like this :

我的 Java 项目上的 SQLite 连接有问题。错误如下所示:

No suitable driver found for JDBC:sqlite:main.db

找不到适合 JDBC:sqlite:main.db 的驱动程序

That's my code:

那是我的代码:

public static void main(String[] args)  {

    Connection c = null;
    try {
      //  Class.forName("org.sqlite.JDBC");
        String url = "JDBC:sqlite:main.db";
        c = DriverManager.getConnection(url);
        System.out.println("Connection to sql");
    } catch ( SQLException e ) {
        System.err.println( e.getMessage() );
    } finally {
        try{
            if( c!= null ) {
                c.close();
            }
        }catch( SQLException ex )
        {
            System.out.println(ex.getMessage());
        }
    }
}

Can You help me please?

你能帮我吗?

回答by Babatunde Adeyemi

If you're using maven, ensure that the scopeisn't specified as test.i.e.

如果您使用的是 maven,请确保scope未将其指定为 test.ie

<dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.18.0</version>
</dependency>

回答by duffymo

"No suitable driver" means that the connection URL is incorrect for the JDBC driver JAR that was loaded.

“没有合适的驱动程序”表示已加载的 JDBC 驱动程序 JAR 的连接 URL 不正确。

Case matters: it should be jdbc:sqlite:main.db. Please read the tutorial.

案例很重要:应该是jdbc:sqlite:main.db。请阅读教程

回答by Anant666

I think you have not added the SQLITE JDBC driver to your classpath. Just download the jar FROM HEREand add it to your classpath. Your error would be resolved.

我认为您还没有将 SQLITE JDBC 驱动程序添加到您的类路径中。只需从这里下载 jar并将其添加到您的类路径中。您的错误将得到解决。

You should also have a look on This answerand This resource. By reading both of them you will learn more about SQLITE as well as how to make a connection with JDBC.

您还应该查看此答案此资源。通过阅读这两篇文章,您将了解更多关于 SQLITE 以及如何与 JDBC 建立连接的知识。