package xxxxx.yyyyy.zzzzz.xyz1.parser.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SampleJdbcTest {

    private static final String URL = "jdbc:derby://localhost:1527/sample";
    private static final String USER = "app";
    private static final String PASSWORD = "app";
    private static final String SQL = "SELECT DESCRIPTION FROM PRODUCT WHERE DESCRIPTION = ?";

    //@Test
    public void test2() {
        try (Connection c = connect(URL, USER, PASSWORD);) {
            try (PreparedStatement ps = c.prepareStatement(SQL);) {
                ps.setString(1, "Identity Server");
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getString(1));
                }
                commit(c);
            } catch (SQLException cause) {
                try {
                    rollback(c);
                } catch (SQLException cause1) {
                    //throw new RuntimeException(cause1);
                }
                throw new RuntimeException(cause);
            } finally {
            }
        } catch (SQLException cause) {
            throw new RuntimeException(cause);
        } finally {
        }
    }
//    @Test
//    public void test() {
//        Connection c = null;
//        PreparedStatement ps = null;
//        ResultSet rs = null;
//        try {
////            c = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
////            c.setAutoCommit(false);
//            c = connect(URL, USER, PASSWORD);
//            ps = c.prepareStatement("SELECT DESCRIPTION FROM PRODUCT WHERE DESCRIPTION = ?");
//            ps.setString(1, "Identity Server");
//            rs = ps.executeQuery();
//            while (rs.next()) {
//                System.out.println(rs.getString(1));
//            }
////            c.commit();
//            commit(c);
//        } catch (SQLException cause) {
//            try {
////                if (c != null) {
////                    c.rollback();
////                }
//                rollback(c);
//            } catch (SQLException cause1) {
//                //throw new RuntimeException(cause1);
//            }
//            throw new RuntimeException(cause);
//        } finally {
//            try {
//                if (ps != null && !ps.isClosed()) {
//                    ps.close();
//                }
//                if (c != null && !c.isClosed()) {
//                    c.close();
//                }
//                System.out.println("rs " + rs.isClosed());
//                System.out.println("ps " + ps.isClosed());
//                System.out.println("c  " + c.isClosed());
//            } catch (SQLException cause) {
//                //throw new RuntimeException(cause);
//            }
//        }
//    }

    private static Connection connect(String url, String user, String password) throws SQLException {
        Connection c = DriverManager.getConnection(url, user, password);
        c.setAutoCommit(false);
        return c;
    }

    private static void commit(Connection c) throws SQLException {
        if (c == null) {
            throw new IllegalArgumentException();
        }
        c.commit();
    }

    private static void rollback(Connection c) throws SQLException {
        if (c == null) {
            throw new IllegalArgumentException();
        }
        c.rollback();
    }
}
