java.lang.ClassNotFoundException:org.sqlite.JDBC IntelliJ + Maven

时间:2017-03-30 23:37:32

标签: java sqlite maven intellij-idea

我试图将我的java应用程序连接到我的sqlite数据库,但我收到了这个老错误。我使用Maven,我的设置如下:

project structure

我使用IntelliJ,据我所知,Maven已经很好地集成了。我想知道我是否需要在其他地方添加库或将jar文件放在类路径上。

我也留下了带有项目定义的pom.xml文件

package database;

import java.io.File;
import java.io.FileNotFoundException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by luiscosta on 3/29/17.
 */
public class DBConnection {

    private Connection conn;
    private String dbPath;


    /**
     * Constructor
     */
    public DBConnection(String name, String folder) throws FileNotFoundException {

        File dbDir = new File(folder);

        if (dbDir.isDirectory() && dbDir.exists()) { //confirms the database directory exists
            if (folder.charAt(folder.length() - 1) == '/') { //checks if the directory ends in /
                this.dbPath = folder + name;

            } else {
                this.dbPath = folder + '/' + name; //if not adds it in the end of the folder pah
            }


            //Connection

            try {
                Class.forName("org.sqlite.JDBC");

                System.out.println("db Path" + this.dbPath);
                this.conn = DriverManager.getConnection("jdbc:sqlite:ChunkMetaData");

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new FileNotFoundException("Directory not found!");
        }
    }

}

这是我的连接类:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Not Intersect(Target, Range("H1:H20")) Is Nothing Then

        Target.Offset(0, 1) = Mid(Target.Value, 1, 5)

    End If

    Application.EnableEvents = True

End Sub

2 个答案:

答案 0 :(得分:1)

尝试使用3.15.1。

我遇到了同样的问题。我尝试了eclipse + maven /命令行,但是在3.16.1中抛出了ClassNotFoundException。但是3.15.1,没问题。

答案 1 :(得分:0)

您必须将sqlite-jdbc-3.16.1.jar添加到classpath中。在Intellij中,转到模块设置,您可以添加。您可以从:https://bitbucket.org/xerial/sqlite-jdbc/downloads/

下载

我尝试了以下代码并且工作正常。

public class SqliteConnectionTry {

   public static void main(String[] args) {
      connect();
  }

   public static void connect() {
      Connection conn = null;
      try {
          // db parameters
           String url = "jdbc:sqlite::memory";
          // create a connection to the database
           conn = DriverManager.getConnection(url);

           System.out.println("Connected!!");

         } catch (SQLException e) {
            System.out.println(e.getMessage());
         } finally {
            try {
               if (conn != null) {
                  conn.close();
               }
         } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

}