Initializing Java Connector

To use the functionality of DHTMLX Connector, first you should initialize it.

Generally, both client- and server-sides are concerned in it.

Client side

If you need just load data from database (with or without data preprocessing)

Specify connector file in the load method of a component.

//index.html
myGrid = new dhtmlXGridObject("someContainer");// initializes grid
...
myGrid.load("myconnector.do");

If you need to perform any update operations

1) Specify connector file in the load method of a component;

2) Initialize dhtmlxDataProcessor on the client side (read more about it here).

//index.html
myGrid = new dhtmlXGridObject("someContainer");// initializes grid
...
myGrid.load("myconnector.do");
myDP = new dataProcessor("myconnector.do");// initializes dhtmlxDataProcessor
myDP.init(myGrid);

Samples client-side initialization

Server side

  1. Import the appropriate connector files into the page.
  2. Create Database connection.
  3. Instantiate the connector object. Linking variable is a mandatory parameter in all constructors. The second parameter(database type)- optional. By default, “MySQL”. Another possible is “Postgre”
  4. The last step is data configuration.
import java.sql.Connection;
import java.sql.DriverManager;
 
import com.dhtmlx.connector.*;
 
 
/**
 * The Class BasicConnector.
*/
public class BasicConnector extends ConnectorServlet {
    @Override
    protected void configure() {
        //obtain DB connection
        Connection conn=null;
        try {
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
          conn = DriverManager.getConnection("jdbc:mysql://localhost/sampleDB","root","");
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
        //Initializes connector
        GridConnector c = new GridConnector(conn);
        //configures the used table and fields
        c.render_table("grid50000", "item_id", "item_nm,item_cd");
    }
}

Samples server-side initialization

Thread-safe code initialization

The code above has been used starting from the initial version, but it has one disadvantage - it is not thread safe. Starting from the version 1.0 you can create a thread safe connector. Its init code will look as:

public class BasicConnector extends ThreadSafeConnectorServlet{
    @Override
        protected void configure(HttpServletRequest req, HttpServletResponse res) {
        //obtain DB connection
        Connection conn=null;
        try {
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
          conn = DriverManager.getConnection("jdbc:mysql://localhost/sampleDB","root","");
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
        //Initialize connector
        GridConnector c = new GridConnector(conn);
            c.servlet(req, res);
        //configure used table and fields
        c.render_table("grid50000", "item_id", "item_nm,item_cd");
    }
}

There are two main differences:

  1. The class extends ThreadSafeConnectorServlet.
  2. During configuration, the servlet method of the connector class must be called.
Back to top