Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite jdbc Eclipse database relative path

I'm trying to use Sqlite, jdbc under Eclipse Luna & Windows 7.

Everything works fine when I use absolute path to the Sqlite database but when relative path used I get this error:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database.

I spent some time Googling this problem and the answer is: Yes you can use relative path with jdbc connection. However it does not work for me.

My code:

package PortiaMoxy;
import static net.mindview.util.Print.*;
String inPath; // incoming file
String outPath; // converted file from incoming file
public File() {
    // Connect to Sqlite db
    Connection c = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        Class.forName("org.sqlite.JDBC");
        // 
        // This connection works fine
        // 
        //c = DriverManager.getConnection("jdbc:sqlite:/JavaProjects/workspace/Polymorphism/sources/PortiaMoxy/moxyimport.sqlite");


        //
        // This connection doesn't work. ???
        //
        c = DriverManager.getConnection("jdbc:sqlite:moxyimport.sqlite");

        c.setAutoCommit(false);
        print("File(): Opened database successfully.");
        stmt = c.createStatement();


        String query = "select ID \"id\", VALUE \"value\"";
        query += "from infiles;";

        rs = stmt.executeQuery(query);

        if (!rs.isBeforeFirst() ) {    
             System.out.println("No data"); 
            } 


        while(rs.next()) {
            String id = rs.getString(1);
            String value = rs.getString(2);
            print("   ID = " + id + " Value = " + value);
        } // end of while
        rs.close();
        stmt.close();
        c.close();



    } // end of try
    catch (SQLException ex) {
        print(ex.getClass().getName() + ": " + ex.getMessage());
        System.exit(0);
    }
    catch (Exception e ) {
        print(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }



} // end of class

public int convert() {
    print(what());

    print("Generic convert");
    return 0;

}

public String what() {
    return "File";
}

}
like image 863
Michael Bee Avatar asked Jul 24 '15 13:07

Michael Bee


People also ask

Where do I put JDBC jar sqlite?

To specify where the sqlite. jar file containing the high-level part and the JDBC driver shall be installed, use the --with-jardir=DIR option. The default is /usr/local/share/java. At runtime, it is necessary to tell the JVM both places with the -classpath and -Djava.

Does JDBC support sqlite?

SQLite JDBC is a library for accessing SQLite databases through the JDBC API.


3 Answers

Set the Relative path in sqlite jdbc Database connectivity using Eclipse

1) Go to Run menu and select Run Configurations

2) Select the second tab (x)=Arguments

3) in Last Panel or section name is Working directory

4) click other Radio button

5) click the File System or Workspace Button and set your project Name

6) click the apply button

//Import the database in your project workspace like 'Demo.db'
Connection conn = null;

public static void connect()
{
    File dbfile=new File(".");
    String url="jdbc:sqlite:"+dbfile.getAbsolutePath()+"\\Demo.db";
    System.out.println(url);
    try {
          Class.forName("org.sqlite.JDBC");
          conn = DriverManager.getConnection(url);

    } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }

    System.out.println("Connection successfully");
}//end connection()
like image 27
Chetan Bhagat Avatar answered Oct 01 '22 00:10

Chetan Bhagat


Put your database within the following folder and try again with relative path:

/JavaProjects/workspace/Polymorphism

This should work.

like image 181
PWPlayDE Avatar answered Oct 01 '22 00:10

PWPlayDE


The problem is that without the full path, jdbc will try to locate the database in the current path.

I am not completely sure, but I think this should be the root of the project.

So you can put the SQLite file in this directory or use the path

like image 33
Jon Rios Avatar answered Oct 01 '22 01:10

Jon Rios