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