import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.Arrays;

/** Created by IntelliJ IDEA. User: MACHIZAUD Andréa Date: 13 déc. 2010 Time: 16:18:20 */
public class OracleConnector {
    private static String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private static String driver = "oracle.jdbc.driver.OracleDriver";
    private static String login = "Andréa";
    private static String pass  = "qU651IfS";

    private static Connection conn = null;
    private static boolean isDriverRegistered = false;

    public static Connection getConnection(){
        try {
            if ( !isDriverRegistered ) {
                Driver oracleDriver = (Driver) Class.forName(driver).newInstance();
                DriverManager.registerDriver(oracleDriver);
                isDriverRegistered = true;
            }
            if( conn ==null ) {
                conn = DriverManager.getConnection(url,login,pass);
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return conn;
    }


    public static void main(String[]args) throws NoSuchAlgorithmException {
        Connection conn = getConnection();
        System.out.println(conn);

        String baba = "baba";
        byte[] b = baba.getBytes();
        System.out.println("Byte : " + Arrays.toString(b) + " (" + new String(b) +")");
        b = MessageDigest.getInstance("SHA-1").digest(b);
        System.out.println("Byte SHA-1 : " + Arrays.toString(b) + " (" + new String(b) +")");

        listTable(conn,"droit");

        System.out.println();
        insertNewData(conn,20L,"sample droit","droit");

        System.out.println();
        listTable(conn,"droit");

    }

    private static void insertNewData(Connection conn,Long id, String data, String tablename) {
        try {
            String query = "INSERT INTO "+tablename+"(id,nom_droit)VALUES('"+id+"','"+data+"')";
            PreparedStatement insertStatement = conn.prepareStatement(query);

            ResultSet set = insertStatement.executeQuery();

            System.out.println("Set fetch size : "+set.getFetchSize());

        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void listTable(Connection conn,String tablename) {
        try {
            PreparedStatement statement = conn.prepareStatement("SELECT * FROM " + tablename);

            ResultSet set = statement.executeQuery();
            while(set.next()) {
                int number = set.getRow();
                long id = set.getLong("id");
                String label = set.getString("nom_droit");
                System.out.println("row n°"+number+" : id("+id+") nom_droit("+label+")");
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
