|
|
Line 1: |
Line 1: |
− | {{ScoutPage|cat=Server}}
| + | The Scout documentation has been moved to https://eclipsescout.github.io/. |
− | | + | |
− | The SqlService provides access to a database. | + | |
− | | + | |
− | * implements {{ScoutJavadoc|ISqlService|I}}
| + | |
− | * extends {{ScoutJavadoc|AbstractSqlService|C}}
| + | |
− | | + | |
− | ==Description==
| + | |
− | {{Note|TODO|Add a description}}
| + | |
− | | + | |
− | ==Minimal configuration==
| + | |
− | To be able to connect to a database, these properties needs to be configured:
| + | |
− | * {{ScoutProp|JdbcDriverName}} the qualified name of the JDBC driver.
| + | |
− | * {{ScoutProp|JdbcMappingName}} the JDBC path <code>"jdbc:..."</code>.
| + | |
− | * {{ScoutProp|Username}} the user name to connect to the database.
| + | |
− | * {{ScoutProp|Password}} the password to connect to the database.
| + | |
− | * {{ScoutProp|SqlStyle}} provides the class of the {{ScoutLink|Concepts|SqlStyle|Sql Style}} that should be used.
| + | |
− | | + | |
− | | + | |
− | | + | |
− | ===Delegation of the configuration to config.ini===
| + | |
− | If the Property {{ScoutProp|JdbcMappingName}} is set (using the {{ScoutLink|SDK|Object Properties View}}), the Java code looks like this:
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | package myapp.server.services.common.sql;
| + | |
− | | + | |
− | import org.eclipse.scout.rt.services.common.jdbc.AbstractSqlService;
| + | |
− | import org.eclipse.scout.service.IService;
| + | |
− | | + | |
− | public class MySqlService extends AbstractSqlService implements IService {
| + | |
− | @Override
| + | |
− | protected String getConfiguredJdbcMappingName() {
| + | |
− | return "jdbc:derby:C:/MyDB";
| + | |
− | }
| + | |
− | // + other configurations...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | The path to the database is hard coded in the code. You might want to delegate this configuration to the <code>config.ini</code> to have more flexibility (e.g one path for development and one path for production). Because the {{ScoutJavadoc|AbstractSqlService|C}} also provides a setter for this property <code>setJdbcMappingName(String)</code>, it is possible to set the property with the <code>config.ini</code> file:
| + | |
− | | + | |
− | <qualified name of the class>#<setter name without set prefix>=<value>
| + | |
− | | + | |
− | This pattern works in Eclipse Scout for all classes extending the {{ScoutJavadoc|AbstractService|C}}
| + | |
− | | + | |
− | For this example:
| + | |
− | | + | |
− | myapp.server.services.common.sql.MySqlService#JdbcMappingName=jdbc:derby:C:/MyDB
| + | |
− | | + | |
− | ==SQL convenience class==
| + | |
− | Scout proposes a convenience singleton class to access the default SqlService: {{ScoutJavadoc|SQL|C}} | + | |
− | | + | |
− | Here a simple select statement:
| + | |
− | <source lang="java">
| + | |
− | Object[][] s = SQL.select("" +
| + | |
− | "SELECT COMPANY_NR," +
| + | |
− | " SHORT_NAME," +
| + | |
− | " NAME" +
| + | |
− | " FROM COMPANY");
| + | |
− | </source>
| + | |
− | | + | |
− | This update example uses binds. The data are read from the formData variable. <code>":firstName"</code> will access the value in <code>formData.getFirstName()</code> or <code>formData.getFirstName().getValue()</code>
| + | |
− | <source lang="java">
| + | |
− | SQL.update(
| + | |
− | " update actor " +
| + | |
− | " set first_name = :firstName," +
| + | |
− | " last_name = :lastName " +
| + | |
− | " where actor_id = :id ",
| + | |
− | formData
| + | |
− | );
| + | |
− | </source>
| + | |
− | | + | |
− | == Multiple SQL Services ==
| + | |
− | {{Note|TODO|Using 2 services, more information in [http://www.eclipse.org/forums/index.php/mv/msg/369585/902322/#msg_902322 forum thread]}}
| + | |
− | | + | |
− | == Advanced binding ==
| + | |
− | | + | |
− | === Solve conflicts between bindings ===
| + | |
− | {{Note|TODO|Add explanations from this [https://www.eclipse.org/forums/index.php/t/803695/ forum thread] (extended to a more common case: for example read from 2 formDatas)}}
| + | |
− | | + | |
− | | + | |
− | === Select into an array holder ===
| + | |
− | Example:
| + | |
− | <source lang="java">
| + | |
− | 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));
| + | |
− | </source>
| + | |
− | | + | |
− | {{Note|TODO|Add explanations from this [http://www.eclipse.org/forums/index.php/mv/msg/310526/824186/#msg_824186 forum thread]}}
| + | |
− | | + | |
− | == See also ==
| + | |
− | * {{ScoutLink|Concepts|SqlStyle|Sql Style}}
| + | |
− | * {{ScoutLink|Concepts|Server Plug-In|Server Plug-In}}
| + | |
− | * {{ScoutLink|HowTo|Write_a_jdbc_connection_bundle|How To write a JDBC connection bundle}}
| + | |