Monday, October 22, 2012

How to Get the Connection using Driver Manager?

    public static Connection getSimpleConnectionFromJdbcCURL() {
        String driverClass = "oracle.jdbc.driver.OracleDriver";
        String connectionURL =
            "jdbc:oracle:thin:@localhost:1521:xe";
        String userID = "hr";
        String userPassword = "hr";

        Connection con = null;
        try {
            Class.forName(driverClass).newInstance();
        } catch (Exception ex) {
           ex.printStackTrace();
        }

        try {
            con =
    DriverManager.getConnection(connectionURL, userID, userPassword);
            con.setAutoCommit(false);
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return con;
    }

----------------------
Close the Connection

    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (resultSet != null){
                close(resultSet);
            }
            if (statement != null){
                close(statement);
            }
            if (connection != null){
                close(connection);
            }
        } catch (Exception e) {

        }
    }