Support Questions

Find answers, ask questions, and share your expertise

Not able to connect phoenix via java jdbc

avatar
Rising Star

i'm trying to connect to phoenix using java jdbc , but i not succeed to etablish connection ,there is exemple show me how to write url of connection ??, i succeed to connect to phoenix by using sqlline.py

". /sqlline.py node4.bigdatau:2181 " this is my code java , thx

  try {
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
    }catch (ClassNotFoundException e) {
		System.out.println("Where is your  JDBC Driver?");
		e.printStackTrace();
	}
   conn =  DriverManager.getConnection(" jdbc:phoenix:195.154.55.93:2181");
    System.out.println("got connection");
1 ACCEPTED SOLUTION

avatar
New Contributor

Hi @nejm hadjmbarek, I'm Posting my code, which is working Fine:

4469-2016-05-24.png

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class phoenix_hbase 
{
 public static void main(String[] args) throws SQLException 
 {
  @SuppressWarnings("unused")
  Statement stmt = null;
  ResultSet rset = null;
 
  try 
  {
   Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
  } 
  catch (ClassNotFoundException e1) 
  {
   System.out.println("Exception Loading Driver");
   e1.printStackTrace();
  }
  try
  {
   Connection con = DriverManager.getConnection("jdbc:phoenix:172.31.124.43:2181:/hbase-unsecure");  //172.31.124.43 is the adress of VM, not needed if ur running the program from vm itself
   stmt = con.createStatement();
    
   PreparedStatement statement = con.prepareStatement("select * from javatest");
   rset = statement.executeQuery();
   while (rset.next()) 
   {
    System.out.println(rset.getString("mycolumn"));
   }
   statement.close();
   con.close();
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
 }
}

View solution in original post

5 REPLIES 5

avatar

Hi @nejm hadjmbarek, can you post the error you're seeing? From the above, it looks like you've forgotten to include the ZooKeeper znode for HBase: "/hbase-unsecure". Try with the following connection string instead: "jdbc:phoenix:195.154.55.93:2181:/hbase-unsecure".

If you need a simple simple java application (with maven pomfile) that embeds and uses the Phoenix JDBC driver, take a look here.

avatar
Super Guru

The leading space in your JDBC url is likely problematic.

If that doesn't fix it, you should provide the error you are experiencing.

avatar
Rising Star

thx ,@Randy Gelhausen,@Josh Elser

i get this error "

Exception in thread "main" java.sql.SQLException: ERROR 103 (08004): Unable to establish connection

Caused by: java.io.IOException: java.lang.reflect.InvocationTargetException" , yes i 'am using simple java app with maven pomfile but i get always this error .

avatar
Expert Contributor

@nejm hadjmbarek Remove the space inside connection string. Add zookeeper znode in the connection string .

jdbc:phoenix:<quorom>:<port>:[zk_rootNode]

If this doesn't work plz paste whole stack trace.

avatar
New Contributor

Hi @nejm hadjmbarek, I'm Posting my code, which is working Fine:

4469-2016-05-24.png

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class phoenix_hbase 
{
 public static void main(String[] args) throws SQLException 
 {
  @SuppressWarnings("unused")
  Statement stmt = null;
  ResultSet rset = null;
 
  try 
  {
   Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
  } 
  catch (ClassNotFoundException e1) 
  {
   System.out.println("Exception Loading Driver");
   e1.printStackTrace();
  }
  try
  {
   Connection con = DriverManager.getConnection("jdbc:phoenix:172.31.124.43:2181:/hbase-unsecure");  //172.31.124.43 is the adress of VM, not needed if ur running the program from vm itself
   stmt = con.createStatement();
    
   PreparedStatement statement = con.prepareStatement("select * from javatest");
   rset = statement.executeQuery();
   while (rset.next()) 
   {
    System.out.println(rset.getString("mycolumn"));
   }
   statement.close();
   con.close();
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
 }
}