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

前端之家收集整理的这篇文章主要介绍了java – 当jdbc与sqlite3数据库连接时,我该怎么做才能避免“内存不足”的错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当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");

解决方法

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

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

我按照我的方式重写了那个教程.有用.不要说它什么也没带来.

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

猜你在找的Java相关文章