3

I installed hadoop,hbase and phoenix in ubuntu 14 and runed a simple example for connecting phoenix in eclipse. It's work and execute my query.

public static void execute() throws ClassNotFoundException {

    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    PreparedStatement ps = null;

    try {
        connection = DriverManager.getConnection("jdbc:phoenix:localhost");

        statement = connection.createStatement();

        // Query for table
        ps = connection.prepareStatement("select * from dum.user_info where id=25 ");
        rs = ps.executeQuery();
        System.out.println("Table Values");
        while (rs.next()) {
            Integer myKey = rs.getInt(1);
            String myColumn = rs.getString(2);
            System.out.println("\tRow: " + myKey + " = " + myColumn);
            System.out.println("" + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " "
                    + rs.getString(6) + " " + rs.getString(7));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } 

}

Now I'd like to run a web application that connect to phoenix. I installed tomcat and create a web dynamic application, I can run it and view page in browser but when I add execute function, it doesn't work.

 public class LoginServlet extends HttpServlet {

private LoginService userValidationService = new LoginService();
private TodoService todoService = new TodoService();

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    try {
        phoenixHelper.execute();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(
            request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String name = request.getParameter("name");
    String password = request.getParameter("password");

    boolean isUserValid = userValidationService.isUserValid(name, password);

    if (isUserValid) {
        request.getSession().setAttribute("name", name);
        response.sendRedirect("/list-todos.do");
    } else {
        request.setAttribute("errorMessage", "Invalid Credentials!");
        request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(
                request, response);
    }
}

}

I faced this error:

java.sql.SQLException: No suitable driver found for jdbc:phoenix:localhost

Can anybody tell me how to run a web application that connect to phoenix?

It's pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.in28minutes</groupId>
<artifactId>in28Minutes-first-webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-core</artifactId>
        <version>4.6.0-HBase-1.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.7</source>
                    <target>1.7</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.geekcap.javaworld.phoenixexample.PhoenixExample</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

NASRIN
  • 475
  • 7
  • 22
  • @Kayaman all dependency is added. – NASRIN Aug 28 '16 at 14:01
  • @Kayaman What library do I need to add in maven? – NASRIN Aug 28 '16 at 15:44
  • @Kayaman You're right. but I create maven project and all dependencies added automatically. Do I should add driver in another way?? – NASRIN Aug 28 '16 at 15:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122041/discussion-between-nasrin-and-kayaman). – NASRIN Aug 28 '16 at 15:59
  • I working with java 1.7 – NASRIN Aug 28 '16 at 16:09
  • I tested Class.forName() and it doesn't work – NASRIN Aug 28 '16 at 16:15
  • Thanks for answer, but I can connect to phoenix in java application but in a web application it doesn't work! – NASRIN Aug 28 '16 at 16:17
  • @Kayaman I installed jdbc6 like this https://www.mkyong.com/maven/how-to-add-oracle-jdbc-driver-in-your-maven-local-repository/ and jdbc in pom.xml but it doesn't work and get the error! – NASRIN Aug 29 '16 at 14:18
  • 1
    That's the oracle jdbc driver, not the Phoenix jdbc driver – OneCricketeer Aug 29 '16 at 14:21
  • @Kayaman :D when I add it in dependency I faced error in pom.xml. but after install jdbc , this file has not error. Acutally I don't khnow how to add ojdbc in eclipse! – NASRIN Aug 29 '16 at 14:23
  • 1
    Read the Phoenix instalation page. It tells you exactly which jar file you need for the client. It isn't ojdbc – OneCricketeer Aug 29 '16 at 14:28
  • @cricket_007 I installed phoenix and worked with it in command line. I run sqlline.py localhost and connect to jdbc:localhost:phoenix in command line. but in web application in eclipse I can't connect. – NASRIN Aug 29 '16 at 14:34
  • Is the webserver even running on the same machine as hbase? https://streever.atlassian.net/wiki/plugins/servlet/mobile?contentId=11993090#content/view/11993090 – OneCricketeer Aug 29 '16 at 14:39
  • @cricket_007 exactly, I want add phoenix-client.jar and hbase-client.jar in maven project in eclipse, how can I add them? other dependency in pom automatically downloaded. – NASRIN Aug 29 '16 at 14:47
  • I don't know the Maven dependency, but you should be able to add both of the jar files in the `lib/` directory, and they will be included in the classpath – OneCricketeer Aug 29 '16 at 15:26

1 Answers1

0

Try loading the driver before creating the connection.

Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
talin
  • 179
  • 3
  • 12