Insert into sqlite Database - Java java.sql

Java examples for java.sql:Sqlite

Description

Insert into sqlite Database

Demo Code


import java.sql.*;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String toolName = "java2s.com";
        String encodedFile = "java2s.com";
        String extension = "java2s.com";
        storeFile(toolName,encodedFile,extension);
    }/*from  ww w.ja  v a 2 s .  com*/
    private static final Logger logger = Logger
            .getLogger(DatabaseUtil.class);
    public static void storeFile(String toolName, String encodedFile,
            String extension) {
        Connection c = null;
        Statement stmt = null;

        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:excelFiles.db");
            c.setAutoCommit(false);

            stmt = c.createStatement();

            String sql = "CREATE TABLE IF NOT EXISTS ExcelFiles "
                    + "(ToolName TEXT    NOT NULL, "
                    + " File     TEXT    NOT NULL, "
                    + " Extension TEXT   NOT NULL, "
                    + " Uploaded INT NOT NULL)";
            stmt.executeUpdate(sql);

            PreparedStatement prep = c
                    .prepareStatement("insert into ExcelFiles values (?, ?, ?, ?);");
            prep.setString(1, toolName);
            prep.setString(2, encodedFile);
            prep.setString(3, extension);
            prep.setInt(4, 0);
            prep.addBatch();

            stmt.close();
            prep.executeBatch();

            c.commit();
            c.close();
        } catch (Exception e) {
            logger.error(e.getClass().getName() + ": " + e.getMessage());
        }
    }
}

Related Tutorials