Tuesday, June 14, 2011

Connecting to mvBase 3.0 with Java

A couple posts back I went through the basics of PICK (mvBase). I finally got a chance to connect to it using the mvBase 3.0 MVSP with Java. First off we need the mvapi.jar which acts as our jdbc like interface (UPDATE: MVSP alone is actually hard to use in a web app as it does not handle any connection pooling and basically only allows one active connection at a time. It also doesn't work in a standard JDBC compliant manner which is why I started a project https://github.com/asharif/mv-session-mgr). I downloaded it off the tigerlogic ftp site ftp://ftp.tigerlogic.com/pub/MVSP/Java/

There are some samples in that zip file as well. Then it was just a matter of writing a bit of code:

package comuniversalmetalsmvbase;

import com.tigr.mvapi.MVConnection;
import com.tigr.mvapi.MVConstants;
import com.tigr.mvapi.MVStatement;
import com.tigr.mvapi.ResultSet.MVResultSet;
import com.tigr.mvapi.exceptions.MVException;
import java.util.Properties;

/**
 *
 * @author arash
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



        Properties props = new Properties();
        props.setProperty("username", "MVSP");
        props.setProperty("password", "");
        String url = "jdbc:mv:mvbase:192.168.1.5:9010";
        String account = "SYSPROG";
        String AM = MVConstants.AM;
        String query;
        MVConnection connection = null;
        try
        {
            connection = new MVConnection(url, props);

            /**
             * Execute a query and display the results:
             */
            MVStatement mvStatement = connection.createStatement();
            connection.logTo(account, "");
           // query = "LIST CUSTOMERS (I";
            //boolean result = mvStatement.execute(query);
            //MVResultSet results = mvStatement.getResultSet();
            MVResultSet results = mvStatement.executeQuery("PEOPLE", "", "", "FIRST-NAME");
            while (results.next())
            {
                String row = results.getCurrentRow();
                System.out.println(row);
            }


        }
        catch (MVException e)
        {
            System.out.println(e.toString());

            
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (MVException e)
                {
                    e.printStackTrace();
                }
            }
        }

    }

}



3 comments:

  1. I am trying to use d3 database with java Api MVSP
    i have download the mvapi.jar and keeping the above code as reference ,the only change i did is
    with username and password ,account .But when i am trying to run the program getting com.tigr.mvapi.exceptions.MVException: server error with errorCode 1023. i check the console but not able to figure out the actual cause.
    please suggest where i am doing wrong.

    ReplyDelete
  2. Post inquiries about MVSP to the TigerLogic forum, or contact the site's VAR. If they don't know, look for anyone in forum talking about MVSP and get them to work with the VAR. If you can't see the TL forum see the Google Group "MVDBMS".

    ReplyDelete
    Replies
    1. MVSP out of the box doesn't work with connection pooling. My project https://github.com/asharif/mv-session-mgr does.

      Delete