Custom Event Handler opening of Database connection

Greetings,

I am trying to create a custom business logic event in Java. Do i have to include code for opening the database connection in order to be able to use prepared statements, or this is already taken of elsewhere in the console?

Something similar to

Connection mysqlConn = DriverManager.getConnection(HOST, USER_ID, PASSWORD);
mysqlConn.getConnection();
PreparedStatement st = mysqlConn.preparedStatement(query); 

Regards,
George

What database are you connecting to?

My tables on backendless

You need to use Backendless SDK to connect to your tables.

That am doing.

Consider my custom event handler below

@BackendlessEvent( "CountObjectsIn4TablesEventHandler" )
public class CountObjectsIn4TablesEventHandler extends com.backendless.servercode.extension.CustomEventHandler
{
    
  @Override
  public Map handleEvent( RunnerContext context, Map eventArgs )
  {
    // add your code here




    return Collections.emptyMap();
  }
    
}

Where it says “add your code here” i need to create a statement object in java to so as to send my SQL statement to the DBMS to get the object counts. It takes an instance of an active connection to create a Statement object. This is how i was expecting to start:



Connection con = DriverManager.getConnection(url, "myLogin", "myPassword");
Statement stmt = con.createStatement();
String query = "SELECT COUNT(*) FROM [table_name]";
ResultSet rs = stmt.executeQuery(query);

i.e use my Connection object con to create the Statement object stmt :

You CANNOT use “DriverManager” and “Connection” classes to communicate with the Backendless database. To run queries and get data from Backendless, you MUST use Backendless SDK.

How to use Backendless SDK on code that sits on the server side, am ok with client side. The custom event handler sits on the server side, unless am getting something wrong.

You use SDK the same way in business logic as you do in a mobile app.

Exactly the same API, you do not need to learn anything new:

Backendless.Data.of( YOUR-CLASS.class ).find(…

Oh, sorry Mark, please confirm the console generated business logic code contains the Backendless SDK, I can see the jar in the “libs” folder.

Yes. it does.

Thank you, please mark the topic as solved.

Thumbs up.