Introduction to the combination of time series database KDB and Java--3 write implementation based on KDB JDBC __c#

Source: Internet
Author: User

KDB source code also provides KDB based on JDBC implementation, in fact, KDB JDBC implementation is based on the kdb of the underlying C.java implementation of the encapsulation, providing a JDBC-oriented friendly interface. But I personally do not tend to use the JDBC interface, many features are not implemented, such as the most important batch function, all SQL must be a single execution. Here is a brief description of how to use JDBC Access KDB to cache the socket connection via the Apache Commons pool.

The Kdbjdbcservice implementation inserts data into the KDB:

</pre></p><p><pre name= "code" class= "java" >/** * * */package KX;
Import java.sql.Connection;


Import java.sql.PreparedStatement;


Import Lombok.extern.log4j.Log4j2;
Import Org.apache.commons.pool2.ObjectPool;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;


Import Org.springframework.stereotype.Service;
    /** * @author cloudlu */@Log4j2 @Service public class Kdbjdbcservice {@Autowired @Qualifier ("Jdbcpool")


    Private objectpool<connection> pool; /** * Insert data to specified table * * @param data *: Data Object Save to KDB * @re
        Turn * * Public boolean insertkdbdata (final kdbdata data) {Connection con = null;
            try {con = pool.borrowobject ();
            Final PreparedStatement insert = con. preparestatement ("q) {' t insert 0n!a::x}"); Insert.settime(1, Data.gettime ());
            Insert.setstring (2, Data.getsym ());
            Insert.setdouble (3, Data.getprice ());
            Insert.setint (4, Data.getsize ());
            Insert.setboolean (5, Data.isstop ());
            Insert.setstring (6, String.valueof (Data.getcond ()));
            Insert.setstring (7, String.valueof (Data.getex ()));
            Insert.executeupdate ();
        return true;
        catch (Final Exception e) {log.error ("fail to insert Data", e);
                finally {if (null!= con) {try {pool.returnobject (con);
                catch (Final Exception e) {log.error ("fail to return con-back to poll", E);
    }} return false; }
}

@Qualifier ("Jdbcpool") is injected by spring: New Genericobjectpool<connection> (New Kdbjdbcpoolfactory ())

Kdbjdbcpoolfactory Implementation Cache Socket connection:

Package kx;
Import java.sql.Connection;

Import Java.sql.DriverManager;
Import Org.apache.commons.pool2.BasePooledObjectFactory;
Import Org.apache.commons.pool2.PooledObject;
Import Org.apache.commons.pool2.impl.DefaultPooledObject;
Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.beans.factory.annotation.Qualifier; public class Kdbjdbcpoolfactory extends basepooledobjectfactory<connection> {@Autowired @Qualifier ("Ticket

    Plant ") Private kdbserver server;
        @Override public Connection Create () throws Exception {Class.forName (JDBC.class.getName ()); Final Connection localconnection = Drivermanager.getconnection ("jdbc:q:" + server.gethost () + ":" + Serve
        R.getport (), Server.getusername (), Server.getpassword ());
    return localconnection; @Override Public pooledobject<connection> Wrap (final Connection con) {return new Defaultpooledob Ject<
    Connection> (con); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.