HomeActive Query Builder - IntroductionQuick Start GuidesQuick Start Guide for Java edition

1.6. Quick Start Guide for Java edition

  1. Drop com.adbs.querybuilder.QueryBuilder on a form using a visual form designer or create an instance programmatically.

    
    import com.adbs.querybuilder.*;
    ...
    QueryBuilder queryBuilder1 = new QueryBuilder();
  2. Add any text control on your form (for example, JTextPane). Handle its Focus event and put changed query text to the query builder.

    
    jTextPane1.addFocusListener(new FocusAdapter()
    {
       public void focusLost(FocusEvent evt)
       {
          try
          {
             queryBuilder1.setSQL(jTextPane1.getText());
          }
          catch (QueryBuilderException ex)
          {
             JOptionPane.showMessageDialog(this, ex.getMessage());
          }
       }
    });
  3. Create PlainTextSQLBuilder instance and bind it to the query builder instance.

    
    PlainTextSQLBuilder sqlBuilder = new PlainTextSQLBuilder();
    sqlBuilder.setQueryBuilder(queryBuilder1);
  4. Handle SQLUpdated event of the PlainTextSQLBuilder to receive notification about query text changes.

    
    sqlBuilder.addSQLUpdatedEventListener(new SQLUpdatedEventListener()
    {
       public void sqlUpdatedEventOccurred(SQLUpdatedEvent event)
       {
          try
          {
             // set the query text to a text control
             jTextPane1.setText(sqlBuilder.getSQL());
          }
          catch (QueryBuilderException ex)
          {
             JOptionPane.showMessageDialog(this, ex.getMessage());
          }
       }
    });
  5. Create metadata provider and supply it with SQL server connection. Set corresponding syntax provider and call the RefreshMetadata method to load objects from a database.

    
    try
    {
       JdbcMetadataProvider metadataProvider = new JdbcMetadataProvider();
       Class.forName("com.mysql.jdbc.Driver");
       metadataProvider.setConnection(DriverManager.getConnection("jdbc:mysql://host:port/database", "user", "password"));
    
       MySQLSyntaxProvider syntaxProvider = new MySQLSyntaxProvider();
    
       queryBuilder1.setSyntaxProvider(syntaxProvider);
       queryBuilder1.setMetadataProvider(metadataProvider);
    
       queryBuilder1.refreshMetadata();
    }
    catch (ClassNotFoundException ex)
    {
       JOptionPane.showMessageDialog(this, "Failed to load JDBC driver: \n" + ex.getMessage());
    }
    catch (Exception ex)
    {
       JOptionPane.showMessageDialog(this, ex.getMessage());
    }
  6. That's all! Now you can run your application.

Related Pages
This page was: Helpful | Not Helpful