当jdbc与sqlite3数据库连接时,我该怎么做才能避免“内存不足”的错误?

时间:2022-11-09 00:15:37

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

当jdbc与sqlite3数据库连接时,我该怎么做才能避免“内存不足”的错误?

java.sql.SQLException: out of memoryat 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");

8 个解决方案

#1


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".

这表明找不到您的clients.db文件。尝试更恰当地定位该文件。向下滚动到标题为“如何指定数据库文件”的部分。

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.

我下载了SQLite JAR,把它放在我的CLASSPATH中,并在这里找到了一个在不到五分钟内完美运行的教程。正如预期的那样,它将test.db放在我的项目根目录中。

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

#2


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.

数据库本身是使用sqlite3官方命令行工具创建的,因此也没有损坏的数据库。我可以安全地告诉你,lib以某种方式被破坏了。

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.

请告诉我你的操作系统和JVM版本是什么,以便我看它是否与我的匹配,我们可以准备一份错误报告。

#3


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.

是的,如果找不到文件,它会生成“内存不足”这样的奇怪异常。在Eclipse IDE中,分别指定数据库名称和路径,将数据库文件名放入字段:数据库位置。

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

示例:数据库位置:c:\ temp \ test.db

#4


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.

如果数据库的路径包含JDBC无法找到的任何空格。例如C:/ Program Files是错误的。必须是C:/Program_Files.I有同样的问题,现在它的工作原理。

#5


1  

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

我遇到了这个问题,试图通过Eclipse的“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.

在Mac上,当我在浏览提示中双击文件名时,数据库位置填充了文件夹和带有文件名的数据库。当我手动将数据库文件添加到“数据库位置”字段时,我就能够连接。

#6


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.

就像DarkCthulhu已经提到的那样,你的数据库文件的路径中不能有任何空格。即使您声明了它的相对路径(如在您的情况下),这也适用。我敢打赌,你项目的路径包含一个或多个空格。

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

您可以声明它的完整路径和空间转义,或者将项目位置更改为没有任何空格的路径!

#7


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!!)当jdbc与sqlite3数据库连接时,我该怎么做才能避免“内存不足”的错误?

我刚遇到同样的问题,我就解决了。我使用IntelliJ作为我的IDE。我将告诉你我是如何通过屏幕截图逐步修复问题的。希望这对你有帮助。 1-去查看|工具窗口|数据库。 2-现在右侧打开一个包含数据库名称的窗口。选择所需的数据库,然后点击“alt + Enter”,在打开的窗口中,确保填写正确的文本框! (它们应该包含“fileName”。目录不够!!)

#8


0  

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

我有同样的问题。我的解决方案是将依赖sqlite-jdbc从版本3.7.2更新到3.16.1

#1


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".

这表明找不到您的clients.db文件。尝试更恰当地定位该文件。向下滚动到标题为“如何指定数据库文件”的部分。

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.

我下载了SQLite JAR,把它放在我的CLASSPATH中,并在这里找到了一个在不到五分钟内完美运行的教程。正如预期的那样,它将test.db放在我的项目根目录中。

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

#2


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.

数据库本身是使用sqlite3官方命令行工具创建的,因此也没有损坏的数据库。我可以安全地告诉你,lib以某种方式被破坏了。

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.

请告诉我你的操作系统和JVM版本是什么,以便我看它是否与我的匹配,我们可以准备一份错误报告。

#3


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.

是的,如果找不到文件,它会生成“内存不足”这样的奇怪异常。在Eclipse IDE中,分别指定数据库名称和路径,将数据库文件名放入字段:数据库位置。

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

示例:数据库位置:c:\ temp \ test.db

#4


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.

如果数据库的路径包含JDBC无法找到的任何空格。例如C:/ Program Files是错误的。必须是C:/Program_Files.I有同样的问题,现在它的工作原理。

#5


1  

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

我遇到了这个问题,试图通过Eclipse的“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.

在Mac上,当我在浏览提示中双击文件名时,数据库位置填充了文件夹和带有文件名的数据库。当我手动将数据库文件添加到“数据库位置”字段时,我就能够连接。

#6


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.

就像DarkCthulhu已经提到的那样,你的数据库文件的路径中不能有任何空格。即使您声明了它的相对路径(如在您的情况下),这也适用。我敢打赌,你项目的路径包含一个或多个空格。

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

您可以声明它的完整路径和空间转义,或者将项目位置更改为没有任何空格的路径!

#7


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!!)当jdbc与sqlite3数据库连接时,我该怎么做才能避免“内存不足”的错误?

我刚遇到同样的问题,我就解决了。我使用IntelliJ作为我的IDE。我将告诉你我是如何通过屏幕截图逐步修复问题的。希望这对你有帮助。 1-去查看|工具窗口|数据库。 2-现在右侧打开一个包含数据库名称的窗口。选择所需的数据库,然后点击“alt + Enter”,在打开的窗口中,确保填写正确的文本框! (它们应该包含“fileName”。目录不够!!)

#8


0  

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

我有同样的问题。我的解决方案是将依赖sqlite-jdbc从版本3.7.2更新到3.16.1