JDBC Error

Hey,

I want to use JDBC to connect kdb.

1?Get jdbc.jar from http://code.kx.com/wsvn/code/kx/kdb%2B/c/?#a552c86fafdfd8cdfec7d5d6b5522f347

2?According to this article, I wrote the following code to have a test.

http://code.kx.com/wiki/Cookbook/InterfacingWithJava#Is_there_a_JDBC_interface.3F

3?Start q at port 5001

But, throw the error No suitable driver found for jdbc:q:127.0.0.1:5001

at this line: con = DriverManager.getConnection(“jdbc:apache:commons:dbcp:q”);

I can not find the reason, need help.

Thanks,

Roy

--------------------CODE-------------------------

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException; importorg.apache.commons.dbcp.ConnectionFactory;importorg.apache.commons.dbcp.DriverManagerConnectionFactory;importorg.apache.commons.dbcp.PoolableConnectionFactory;importorg.apache.commons.dbcp.PoolingDriver;importorg.apache.commons.pool.ObjectPool;importorg.apache.commons.pool.impl.GenericObjectPool; publicclassTestJdbc{publicTestJdbc(){  ObjectPool connectionPool=newGenericObjectPool(null);ConnectionFactory connectionFactory=newDriverManagerConnectionFactory("jdbc:q:[127.0.0.1:5001]("http://127.0.0.1:5001")",null);  PoolableConnectionFactory poolableConnectionFactory=newPoolableConnectionFactory(connectionFactory, connectionPool,null,null,false,true); try{Class.forName("org.apache.commons.dbcp.PoolingDriver");}catch(ClassNotFoundExceptione){e.printStackTrace();}  PoolingDriver driver=null;try{driver=(PoolingDriver)DriverManager.getDriver("jdbc:apache:commons:dbcp:");}catch(SQLExceptione){e.printStackTrace();}driver.registerPool("q", connectionPool);} publicConnectiongetConnection(){Connectioncon=null;try{con=DriverManager.getConnection("jdbc:apache:commons:dbcp:q");}catch(SQLExceptione){thrownewRuntimeException(e);}returncon;} publicstaticvoidmain(String[]args){TestJdbc t=newTestJdbc();Connectionconn=t.getConnection(); } }

first guess is that you dont have the kx jdbc.jar in your classpath.

some hints, although for mysql are likely relevant here too

http://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w

Hey, Charles

jdbc.jar is indeed included in my classpath.


?

I see you’re using IDEA. Try to clear caches and restart. I’ve found sometimes it forgets newly added libs, and a clear cache+restart fixes it. 

I have cleaned project(Project > Clean > Clean projects) and restart Eclipse. However, same error..

I tried something like these:

  1. Change database to mysql and add a jdbc jar of mysql(mysql-connector-java.jar)

just modified this line:

ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(“jdbc:mysql://127.0.0.1:3306/database”, “username”, “password”);

It’s all OK. I can get a connection.

  1. I used jdbc.java and c.java to built jdbc.jar on my PC, replaced current jdbc.jar.

same error.

So, I doubt that the jdbc.jar is unavaliable or something else.

Anyone knows?

If you want to use apache dbcp, try a fuller example here

https://svn.apache.org/repos/asf/commons/proper/dbcp/branches/TEST_DBCP_1_3_BRANCH/doc/ManualPoolingDriverExample.java

noting

// First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done.");

which for the kx jdbc should be

Class.forName(“jdbc”);

If you dont want pooling, just use some simpler jdbc example.

the following appears to work ok in netbeans with jdk1.8
using http://kx.com/q/c/jdbc.jar

and these jars from http://commons.apache.org/downloads/index.html

commons-dbcp2-2.0.jar

commons-logging-1.1.3.jar

commons-pool2-2.2.jar

results -

run:

Loading underlying JDBC driver.

Done.

Setting up data source.

Done.

Creating connection.

Creating statement.

Executing statement.

Results:

a03.0

b14.0

c25.0

BUILD SUCCESSFUL (total time: 0 seconds)

Code -

package jdbctest;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.Statement;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.apache.commons.pool2.ObjectPool;

import org.apache.commons.pool2.impl.GenericObjectPool;

import org.apache.commons.dbcp2.ConnectionFactory;

import org.apache.commons.dbcp2.PoolableConnection;

import org.apache.commons.dbcp2.PoolingDataSource;

import org.apache.commons.dbcp2.PoolableConnectionFactory;

import org.apache.commons.dbcp2.DriverManagerConnectionFactory;

public class ManualPoolingDriverExample {

    public static void main(String args) {

        System.out.println(“Loading underlying JDBC driver.”);

        try {

            Class.forName(“jdbc”);

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

        System.out.println(“Done.”);

        System.out.println(“Setting up data source.”);

        DataSource dataSource = setupDataSource(“jdbc:q:127.0.0.1:5000”);

        System.out.println(“Done.”);

        Connection conn = null;

        Statement stmt = null;

        ResultSet rset = null;

        try {

            System.out.println(“Creating connection.”);

            conn = dataSource.getConnection();

            System.out.println(“Creating statement.”);

            stmt = conn.createStatement();

            System.out.println(“Executing statement.”);

            rset = stmt.executeQuery(“q)(a:ab`c;b:0 1 2;c:3 4 5.)”);

            System.out.println(“Results:”);

            int numcols = rset.getMetaData().getColumnCount();

            while(rset.next()) {

                for(int i=1;i<=numcols;i++) {

                    System.out.print(“\t” + rset.getString(i));

                }

                System.out.println(“”);

            }

        } catch(SQLException e) {

            e.printStackTrace();

        } finally {

            try { if (rset != null) rset.close(); } catch(Exception e) { }

            try { if (stmt != null) stmt.close(); } catch(Exception e) { }

            try { if (conn != null) conn.close(); } catch(Exception e) { }

        }

    }

    

    public static DataSource setupDataSource(String connectURI) {

        ConnectionFactory connectionFactory =new DriverManagerConnectionFactory(connectURI,null);

        PoolableConnectionFactory poolableConnectionFactory =new PoolableConnectionFactory(connectionFactory, null);

        ObjectPool<PoolableConnection> connectionPool =new GenericObjectPool<>(poolableConnectionFactory);

        poolableConnectionFactory.setPool(connectionPool);

        PoolingDataSource<PoolableConnection> dataSource =new PoolingDataSource<>(connectionPool);

        return dataSource;

    }

}