// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package javadb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JDBCDALModern extends Demo implements DAL {
    private String constr;
    private String usr;
    private String pwd;
    public JDBCDALModern(String constr, String usr, String pwd) {
        this.constr = constr;
        this.usr = usr;
        this.pwd = pwd;
    }
    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(constr, usr, pwd);
    }
    @Override
    public T1 getOneByPK(int f1) throws SQLException {
        T1 res;
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("SELECT f1,f2 FROM t1 WHERE f1 = ?")) {
                pstmt.setInt(1, f1);
                try(ResultSet rs = pstmt.executeQuery()) {
                    if(rs.next()) {
                        res = new T1(rs.getInt("f1"), rs.getString("f2"));
                    } else {
                        res = null;
                    }
                }
            }
        }
        return res;
    }
    @Override
    public List<T1> getAll() throws SQLException {
        List<T1> res = new ArrayList<T1>();
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("SELECT f1,f2 FROM t1")) {
                try(ResultSet rs = pstmt.executeQuery()) {
                    while(rs.next()) {
                        res.add(new T1(rs.getInt("f1"), rs.getString("f2")));
                    }
                }
            }
        }
        return res;
    }
    @Override
    public List<T1> getRangeOfPK(int f1_start, int f1_end) throws SQLException {
        List<T1> res = new ArrayList<T1>();
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("SELECT f1,f2 FROM t1 WHERE f1 BETWEEN ? AND ?")) {
                pstmt.setInt(1, f1_start);
                pstmt.setInt(2, f1_end);
                try(ResultSet rs = pstmt.executeQuery()) {
                    while(rs.next()) {
                        res.add(new T1(rs.getInt("f1"), rs.getString("f2")));
                    }
                }
            }
        }
        return res;
    }
    @Override
    public List<T1> getContainingF2(String f2) throws SQLException {
        List<T1> res = new ArrayList<T1>();
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("SELECT f1,f2 FROM t1 WHERE f2 LIKE ?")) {
                pstmt.setString(1, "%" + f2 + "%");
                try(ResultSet rs = pstmt.executeQuery()) {
                    while(rs.next()) {
                        res.add(new T1(rs.getInt("f1"), rs.getString("f2")));
                    }
                }
            }
        }
        return res;
    }
    @Override
    public void saveChanges(T1 o) throws SQLException {
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("UPDATE t1 SET f2 = ? WHERE f1 = ?")) {
                pstmt.setString(1, o.getF2());
                pstmt.setInt(2,  o.getF1());
                pstmt.executeUpdate();
            }
        }
    }
    @Override
    public void saveNew(T1 o) throws SQLException {
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("INSERT INTO t1 VALUES(?,?)")) {
                pstmt.setInt(1,  o.getF1());
                pstmt.setString(2, o.getF2());
                pstmt.executeUpdate();
            }
        }
    }
    @Override
    public void remove(int f1) throws SQLException {
        try(Connection con = getConnection()) {
            try(PreparedStatement pstmt = con.prepareStatement("DELETE FROM t1 WHERE f1 = ?")) {
                pstmt.setInt(1, f1);
                pstmt.executeUpdate();
            }
        }
    }
    public static void main(String[] args) throws SQLException {
        test("Plain JDBC - MySQL", new JDBCDALModern("jdbc:mysql://localhost/Test", "root", ""));
        test("Plain JDBC - Oracle", new JDBCDALModern("jdbc:oracle:thin:@localhost:1521:xe", "arne", "xxxxxx"));
        test("Plain JDBC - DB2", new JDBCDALModern("jdbc:db2://localhost:50000/Test", "arne", "xxxxxx"));
    }
}
