-3

我可以更快地写入sql数据库吗?我想用更少的时间获得 TestLetturaScritturaDatabase.sqlite。阅读不是问题,但写作对我来说不是很好。我的软件每秒多次写入小部分;是否不可能减少这些部分的数量并增加每个部分的记录数量。

package sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestLetturaScritturaDatabase {

    public static void main(String[] args) throws ClassNotFoundException,
            SQLException {
        double tempoPRINCIPALE = System.nanoTime();
        double tempo = System.nanoTime();
        int i;
        int j;
        int k;
        int CAMPI = 100;
        int RECORD = 100000;
        Double[][] MATRICE = new Double[RECORD][CAMPI];
        for (j = 0; j < RECORD; j++) {
            for (k = 0; k < CAMPI; k++) {
                MATRICE[j][k] = (double) (j - k);
            }
        }
        Class.forName("org.sqlite.JDBC").getInterfaces();
        String nome = "jdbc:sqlite:TestLetturaScritturaDatabase.sqlite";
        Connection conn = DriverManager.getConnection(nome);
        Statement stat = conn.createStatement();
        stat.executeUpdate("DROP TABLE IF EXISTS matrice");
        String TABELLA = "CREATE TABLE matrice (id INTEGER PRIMARY KEY AUTOINCREMENT";
        for (i = 0; i < CAMPI; i++) {
            TABELLA = TABELLA + ", '" + i + "' REAL";
        }
        TABELLA = TABELLA + ")";
        String Inserisci = "INSERT INTO matrice ('";
        stat.executeUpdate(TABELLA);
        for (j = 0; j < RECORD; j++) {
            for (k = 0; k < CAMPI; k++) {
                if (k < CAMPI - 1) {
                    Inserisci = Inserisci + k + "', '";
                } else {
                    Inserisci = Inserisci + k + "') VALUES ('";
                }
            }
            for (k = 0; k < CAMPI; k++) {
                if (k < CAMPI - 1) {
                    Inserisci = Inserisci + MATRICE[j][k] + "', '";
                } else {
                    Inserisci = Inserisci + MATRICE[j][k] + "')";
                }
            }
            stat.executeUpdate(Inserisci);
            Inserisci = "INSERT INTO matrice ('";
        }
        System.out.println("Database creato con successo!\n");
        ResultSet rs = stat.executeQuery("SELECT * FROM matrice");
        System.out.println("Contenuto matrice:\n");
        while (rs.next()) {
            for (k = 1; k <= CAMPI + 1; k++) {
                if (k <= CAMPI) {
                    System.out.print(rs.getString(k) + " ");
                } else {
                    System.out.println(rs.getString(k));
                }
            }
        }
        conn.close();
        stat.close();
        rs.close();
    }
}

有错误的新代码:

public static void main(String[] args) throws ClassNotFoundException,
            SQLException {
        Class.forName("org.sqlite.JDBC").getInterfaces();
        Connection conn = DriverManager
                .getConnection("jdbc:sqlite:test.sqlite");
        conn.setAutoCommit(false);
        Statement stat = conn.createStatement();
        stat.executeUpdate("DROP TABLE IF EXISTS people");
        stat.addBatch("CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT NOT NULL, occupation, vote NULL)");
        stat.addBatch("INSERT INTO people (nome, occupation) VALUES ('Peruzzo','Unable')");
        stat.addBatch("INSERT INTO people (nome, occupation) VALUES ('Piyush','Clever')");
        stat.addBatch("INSERT INTO people (nome, occupation,vote) VALUES ('Stackoverflow', 'Professionals','10')");
        stat.addBatch("UPDATE people SET nome = 'Mario Peruzzo' WHERE nome = 'Peruzzo';");
        stat.executeBatch();
        ResultSet rs = stat.executeQuery("SELECT * FROM people");
        while (rs.next()) {
            System.out.println("Nome: " + rs.getString("nome"));
            System.out.println("Lavoro: " + rs.getString("occupation"));
            System.out.println("Voto: " + (rs.getDouble("vote") * 2));
        }
        conn.close();
        stat.close();
        rs.close();
    }
4

1 回答 1

3

您可以与Sun 文档中列出的Batch Updates组合使用。在使用批量更新之前关闭PreparedStatementsAuto Commitconn.setAutoCommit(false)

于 2013-04-02T20:49:24.110 回答