H2 console error: No suitable driver found for 08001/0

纵然是瞬间 提交于 2020-01-03 16:51:12

问题


Hello Im having problem with viewing my schema in H2 console Database:

Im using spring boot:

spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE;MVCC=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

this is my login page:

so what i see inside is standard console view , without my tables and yet my app is working fine.


回答1:


In my case, the problem was that I implemented a custom Filter (see here, here and here) and the custom HttpServletRequestWrapper needs to take care of the H2 console login request which comes with the form data (including Driver Class input) and parse it as parameters:

public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {

private String body;

public MyHttpServletRequestWrapper(HttpServletRequest request) {
    super(request);
    this.body = IOUtils.toString(request.getReader());
    //...
}

@Override
public Enumeration<String> getParameterNames() {
    if (!parsedParams)
        parseParams();

    List<String> result = Collections.list(super.getParameterNames());
    result.addAll(parameters.keySet());

    return Collections.enumeration(result);
}

private void parseParams() {
    if (!body.isEmpty()) {
        String[] rps = body.split("&");

        for (String rp : rps) {
            String[] kv = rp.split("=");
            try {
                parameters.put(kv[0], kv.length > 1 ? new String[]{URLDecoder.decode(kv[1], "UTF-8")} : new String[]{""});
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        parameters.setLocked(true);
        parsedParams = true;
    }
}

@Override
public Map<String, String[]> getParameterMap() {
    if (!parsedParams)
        parseParams();

    Map<String, String[]> s = super.getParameterMap();
    return Stream.concat(parameters.entrySet().stream(), s.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
public String getParameter(String name) {
    return parameters.get(name) != null ? parameters.get(name)[0] : super.getParameter(name);
}

@Override
public String[] getParameterValues(String name) {
    String[] s = super.getParameterValues(name);
    return ArrayUtils.addAll(s, parameters.get(name));
}
}



回答2:


Usually "Test connection" is working (green line after login form), but connecting does not. This can be caused by handling of headers, which can be caused by filters or security config. For example, I was hit by this twice:

  1. I have same symptoms and problem was caused by usage of logging. Removing dependency helped to get rid of No suitable driver found for 08001/0 message while logging into h2-console:

    <dependency>
        <groupId>org.zalando</groupId>
        <artifactId>logbook-spring-boot-starter</artifactId>
    </dependency>
    

I am not sure, if it is bug of given project, or by wrong usage/config.

  1. Configuring OAuth2 does hit me also. I need to exclude h2-console uri from web and also http security. See some info here. See snippets:

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(
            securedEnabled = true,
            jsr250Enabled = true,
            prePostEnabled = true
    )
    @RequiredArgsConstructor
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        public void configure(WebSecurity web) {
            web.ignoring().antMatchers("/h2-console/**");
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    // only part of config is shown here...
                    .authorizeRequests()
                    .antMatchers("/",
                            "/error",
                            "/favicon.ico",
                            "/**/*.png",
                            "/**/*.gif",
                            "/**/*.svg",
                            "/**/*.jpg",
                            "/**/*.html",
                            "/**/*.css",
                            "/**/*.js",
                            "/h2-console/**")
                    .permitAll()
                    .antMatchers("/auth/**", "/oauth2/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .oauth2Login();
        }
    }
    



回答3:


When I encountered this problem I was following a spring boot tutorial. In this tutorial, my spring boot guru used the following code in the application.properties:

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:someName

The last line is not necessary because these are the default configurations done by spring boot:

spring.datasource.url=jdbc:h2:mem:testdb  
spring.datasource.driverClassName=org.h2.Driver  
spring.datasource.username=sa  
spring.datasource.password=  
spring.h2.console.enabled=false

When I removed it, all worked as intended.




回答4:


Use entire String for connection in H2 Console i.e. , You will see only your tables

jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE;MVCC=FALSE



来源:https://stackoverflow.com/questions/39854172/h2-console-error-no-suitable-driver-found-for-08001-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!