6

What do I have to do to avoid error of "out of memory", when connection by jdbc with sqlite3 database?

java.sql.SQLException: out of memory
at org.sqlite.DB.throwex(DB.java:288)
    at org.sqlite.NestedDB._open(NestedDB.java:73)
    at org.sqlite.DB.open(DB.java:77)
    at org.sqlite.Conn.<init>(Conn.java:88)
    at org.sqlite.JDBC.connect(JDBC.java:64)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at action.Actions.<init>(Actions.java:18)
    at controler.ClientControler.<init>(ClientControler.java:14)
    at main.Main.main(Main.java:20)


Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:clients.db");
3
  • How big is the database and what are your jvm memory settings (-Xmx)
    – beny23
    Aug 14, 2011 at 23:11
  • This database has 4kB, so I think it is other problem. I read somewhere, it can be becouse driver can't find file, but I put file in diffrent folders, it didn't help... Maybe I put to wrong folder...
    – kspacja
    Aug 14, 2011 at 23:18
  • I also have this problem, it is real.
    – gd1
    Sep 8, 2011 at 22:56

8 Answers 8

5

This suggests that your clients.db file couldn't be found. Try locating that file more appropriately. Scroll down to the section entitled "How to Specify Database Files".

I downloaded the SQLite JAR, put it in my CLASSPATH, and found a tutorial here that worked perfectly in less than five minutes. It put test.db in my project root, as expected.

I've rewritten that tutorial the way I'd do it. It works. Don't say it brings nothing.

package sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Test
{

    private static final String DEFAULT_DRIVER = "org.sqlite.JDBC";
    private static final String DEFAULT_URL = "jdbc:sqlite:data/test.db";

    public static void main(String[] args)
    {
        Connection conn = null;
        try
        {
            conn = createConnection(DEFAULT_DRIVER, DEFAULT_URL);
            createTable(conn);

            List<Person> people = new ArrayList<Person>();
            people.add(new Person("Gandhi", "politics"));
            people.add(new Person("Wittgenstein", "philosophy"));
            people.add(new Person("Turing", "computers"));
            saveAll(conn, people);

            List<Person> rows = findAll(conn);
            System.out.println(rows);
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            close(conn);
        }
    }

    private static List<Person> findAll(Connection conn) throws SQLException
    {
        List<Person> rows = new ArrayList<Person>();
        ResultSet rs = null;
        Statement stat = null;

        try
        {
            stat = conn.createStatement();
            rs = stat.executeQuery("select * from people;");
            while (rs.next())
            {
                rows.add(new Person(rs.getString("name"), rs.getString("occupation")));
            }
        }
        finally
        {
            close(stat);
            close(rs);
        }

        return rows;
    }

    private static void saveAll(Connection conn, List<Person> people) throws SQLException
    {
        PreparedStatement prep = null;
        try
        {
            prep = conn.prepareStatement("insert into people values (?, ?);");

            for (Person person : people)
            {
                prep.setString(1, person.getName());
                prep.setString(2, person.getOccupation());
                prep.addBatch();
            }

            conn.setAutoCommit(false);
            prep.executeBatch();
            conn.setAutoCommit(true);
        }
        finally
        {
            close(prep);
        }
    }

    private static void createTable(Connection conn) throws SQLException
    {
        Statement stat = null;
        try
        {
            stat = conn.createStatement();
            stat.executeUpdate("drop table if exists people;");
            stat.executeUpdate("create table people (name, occupation);");
        }
        finally
        {
            close(stat);
        }
    }

    private static Connection createConnection(String driver, String url) throws ClassNotFoundException, SQLException
    {
        Class.forName(DEFAULT_DRIVER);
        Connection conn = DriverManager.getConnection(DEFAULT_URL);

        return conn;
    }

    private static void close(Connection conn)
    {
        try
        {
            if (conn != null)
            {
                conn.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    private static void close(Statement stat)
    {
        try
        {
            if (stat != null)
            {
                stat.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void close(ResultSet rs)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

class Person
{
    private String name;
    private String occupation;

    Person(String name, String occupation)
    {
        this.name = name;
        this.occupation = occupation;
    }

    public String getName()
    {
        return this.name;
    }

    public String getOccupation()
    {
        return this.occupation;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("{ name: ").append(this.name).append(", occupation: ").append(this.occupation).append(" }");

        return sb.toString();
    }
}
7
  • 1
    Uh, wow, so an SQLException whose message is "out of memory" actually means that the database file is not found, and does not happen to be an OufOfMemoryError. Who could have thought of that? Aug 14, 2011 at 23:35
  • Nothing? It explains what the error means. Is that worthless? You might have read it, but you didn't understand it. Did you check to see if your database file is located? Did you change either the file location or the path to see if that had any effect? You have to do something to solve your problems.
    – duffymo
    Aug 14, 2011 at 23:58
  • I checked everythings, what you mentioned and all the time I have this same exception...
    – kspacja
    Aug 15, 2011 at 0:03
  • You'er still doing it incorrectly. I'll bet you didn't go back and re-read that link I posted.
    – duffymo
    Aug 15, 2011 at 0:07
  • I used already this tutorial and it still doesn't work... I don't what's going on
    – kspacja
    Aug 15, 2011 at 1:07
2

I have the same problem here, and I think we have run into some bug. The exception is absolutely not caused by "the file non existing": I carefully checked it with a proper test case.

The database itself is created using sqlite3 official command line tool, so no corrupt database either. I can safely tell you the lib is broken somehow.

Please tell me what is your OS and JVM version so that I see if it matches mine, and we can prepare a bug report.

2

Yes, in case when file not found it generates such strange exception "out of memory". In Eclipse IDE instead specifying database name and path separately, put database file name into field: Database location.

Example: Database location: c:\temp\test.db

1
  • Thanks, this solution works for me. I'm using Eclipse database plugin, the exactly same error. I specify filename in Database field and also append it to Database location. It's really confusing...t
    – Evi Song
    Dec 29, 2013 at 16:38
1

If path to your database contains any spaces JDBC can't find it. For example C:/Program Files is wrong. Must be C:/Program_Files. I had same problem and now it works.

1

I came across this issue trying to connect via Eclipse's "Data Source Explorer".

On a Mac, when I double clicked the file name in the browse prompt the database location was populated with the folder and the database with the file name. When I manually added the database file to the "Database Location" field I was then able to connect.

0

Like DarkCthulhu already mentioned you must not have any spaces in the path to your database file. This applies even if you declared a relative path to it (like in your case). I bet the path to your project contains one or more spaces.

You can either declare it with its full path and spaces escaped, or by changing your project location to a path without any spaces!

1
  • What does your answer add over the other answer that has already been mentioned? Jun 18, 2014 at 16:05
0

I just faced the same problem and I solved it . I was using IntelliJ as my IDE. I will show you how i fixed the problem by screen shots , step by step. Hope this will help you .
1- go to view | tool windows | database .
2- now a window containing the name of your databases is open in the right hand.select the database you want , then tap "alt+Enter"
now , on the opened window , make sure that you have filled the textboxes correct ! (they should include the "fileName" . directory is not enough!!)

0

I had the same problem. My solution is update the dependency sqlite-jdbc from version 3.7.2 to 3.16.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.