Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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 Main {
    public static void main(String[] args) throws Exception {
        String DEFAULT_DRIVER = "org.sqlite.JDBC";
        String DEFAULT_URL = "jdbc:sqlite:data/test.db";

        Connection conn = createConnection(DEFAULT_DRIVER, DEFAULT_URL);
        createTable(conn);

        List<Person> people = new ArrayList<Person>();
        people.add(new Person("A", "a"));
        people.add(new Person("B", "b"));
        people.add(new Person("C", "c"));
        saveAll(conn, people);

        List<Person> rows = findAll(conn);
        System.out.println(rows);
        close(conn);
    }

    private static List<Person> findAll(Connection conn) throws SQLException {
        List<Person> rows = new ArrayList<Person>();
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("select * from people;");
        while (rs.next()) {
            rows.add(new Person(rs.getString("name"), rs.getString("occupation")));
        }
        close(stat);
        close(rs);
        return rows;
    }

    private static void saveAll(Connection conn, List<Person> people) throws SQLException {
        PreparedStatement 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);
        close(prep);
    }

    static void createTable(Connection conn) throws SQLException {
        Statement stat = null;
        stat = conn.createStatement();
        stat.executeUpdate("drop table if exists people;");
        stat.executeUpdate("create table people (name, occupation);");
        close(stat);
    }

    static Connection createConnection(String driver, String url) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url);
        return conn;
    }

    static void close(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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