B4J Question Connecting to a NoSQL database

Ray Y

Member
So, I have a very limited knowledge of Java, so maybe someone has already done this and will save me the trouble of trial and error on my part. Assistance is greatly appreciated.

I see references and examples here to using JDBC to connect to "any" database, but then all the postings show a SQL database and/or using subsequent SQL statements.

I would like to connect to a non-SQL database (here called "qm"). I have a vendor-supplied JDBC .jar file. It exposes database specific commands - not SQL statements. The (my simplified) example they give for using it is:

import qmclient.*;
import java.io.*;

class ex
{
private static qmclient qm;
public static void main(String[] args) throws IOException
{
int fSales; // SALES file
String SaleRec; // SALES record

qm = new qmclient();
if (!qm.ConnectLocal("demo"))
{
System.out.println("Failed to connect to server: "+ qm.Error);
return;
}

// Open files
fSales = qm.Open("SALES");
if (fSales == 0)
{
System.out.println("Cannot open SALES file");
return;
}
SaleRec = qm.Read(fSales, "1234");
.
.
.


So, the qmclient is the database pure-java driver, and the ConnectLocal, Open, Read, etc are all API specific to this database.

Obviously, my question is: how do I implement this in B4J?

I get including the #AdditionalJar line, but how do I associate my database object in B4J to it? Not Private qm as SQL as in the examples posted here, since this isn't a SQL driver.

I'm obviously missing something probably pretty obvious...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that there is a library for MongoDB: MongoDB - Documents database

B4X:
Dim qm As JavaObject
qm.InitializeNewInstance("qmclient.qmclient", Null)
If qm.RunMethod("ConnectLocal", Array("demo")) = False Then
 Log("Failed to connect")
 Return
End If
Dim fSales As Int = qm.RunMethod("Open", "SALES"))

Note that based on this API I can say that this library was not written by a Java expert.
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
Hi Ray Y,
i know vey well openqm and other multivalue database (pick, d3,)

how do you plan to use b4j and openqm?
what is your project?
 
Upvote 0

Ray Y

Member
Thank you Erel. I was actually using JavaObject and RunMethod but my syntax was a bit off - thank you for clarifying. I haven't tried it yet but knowing you, I'm sure it will work perfectly. RE: Not Java experts: Neither am I, which further confused the issue I'm sure. But as far as the author of the API, all I can say is they are database experts, not Java experts. Of course, now I'm curious as to why you can say that. How should it have been implemented?

Giannimaione: I have been working with MV for over 30 years, and I love the paradigm and the language. I'm consulting right now on a project developing a new front end for a small company that is using Universe. I'm embarrassed to admit I'm very new to the B4x world (why didn't I know about this environment before?), but I'm having a great time with it. So, I figured since one of their requirements was both Win and Mac clients, this would be a great time to try and merge the two (MV and B4x.)

It's nice to know there are others of us still around. I think we're a dying breed. Feel free to email me and we can chat further.
 
Upvote 0

Ray Y

Member
I tried it, and it does work (except I had to change "SALES" to an object.)

But yes, it's working. Thanks again.
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
wow, great!
can you describe a scenario and post a sample code?
 
Upvote 0

Ray Y

Member
Here is what I came up with as a connection test:

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
   #CommandLineArgs:
   #MergeLibraries: True
   #AdditionalJar: qmclient.jar
#End Region

Sub Process_Globals
   Private qm As JavaObject
End Sub

Sub AppStart (Args() As String)

   qm.InitializeNewInstance("qmclient.qmclient", Null)

   ' Note that here QMSYS refers to the QM account we want to do our work in
   If qm.RunMethod("ConnectLocal", Array("QMSYS")) = False Then
       Log("Failed to connect")
       Return
   End If
   ' Open the VOC file
   Dim fVoc As Int = qm.RunMethod("Open", Array("VOC"))
   ' Select all the records to select list 0
   qm.RunMethod("Select", Array(fVoc, 0))
   ' Get the list as a delimited string
   Dim List As String = qm.RunMethod("ReadList", Array(0))
 
   '    This doesn't work for some reason. The QMClient docs say it should.
   'List = List.Replace(qm.GetField("FM"), CRLF)
   List = List.Replace(Chr(254), CRLF)

   Log(List)
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

All it does is connect to the QMSYS account, select the VOC file, and print the IDs to the B4J Log window. But yes, it works.

I am using the regular QM for Windows as well as the "pure Java" .jar file, both from the Zumasys Downloads page:

