Skip to main content
Jump to: navigation, search

Scout/Concepts/Sql Service

Wiki Home

The SqlService provides access to a database.

  • implements I obj.pngISqlService
  • extends C obj.pngAbstractSqlService


Minimal configuration

To be able to connect to a database, these properties needs to be configured:

  • JdbcDriverName the qualified name of the JDBC driver.
  • JdbcMappingName the JDBC path "jdbc:...".
  • Username the user name to connect to the database.
  • Password the password to connect to the database.
  • SqlStyle provides the class of the Sql Style that should be used.

Delegation of the configuration to config.ini

If the Property JdbcMappingName is set (using the Object Properties View), the Java code looks like this:

import org.eclipse.scout.service.IService;
public class MySqlService extends AbstractSqlService implements IService {
  protected String getConfiguredJdbcMappingName() {
    return "jdbc:derby:C:/MyDB";
  // + other configurations...

The path to the database is hard coded in the code. You might want to delegate this configuration to the config.ini to have more flexibility (e.g one path for development and one path for production). Because the C obj.pngAbstractSqlService also provides a setter for this property setJdbcMappingName(String), it is possible to set the property with the config.ini file:

<qualified name of the class>#<setter name without set prefix>=<value>

This pattern works in Eclipse Scout for all classes extending the C obj.pngAbstractService

For this example:

SQL convenience class

Scout proposes a convenience singleton class to access the default SqlService: C obj.pngSQL

Here a simple select statement:

    Object[][] s ="" +
        "SELECT COMPANY_NR," +
        "       SHORT_NAME," +
        "       NAME" +
        " FROM  COMPANY");

This update example uses binds. The data are read from the formData variable. ":firstName" will access the value in formData.getFirstName() or formData.getFirstName().getValue()

        " update      actor " +
        " set         first_name = :firstName," +
        "             last_name = :lastName " +
        " where       actor_id = :id ",

Multiple SQL Services

Advanced binding

Solve conflicts between bindings

Select into an array holder


    BeanArrayHolder<AnswerBean> holder = new BeanArrayHolder<AnswerBean>(AnswerBean.class);
    SQL.selectInto(" select question_id, name, answer_id " +
        " from answers " +
        " into  :{list.questionNr}, :{list.yourName}, :{list.answerNr}", new NVPair("list", holder));

See also

Back to the top