Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

JForex and MySQL database
 Post subject: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 13:34 

User rating: 0
Joined: Wed 30 Apr, 2014, 13:27
Posts: 5
Location: FranceFrance
Hello,

I'm looking for a way to connect JForex and MySQL database.

I wrote a MyStrategy.java code inside <UserDir>\Documents\JForex\Strategies folder.

package jforex;

import java.util.*;

import com.dukascopy.api.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Library("mysql-connector-java-5.1.30-bin.jar")


public class MyStrategy implements IStrategy {
    @Configurable("DB IP/host")
    public String db_ip_setting = "127.0.0.1";
    @Configurable("DB port")
    public String db_port_setting = "3306";
    @Configurable("DB login")
    public String db_user_setting = "root";
    @Configurable("DB password")
    public String db_password_setting = "123456";
    @Configurable("DB name")
    public String db_name_setting = "test";   
   
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
   
    private Connection db_conn = null;

    private Connection db_connect(String db_name, String host, String port, String user, String pass)
    {
        Connection connection = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://" + host + ":" + port + "/" + db_name;
            console.getOut().println(url);
            connection = DriverManager.getConnection(url, user, pass);
        }
        catch (SQLException e)
        {
            console.getOut().println("DB connection failed!");
            return null;
        }
       
        if (connection != null)
        {
            console.getOut().println("DB connection established!");
        }
        else
        {
            console.getOut().println("Failed to make a connection!");
        }
       
        return connection;
    }
       
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
       
        db_conn = db_connect(db_name_setting, db_ip_setting, db_port_setting, db_user_setting, db_password_setting);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        //console.getOut().println("new tick " + instrument.toString());
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


I also have mysql-connector-java-5.1.30-bin.jar in <UserDir>\Documents\JForex\Strategies\files directory

but I get an error. My code doesn't connect to database.

I don't understand why. Database exists. Login / password are ok.
I can connect to this database using a Python script so the the problem is not on MySQL side.

Any idea ?


Kind regards

Femto


 
 Post subject: Re: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 13:55 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
FemtoTrader wrote:
but I get an error. My code doesn't connect to database.
Please elaborate. Also specify what errors you get in the Java console, see: viewtopic.php?f=81&t=39073


 
 Post subject: Re: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 14:11 

User rating: 0
Joined: Wed 30 Apr, 2014, 13:27
Posts: 5
Location: FranceFrance
I get this error on Java console:

Quote:
java.lang.ExceptionInInitializerError
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at jforex.MyStrategy.db_connect(MyStrategy.java:52)
at jforex.MyStrategy.onStart(MyStrategy.java:87)
at com.dukascopy.api.impl.execution.u.call(Unknown Source)
at com.dukascopy.api.impl.execution.u.call(Unknown Source)
at com.dukascopy.api.impl.connect.ag.a(Unknown Source)
at com.dukascopy.api.impl.connect.bk.bz(Unknown Source)
at com.dukascopy.api.impl.connect.bk.call(Unknown Source)
at com.dukascopy.api.impl.execution.k.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at com.dukascopy.api.impl.execution.g$a.f(Unknown Source)
at com.dukascopy.api.impl.execution.g$a.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "file.encoding" "read")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
at java.lang.System.getProperty(Unknown Source)
at com.mysql.jdbc.StringUtils.<clinit>(StringUtils.java:68)
... 15 more


and I get "DB connection failed" strategy message.

I'm using https://dev.mysql.com/downloads/connector/j/


 
 Post subject: Re: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 14:15 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
the dependency is trying to read a system property, this is only allowed if you grant the strategy full access to you local workstation resources, i.e. add the @RequiresFullAccess annotation before the class definition.


 
 Post subject: Re: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 14:25 

User rating: 0
Joined: Wed 30 Apr, 2014, 13:27
Posts: 5
Location: FranceFrance
If think I need to import something because I get

13:24:25 RequiresFullAcces cannot be resolved to a type


 
 Post subject: Re: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 14:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
you are missing a letter at the end.


 
 Post subject: Re: JForex and MySQL database Post rating: 0   New post Posted: Wed 30 Apr, 2014, 15:44 

User rating: 0
Joined: Wed 30 Apr, 2014, 13:27
Posts: 5
Location: FranceFrance
I'm really sorry. Thanks. I get "DB connection established!" message so it should be ok now.


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com