Connecting to apache phoenix using JDBC and Java

Zeliax picture Zeliax · Mar 28, 2017 · Viewed 8.8k times · Source

I have a small java program in which I try to establish a connection to a remote Phoenix server I have running.

package jdbc_tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PhoenixTest {

    static final String JDBC_DRIVER = "org.apache.phoenix.jdbc.PhoenixDriver";
    static final String IP = "<placeholder>"
    static final String PORT = "<placeholder>"
    static final String DB_URL = "jdbc:phoenix://" + IP + ":" + PORT + "/";

    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;

        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");

            System.out.println("Connecting to database..");

            conn = DriverManager.getConnection(DB_URL);

            System.out.println("Creating statement...");

            st = conn.createStatement();
            String sql;
            sql = "SELECT DISTINCT did FROM sensor_data";
            ResultSet rs = st.executeQuery(sql);

            while(rs.next()) {
                String did = rs.getString(1);
                System.out.println("Did found: " + did);
            }

            rs.close();
            st.close();
            conn.close();

        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (st != null)
                    st.close();
            } catch (SQLException se2) {
            } // nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } // end finally try
        } // end try
    System.out.println("Goodbye!");
    }
}

When I try to run my program I get:

java.sql.SQLException: No suitable driver found for jdbc:phoenix://<ip>:<port>/

I have added the following jar from the Apache Phoenix distribution:

phoenix-4.9.0-HBase-1.2-client.jar

which matches my Phoenix and Hbase vesions.

What am I doing wrong?

Answer

YCF_L picture YCF_L · Mar 28, 2017

I think you are missing the name of your database and also the URL seams not correct:

"jdbc:phoenix://" + IP + ":" + PORT + "/" + DB_NAME
//--------------------------------------------^^

You have to try with this URL Instead :

Connection con = DriverManager.getConnection("jdbc:phoenix:<IP>:<port>:/<DB_NAME>");