Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

JDBC Connection to phoenix failed

avatar
Explorer

I have tried the below sample code from Phoenix website by copying the phoenix client jar from /usr/hdp/current/phoenix-client in my server. I tried running it locally as well as on the server, but i keep getting the below error:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:phoenix:zk1,zk2,zk3:2181:/hbase-unsecure

Are there any other prerequisites other than including the client jar in the code to connect to phoenix?

PS: I had no problems connecting to phoenix from command line.

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 test {

	public static void main(String[] args) throws SQLException {
		Statement stmt = null;
		ResultSet rset = null;
		
		Connection con = DriverManager.getConnection("jdbc:phoenix:zk1,zk2,zk3:2181:/hbase-unsecure");
		stmt = con.createStatement();
		
		stmt.executeUpdate("create table test (mykey integer not null primary key, mycolumn varchar)");
		stmt.executeUpdate("upsert into test values (1,'Hello')");
		stmt.executeUpdate("upsert into test values (2,'World!')");
		con.commit();
		
		PreparedStatement statement = con.prepareStatement("select * from test");
		rset = statement.executeQuery();
		while (rset.next()) {
			System.out.println(rset.getString("mycolumn"));
		}
		statement.close();
		con.close();
	}
}
4 REPLIES 4

avatar
Master Mentor

@ROHIT AILA

If you want to connect using Phoenix APIs (phoenix URL"jdbc:phoenix:") then you will have to load Driver Class of type "

"org.apache.phoenix.jdbc.PhoenixDriver"

import org.apache.phoenix.jdbc.PhoenixDriver;


Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
Statement stmt = null;		
ResultSet rset = null;				
Connection con = DriverManager.getConnection("jdbc:phoenix:zk1,zk2,zk3:2181:/hbase-unsecure");

.

avatar
Explorer

@Jay SenSharma Is it possible to connect to phoenix on server from local machine?

avatar
Rising Star

You need phoenix-client libs in classpath for remote PQS connection.