Backendless Support
 
Solved

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

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

Regards,

George

Leave a Comment

Comments (10)

photo
1

What database are you connecting to?

photo
1

My tables on backendless

photo
1

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

photo
1

That am doing.

Consider my custom event handler below

  1. @BackendlessEvent( "CountObjectsIn4TablesEventHandler" )
  2. public class CountObjectsIn4TablesEventHandler extends com.backendless.servercode.extension.CustomEventHandler
  3. {
  4. @Override
  5. public Map handleEvent( RunnerContext context, Map eventArgs )
  6. {
  7. // add your code here
  8. return Collections.emptyMap();
  9. }
  10. }

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:

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

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

photo
1

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.

photo
1

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.

photo
1

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(.......

photo
1

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

photo
1

Yes. it does.

photo
1

Thank you, please mark the topic as solved.

Thumbs up.

photo