Hikaricp - Unable to get driver for JDBC URL

Asked

Viewed 3,806 times

1

I’m trying to implement the Hikaricp to take care of the connections in a web project.

Steps in a row:

  1. I created a Dynamic Web Project
  2. I added the .jars needed within WEB-INF/lib (HikariCP-2.3.5.jar / sfl4j-api.1.7.10.jar / sfl4j-jdk.1.7.10.jar / javassist.jar - mysql-connector-java-5.1.35-bin.jar)
  3. I created a class that will serve the connections (HikariPool)

Additional information:

  1. Java version: 1.8.0_31
  2. JDK: 1.7.0_45

Follow the codes used:

Hikaripool

package br.com.conexao;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class HikariPool {

    private static HikariPool instance = null;
    private HikariDataSource ds = null;

    static {
        try {
            instance = new HikariPool();
        } catch (Exception e) {
            System.out.println("Erro caiu aqui: " + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);

        }

    }

    private HikariPool() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/teste");
        config.setUsername("teste");
        config.setPassword("teste");
        //config.addDataSourceProperty("cachePrepStmts", "true");
        //config.addDataSourceProperty("prepStmtCacheSize", "250");
        //config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        //config.addDataSourceProperty("useServerPrepStmts", "true");

        ds = new HikariDataSource(config);
    }

    public static HikariPool getInstance() {
        return instance;
    }

    public Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

}

So on the main page as a test, I’m using the following code:

index.jsp

try {

    Logger logger = Logger.getLogger(this.getClass().getSimpleName());
    Statement stmt = null;
    ResultSet rset = null;
    Connection conn = null;

    HikariPool pool = HikariPool.getInstance();

    try {
        conn = pool.getConnection();
        stmt = conn.createStatement();
        rset = stmt.executeQuery("select * from psw_relatorio_server");
        while (rset.next()) {
            System.out.println(String.format("[%s]",
                    rset.getString(1)));
        }
    } catch (SQLException se) {
        logger.log(Level.SEVERE, se.getMessage());
    } finally {
        if (stmt != null) {
            stmt.close();
        }

        if (rset != null) {
            rset.close();
        }

        if (conn != null) {
            conn.close();
        }

    }

} catch (Exception e) {
    e.printStackTrace();
}

Error:

Erro caiu aqui: Unable to get driver for JDBC URL jdbc:mysql://localhost:3306/teste
mar 24, 2015 3:44:44 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/webviewer2] threw exception [javax.servlet.ServletException: java.lang.ExceptionInInitializerError] with root cause
java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(Unknown Source)
    at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:52)
    at com.zaxxer.hikari.pool.PoolUtilities.initializeDataSource(PoolUtilities.java:107)
    at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:153)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:61)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:49)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:80)
    at br.com.conexao.HikariPool.<init>(HikariPool.java:35)
    at br.com.conexao.HikariPool.<clinit>(HikariPool.java:16)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:236)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

Doubt, what would be the problem if I put the connection jar in the lib? NOTE: Connection data such as user, database name and the like were replaced by company security issues. But I’m sure they’re right.

1 answer

3


Well, I changed the connection form of the Hikaripool class and it worked:

Hikaripool

package br.com.conexao;

import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class HikariPool {

    private static HikariPool instance = null;
    private HikariDataSource ds = null;

    static {
        try {
            instance = new HikariPool();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }

    }

    //ALTERADO
    private HikariPool() {
        ds = new HikariDataSource();

        ds.setMaximumPoolSize(10);
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setJdbcUrl("jdbc:mysql://localhost:3306/teste");
        ds.setUsername("teste");
        ds.setPassword("teste");
        ds.addDataSourceProperty("cachePrepStmts", "true");
        ds.addDataSourceProperty("prepStmtCacheSize", "250");
        ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        ds.addDataSourceProperty("useServerPrepStmts", "true");
    }

    public static HikariPool getInstance() {
        return instance;
    }

    public Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.