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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7060274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:29:18  来源:igfitidea点击:

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

javajdbcsqlite

提问by kspacja

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

回答by duffymo

Thissuggests that your clients.dbfile 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 herethat 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();
    }
}

回答by gd1

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 sqlite3official 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 版本是什么,以便我查看它是否与我的匹配,我们可以准备错误报告。

回答by harvyS

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

回答by lachut

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

如果您的数据库路径包含任何空格,则 JDBC 找不到它。例如C:/Program Files是错误的。必须是C:/Program_Files。我有同样的问题,现在它可以工作了。

回答by Adam

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

我在尝试通过 Eclipse 的“数据源资源管理器”进行连接时遇到了这个问题。

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 上,当我在浏览提示中双击文件名时,数据库位置填充了文件夹和具有文件名的数据库。当我手动将数据库文件添加到“数据库位置”字段时,我就可以连接了。

回答by Max

Like DarkCthulhualready 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!

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

回答by Shahryar Saljoughi

I just faced the same problem and I solved it . I was using IntelliJas 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!!)

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

回答by Stas Kharchenko

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