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