3

I'm trying to connect my java application to my sqlite database but I'm getting this good old error. I'm using Maven and my setup looks like this:

project structure

I'm using IntelliJ and as far as I know Maven is well integrated. I'd like to know If I need to add the library somewhere else or put the jar file on the classpath.

I also leave here my pom.xml file with project definitions

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>

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

</project>

This is my connection class:

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!");
        }
    }

}
8
  • How are you running the app? from the command line? from the IDE? Commented Mar 31, 2017 at 1:01
  • Seems Intellij is not downloading the jar for you. You can do mvn clean install from command line and try. Commented Mar 31, 2017 at 2:08
  • If you look at the screenshot, IntelliJ is looking inside the JAR file - you can't do that if it's not downloaded, surely? Commented Mar 31, 2017 at 2:38
  • Okay. I missed the project structure screenshot. Yes indeed it's there. Commented Mar 31, 2017 at 3:54
  • I'm running from the command line @Catchwa . Why? Commented Apr 3, 2017 at 11:38

3 Answers 3

3

Try using 3.15.1.

I had the same problem. I tried eclipse + maven / command line but ClassNotFoundException was thrown with 3.16.1. But with 3.15.1, no problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You will have to add sqlite-jdbc-3.16.1.jar into classpath. In Intellij, going to module settings you can add. You can download from : https://bitbucket.org/xerial/sqlite-jdbc/downloads/

I tried the below code and worked fine.

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());
        }
    }
}

}

2 Comments

If he's using Maven he shouldn't have to download anything manually
yes, it should work using maven and his pom looks good. But seems Intellij not downloading the jar in his case.
0

I just had the same problem (btw also happens with different drivers):

So the thing is that maven downloads the jar, but it does not put in your classpath

What I did in order to avoid to have to do this manually is packaging my application into a single jar - driver included

I have done this with the plugin:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>path/to/MainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.