Geertjan's Blog

  • November 19, 2008

Getting Started with the Database Explorer API

Geertjan Wielenga
Product Manager
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 (rs.next()) {
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 J2EEComponentDropProvider.java 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.

Join the discussion

Comments ( 1 )
  • arunkumar Monday, January 12, 2009

    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 .

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.