http://www.openqm-zumasys.com/downloads/

I had noted that I am investigating using B4J to connect to Universe, but I already have QM installed on this PC, so that's why I am using that instead. I'm sure Universe has a "pure Java" driver as well.

For anyone who doesn't know what QM is, it's a NoSql database that stores key/value pairs as multidimentional (2, 3 and 4 dimensions and beyond) records (e.g. you can store an entire invoice with its multiple line items and multiple prices for each line item as a single database record, requiring only a single disk read to retrieve the entire data set) and has it's own native Basic programming language. It's been around for more than 50 years and is still being used for new applications because it does things that relational databases just can't do. Consider it instead of MySQL or SQLite if you have data that can't fit neatly into single records (like XML data or hierarchies.) It's not free, but it isn't expensive. And yes, it works with B4X!

http://www.openqm-zumasys.com
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
ok!
and now how to connect on remote openqm 2.6-6 on server linux ?
i have tried with method "Connect" on local host and it work (QM for windows) .... ("127.0.0.1",-1,"user_name","password","account")
 
Upvote 0

Ray Y

Member
You should just be able to change the IP address in that Connect statement to the remote machine. But, I have not tried this QMClient connector with the Linux OpenQM version, which is several years old. If I get a chance today, I will fire up my Linux box to try it and see what happens.

There is an updated Linux version available for download from that Zumasys site, but I imagine it is limited to the free personal license (2 users) unless you pay for more users. But it might be worth trying to connect to that updated version to see if it works if you have problems with the OpenQM version.

What happens when you try it with the remote IP instead of the local?
 
Upvote 0

Ray Y

Member
A follow up: I was in touch with Martin Phillips this morning, the author of QM, on a different subject. I asked him about this, and he said using a current version of QMClient to connect to the 2007 OpenQM version will almost surely not work. You may be able to use a version 2.6 of QMClient, but he pointed out that there have been some significant changes and bug fixes made since then.

Do you have a production server running 2.6?
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
yes! yes! yes!
test on remote server linux (openqm) and it works! wonderful !
 
Upvote 0

Peter Gonzalez

Member
Licensed User
Here is what I came up with as a connection test:

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
   #CommandLineArgs:
   #MergeLibraries: True
   #AdditionalJar: qmclient.jar
#End Region

Sub Process_Globals
   Private qm As JavaObject
End Sub

Sub AppStart (Args() As String)

   qm.InitializeNewInstance("qmclient.qmclient", Null)

   ' Note that here QMSYS refers to the QM account we want to do our work in
   If qm.RunMethod("ConnectLocal", Array("QMSYS")) = False Then
       Log("Failed to connect")
       Return
   End If
   ' Open the VOC file
   Dim fVoc As Int = qm.RunMethod("Open", Array("VOC"))
   ' Select all the records to select list 0
   qm.RunMethod("Select", Array(fVoc, 0))
   ' Get the list as a delimited string
   Dim List As String = qm.RunMethod("ReadList", Array(0))
 
   '    This doesn't work for some reason. The QMClient docs say it should.
   'List = List.Replace(qm.GetField("FM"), CRLF)
   List = List.Replace(Chr(254), CRLF)

   Log(List)
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

All it does is connect to the QMSYS account, select the VOC file, and print the IDs to the B4J Log window. But yes, it works.

I am using the regular QM for Windows as well as the "pure Java" .jar file, both from the Zumasys Downloads page:

http://www.openqm-zumasys.com/downloads/

I had noted that I am investigating using B4J to connect to Universe, but I already have QM installed on this PC, so that's why I am using that instead. I'm sure Universe has a "pure Java" driver as well.

For anyone who doesn't know what QM is, it's a NoSql database that stores key/value pairs as multidimentional (2, 3 and 4 dimensions and beyond) records (e.g. you can store an entire invoice with its multiple line items and multiple prices for each line item as a single database record, requiring only a single disk read to retrieve the entire data set) and has it's own native Basic programming language. It's been around for more than 50 years and is still being used for new applications because it does things that relational databases just can't do. Consider it instead of MySQL or SQLite if you have data that can't fit neatly into single records (like XML data or hierarchies.) It's not free, but it isn't expensive. And yes, it works with B4X!

http://www.openqm-zumasys.com





Hello Ray, I'm also a veteran Pick programmer looking for modernizing character screens. I'll be purchasing this software next week and hope to dig in over the winter.

Thanks,
-Peter G
 
Upvote 0
Top