Getting Started with the Database Explorer API

Now that the Database Explorer API is official, I need to write a tutorial covering some typical things that you might want to do with it. David Van Couvering, who leads this area of NetBeans, provided me with the following notes. Thanks a lot David. The use case is that of a developer wanting to inspect metadata for a connection in the Database Explorer.
  1. Get a connection. Normally the first step for this is to find out what connection the user wants to use. We provide a helper utility that provides you with a combo box containing all current connections in the Database Explorer, as well as the ability to create a new connection. Here's how you might use this API:

    • In your UI, create a combo box.

    • Associate the combo box with the utility:
      DatabaseExplorerUIs.connect(dbconnComboBox, ConnectionManager.getDefault());
    • In your action listener for your combo box:
      Object selected = dbconnComboBox.getSelectedItem();
              if (selected instanceof DatabaseConnection) {
                  dbconn = (DatabaseConnection) selected;
  2. Make sure you're connected. It's possible your database connection is not connected. Here's what you do:
           Connection conn = dbconn.getJDBCConnection(true);
            if (conn == null) {
            conn = dbconn.getJDBCConnection(true);
            if (conn == null) {
                // user chose not to connect, handle this
Get metadata. Let's say you want to get all the tables for the connection. We are going to be adding a new Metadata API, but for now you just use the native JDBC DatabaseMetadata. So, for example:
       Connection conn = dbconn.getJDBCConnection(true);
        try {
            ResultSet rs = conn.getMetaData().getTables(null, null, "\*", new String[]{"TABLE"});
            while ( {
                System.out.println("Table name is " + rs.getString("TABLE_SCHEMA") + "." + rs.getString("TABLE"));
        } catch (SQLException sqle) {
            // handle me

For example, here's a TopComponent showing the result of the code above:

Finally, here's some code, also from David, for dragging and dropping in a snippet. It comes from in org.netbeans.modules.form.j2ee:

     \* Processes given transferable and returns the corresponding
     \* NewComponentDrop.
     \* @param formModel corresponding form model.
     \* @param transferable description of transferred data.
     \* @return NewComponentDrop that corresponds to given
     \* transferable or null if this provider
     \* don't understand to or don't want to process this data transfer.
    public NewComponentDrop processTransferable(FormModel formModel, Transferable transferable) {
        try {
            if (transferable.isDataFlavorSupported(DatabaseMetaDataTransfer.CONNECTION_FLAVOR)) {
                DatabaseMetaDataTransfer.Connection connection = (DatabaseMetaDataTransfer.Connection)transferable.getTransferData(DatabaseMetaDataTransfer.CONNECTION_FLAVOR);
                return new DBConnectionDrop(formModel, connection);
            } else if (transferable.isDataFlavorSupported(DatabaseMetaDataTransfer.COLUMN_FLAVOR)) {
                DatabaseMetaDataTransfer.Column column = (DatabaseMetaDataTransfer.Column)transferable.getTransferData(DatabaseMetaDataTransfer.COLUMN_FLAVOR);
                return new DBColumnDrop(formModel, column);
            } else if (transferable.isDataFlavorSupported(DatabaseMetaDataTransfer.TABLE_FLAVOR)) {
                DatabaseMetaDataTransfer.Table table = (DatabaseMetaDataTransfer.Table)transferable.getTransferData(DatabaseMetaDataTransfer.TABLE_FLAVOR);
                return new DBTableDrop(formModel, table);
        } catch (Exception ex) {
            // should not happen

            Logger.getLogger(getClass().getName()).log(Level.INFO, ex.getMessage(), ex);
        return null;

All the above code will be part of a new tutorial soon.


hi,i am from India.i am learning netbeans IDE .for that i need a complete tutorial to learn and create project.i have one more doubt that how to create a database and connect that database to my java application?if you answer all the above .i am very happy and salute u sir.hope you wi;ll reply . bye .

Posted by arunkumar on January 11, 2009 at 07:17 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016