Active Query Builder support area

Quick Start Guide for the Java Edition

Last modified:


  1. Drop the com.adbs.querybuilder.QueryBuilder control 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 the 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 a metadata provider and supply it with a database connection. Set a corresponding syntax provider and call the RefreshMetadata method to load objects from the 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.

Is this article helpful for you?