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); }
}