// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

public class AccessPrep {
    private static final String CONURL = "jdbc:odbc:;Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Databases\\MSAccess\\Test.mdb";
    public static void insert(int f1, double f2, String f3, BigDecimal f4, boolean f5, Date f6) throws SQLException {
        Connection con = DriverManager.getConnection(CONURL);
        PreparedStatement pstmt = con.prepareStatement("INSERT INTO pt VALUES (?, ?, ?, ?, ?, ?)");
        pstmt.setInt(1, f1);
        pstmt.setDouble(2, f2);
        pstmt.setString(3, f3);
        pstmt.setBigDecimal(4, f4);
        pstmt.setBoolean(5, f5);
        pstmt.setTimestamp(6, new Timestamp(f6.getTime()));
        pstmt.executeUpdate();
        con.close();
    }
    public static void updateF5(boolean f5, int f1) throws SQLException {
        Connection con = DriverManager.getConnection(CONURL);
        PreparedStatement pstmt = con.prepareStatement("UPDATE pt SET f5 = ?, f6 = ? WHERE f1 = ?");
        pstmt.setBoolean(1, f5);
        pstmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
        pstmt.setInt(3, f1);
        pstmt.executeUpdate();
        con.close();
    }
    public static void printRange(int startf1, int endf1) throws SQLException {
        Connection con = DriverManager.getConnection(CONURL);
        PreparedStatement pstmt = con.prepareStatement("SELECT f1, f2, f3, f4, f5, f6 FROM pt WHERE f1 BETWEEN ? AND ?");
        pstmt.setInt(1, startf1);
        pstmt.setInt(2, endf1);
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            System.out.println(rs.getInt(1) + " " + rs.getDouble(2) + " " +
                               rs.getString(3) + " " + rs.getBigDecimal(4) + " " +
                               rs.getBoolean(5) + " " + rs.getTimestamp(6));
        }
        con.close();
    }
    public static void main(String[] args) throws Exception {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // tabel pt:
        //  f1 - long integer
        //  f2 - double
        //  f3 - text
        //  f4 - currency
        //  f5 - yes/no
        //  f6 - date/time
        insert(1, 1.2, "ABC", new BigDecimal("1.20"), true, new Date());
        Thread.sleep(2000);
        insert(2, 3.4, "DEF", new BigDecimal("3.40"), true, new Date());
        updateF5(false, 2);
        printRange(1, 2);
    }
}
