java—如何在hadoop和phoenix上运行web应用程序

6tr1vspr  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(362)

我在ubuntu14中安装了hadoop、hbase和phoenix,并运行了一个在eclipse中连接phoenix的简单示例。它的工作和执行我的查询。

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();
    } 

}

现在我想运行一个连接到phoenix的web应用程序。我安装了tomcat并创建了一个web动态应用程序,我可以运行它并在浏览器中查看页面,但是当我添加execute函数时,它就不起作用了。

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);
    }
}

}

我面对这个错误:

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

有人能告诉我如何运行一个连接到phoenix的web应用程序吗?
它是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>
insrf1ej

insrf1ej1#

尝试在创建连接之前加载驱动程序。

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

相关问题