Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Any Doc. to Connect to MySQL (trying to connect the contact sample app to mysql)
Any Doc. to Connect to MySQL [message #1746645] Wed, 02 November 2016 13:21 Go to next message
Santo Mota is currently offline Santo MotaFriend
Messages: 63
Registered: July 2013
Member
Hi expert, i am following the contact app sample, I found that the connection is with derby database, but I would like to use mysql instead.

I'm reading this documentation https://eclipsescout.github.io/6.0/beginners-guide.html#sec-contacts_jdbc

and studying from scout book begginers-guide.
But there is not sample to use other data base instead of derby.

Re: Any Doc. to Connect to MySQL [message #1746956 is a reply to message #1746645] Tue, 08 November 2016 03:39 Go to previous messageGo to next message
Santo Mota is currently offline Santo MotaFriend
Messages: 63
Registered: July 2013
Member
I was checking the derby connexion, and I have a few quetion about it.

Why are you using this style to connect. I was testing a normal conection just using the old sentences and it seem to work.
I trying to connecto to mysql, and I would like to do it in scout style.
so any light on this could be helpthy.


public static class DatabaseAutoCreateProperty extends AbstractBooleanConfigProperty {
// defines default value and key

@Override
protected Boolean getDefaultValue() {
return Boolean.TRUE; // <1>
}

@Override
public String getKey() {
return "contacts.database.autocreate"; // <2>
}
}
Re: Any Doc. to Connect to MySQL [message #1746965 is a reply to message #1746956] Tue, 08 November 2016 07:16 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 1252
Registered: October 2011
Senior Member
Here is how I connected to a MySql Database from a Scout Application. I have create a helloworld project in Eclipse (follow this tutorial).

1) Add the dependencies:
Open the pom.xml in the "helloworld.server" project and add those 2 dependencies:
    <dependency>
      <groupId>org.eclipse.scout.rt</groupId>
      <artifactId>org.eclipse.scout.rt.server.jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.17</version>
    </dependency>

You can copy the xml snippet here and paste it somewhere between <dependencies>...</dependencies>

Please note that the version of mysql-connector-java might be old. I have taken a version I already had in my maven repository. Please check what is the appropriate version for you, depending on your version of your MySql server and depending on what is available on maven central.

2) Create a service class:
In your server project, create a new class that will implement the ISqlService. For MySql, I recommend to extend from AbstractMySqlSqlService. Here is what I have used:
package com.example.helloworld.server.sql;

import org.eclipse.scout.rt.server.jdbc.mysql.AbstractMySqlSqlService;

public class HelloworldSqlService extends AbstractMySqlSqlService {

  @Override
  protected String getConfiguredJdbcMappingName() {
    return "jdbc:mysql://localhost:3306/testdb";
  }

  @Override
  protected String getConfiguredUsername() {
    return "root";
  }

  @Override
  protected String getConfiguredPassword() {
    return "";
  }
}

Of course you need to change the values, depending on your setup (jdbc mapping name, username, and password).

----

I hope this helps. Feel free to continue the discussion.
Re: Any Doc. to Connect to MySQL [message #1747263 is a reply to message #1746965] Fri, 11 November 2016 12:36 Go to previous messageGo to next message
Nelson Florez is currently offline Nelson FlorezFriend
Messages: 8
Registered: July 2016
Junior Member
I doubt the connection to the database. As it is sent by parameter the address of the server and the name of the bd, given that as it is raised in the solution these values are fixed, therefore it becomes necessary to change the code in the connection.
Is there a property file that can be filled with the values or is it up to the programmer to solve it?
Re: Any Doc. to Connect to MySQL [message #1747290 is a reply to message #1747263] Fri, 11 November 2016 17:33 Go to previous message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 1252
Registered: October 2011
Senior Member
When you provide examples, it is always a tradeoff between being simple and being complex. Of course, in normal cases, you do not want to have the connection path, username and password hardcoded in your application code. I recommend to use the configuration possibilities offered by scout.

With this class to define the properties:
package com.example.helloworld.server.sql;

import org.eclipse.scout.rt.platform.config.AbstractStringConfigProperty;

public class DatabaseProperties {

  public static class JdbcMappingNameProperty extends AbstractStringConfigProperty {
    @Override
    protected String getDefaultValue() {
          return "jdbc:mysql://localhost:3306/testdb";
    }

    @Override
    public String getKey() {
      return "myapp.database.jdbc.mapping.name";
    }
  }

  public static class UsernameProperty extends AbstractStringConfigProperty {
      @Override
      protected String getDefaultValue() {
          return "root";
      }
      
      @Override
      public String getKey() {
          return "myapp.database.username";
      }
  }

  public static class PasswordProperty extends AbstractStringConfigProperty {
      @Override
      protected String getDefaultValue() {
          return "";
      }
      
      @Override
      public String getKey() {
          return "myapp.database.password";
      }
  }
}


Your service will looks like that:
package com.example.helloworld.server.sql;

import org.eclipse.scout.rt.platform.config.CONFIG;
import org.eclipse.scout.rt.server.jdbc.mysql.AbstractMySqlSqlService;

public class HelloworldSqlService extends AbstractMySqlSqlService {

  @Override
  protected String getConfiguredJdbcMappingName() {
    return CONFIG.getPropertyValue(JdbcMappingNameProperty.class);
  }

  @Override
  protected String getConfiguredUsername() {
    return CONFIG.getPropertyValue(UsernameProperty.class);
  }

  @Override
  protected String getConfiguredPassword() {
    return CONFIG.getPropertyValue(PasswordProperty.class);
  }
}


The values will be read from:
* system properties
* an entry in a config.properties file
* environment variables
(see the documentation if you need more details).

This way you can set different values for your different environement (INTEGRATION, PRODUCTION, ...)

Previous Topic:scout + springdata jpa
Next Topic:Issues with Hello World example for beginners
Goto Forum:
  


Current Time: Thu Dec 26 18:29:31 GMT 2024

Powered by FUDForum. Page generated in 0.03278 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top