#### Using the Catalog Bean

After making the required preparations, you can now use the Catalog Bean to perform tasks according to your requirements.

##### Creating a catalog

The method newCatalog creates an empty catalog according to the catalog name that was set in catName, and saves it to disk.

When creating a new catalog, the directory of the catalog name should exist and contain no catalog files. You should make sure of this before you start creating the new catalog.

    Try
    {
    jrCatalog.setCatName("c:\\a.cat");
    newCatalog();
    }
    catch(JRCatalogException e)
    {
    System.out.println(e);
    }

##### Loading an existing catalog

The method loadCatalog is used to load a catalog according to the catalog name that was set in catName.

    try
    {
    jrCatalog.setCatName("c:\\a.cat");
    loadCatalog();
    }
    catch(JRCatalogException e)
    {
    System.out.println(e);
    }

##### Manipulating a catalog

A catalog can be manipulated by the actions of adding, deleting and editing of objects within it. A catalog contains many kinds of objects. These objects include connections, tables, views, file queries, stored procedures, queries, WHERE portions, formulas, summaries and parameters.

For example, you can modify the connection, the name, the URL, the user, the password and the driver.

    try
    {
    ConnectionDesc conDesc=new ConnectionDesc();
    conDesc.strName="Demo";
    conDesc.strURL="jdbc:oracle:thin:@myhost:1521:orcl";
    conDesc.strUser="system";
    conDesc.strPassword="manager";
    conDesc.strDriver="oracle.jdbc.driver.OracleDriver";
    jrCatalog.modifyConnection(conDesc);
    }
    catch(JRCatalogException e)
    {
    System.out.println(e);
    }

##### Getting information

You can get information about objects in a catalog via some methods.

For example, you can get the connection information via the method getConnectionDesc().

    Try
    {
    ConnectionDesc conDesc = bean.getConnectionDesc();
    }
    catch(JRCatalogException e)
    {
    System.out.println(e);
    }

##### Saving a catalog

Changes to a catalog are saved to a disk only when the method saveCatalog is used.

    Try
    {
    jrCatalog.saveCatalog();
    }
    catch(JRCatalogException e)
    {
    System.out.println(e);
    }

##### Handling exceptions

When an error occurs, the JRCatalogException is thrown out. Your program will catch the exception and handle the error.

The exception describes the error code and detailed message as follows:

- Code: 0
  Message: "System initialize failed."
  Description: The system initialization process failed. The Catalog Bean needs to be initialized to resolve the environment. This error is caused by reasons such as wrong report home, and bad installation.
  Detail: The reason why the initialization failed.
- Code: 1
  Message: "The catalog name is null."
  Description: The catalog name is null. The catalog name should be set before the loadReport() or newReport() methods are used.
  Detail: None.
- Code: 2
  Message: "Creating the catalog failed, because the specified directory does not exist or there is an existing catalog in that directory."
  Description: Cannot create a catalog in the specified directory, because the directory does not exist or there is a catalog in the directory.
  Detail: None
- Code: 3
  Message: "Cannot open the catalog file."
  Description: Cannot open the specified catalog file. The reason is file not found, file corrupt, format error or version mismatch.
  Detail: The reason why the opening of a catalog file failed.
- Code: 4
  Message: "Save catalog failed."
  Description: Cannot save the catalog file successfully. The cause is a file writing error, or a formula file save has failed.
  Detail: The reason why the saving of a catalog failed.
- Code: 5
  Message: "No catalog loaded."
  Description: The loadCatalog() or newCatalog() methods should be used before other methods, and closeCatalog() should be used in the end.
  Detail: None
- Code: 6
  Message: "The name of the WHERE portion is invalid or already exists."
  Description: The name specified is invalid or has already been used by another WHERE portion.
  Detail: None
- Code: 7
  Message: "The specified WHERE portion does not exist."
  Description: The specified WHERE portion does not exist.
  Detail: None.
- Code: 8
  Message: "A catalog can contain only one connection."
  Description: A catalog can contain only one connection, and this error occurs when adding a second connection.
  Detail: None.
- Code: 9
  Message: "The name of the connection is invalid or already exists."
  Description: The name specified is invalid or has already been used.
  Detail: None
- Code: 10
  Message: "Connection failure."
  Description: The JDBC connection failed when trying to create a connection to the database.
  Detail: The message of SQLException or ClassNotFoundException.
- Code: 11
  Message: "The name of the file query (customer SQL) is invalid or already exists."
  Description: The name of file query is either invalid or already exists.
  Detail: None.
- Code: 12
  Message: "Cannot create the file query."
  Description: Cannot create file query object.
  Detail: The reason why the file query cannot be created.
- Code: 13
  Message: "There is no connection in the catalog."
  Description: There is no connection in the catalog. If you are deleting or modifying a connection, the problem is that no such connection exists. If you are adding a file query, you cannot create it without a connection.
  Detail: None
- Code: 14
  Message: "The specified file query doesn't exist."
  Description: The specified file query does not exist.
  Detail: None.
- Code: 15
  Message: "Unknown Error."
  Description: The unexpected error.
  Detail: None
- Code: 16
  Message: "Invalid user ID or password"
  Description: The user ID or password is invalid.
  Detail: None
- Code: 17
  Message: "Only Tables or Views can be added into catalog."
  Description: The table type users specified is not supported.
  Detail: None
- Code: 18
  Message: "Close connection failure."
  Description: When closing connection, an error occurred.
  Detail: None