zoukankan      html  css  js  c++  java
  • 解决ClassNotFoundException: org.h2.Driver

    解决方案:

    pom.xml里面这样写

        <!--H2数据库-->
        <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
        <dependency>
          <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>RELEASE</version>
         <scope>compile</scope>
        </dependency>

    不要如下这样写

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <scope>test</scope>
    </dependency>

    参考网站:https://stackoverflow.com/questions/36551896/java-lang-classnotfoundexception-org-h2-jdbcx-jdbcdatasource

    多看看外国网站,还是很有帮助的

    我的小DEMO

    一、类

    package com.imooc.util;
    
    import org.junit.Test;
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.Properties;
    
    /**
     *
     * @author ping
     */
    public class H2DataBaseTest {
    
        @Test
        public static void main(String[] args) throws Exception {
            InputStream inputStream = H2DataBaseTest.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            String driverClass = properties.getProperty("jdbc.driverClass");
            String jdbcUrl = properties.getProperty("jdbc.url");
            String username = properties.getProperty("jdbc.user");
            String password = properties.getProperty("jdbc.password");
            Class.forName(driverClass);
            Connection connection = DriverManager.getConnection(jdbcUrl,username,password);
    
    //        String sql = "CREATE TABLE  HE_SHOU_YOU(ID INT , NAME VARCHAR(100))";
    //        String sql = "INSERT INTO HE_SHOU_YOU VALUES(122,'呵呵呵')";
            String sql = "SELECT * FROM HE_SHOU_YOU ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute();
            System.out.println("到这步了");
    
            ResultSet resultSet= preparedStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1));
            }
        }
    }
    View Code

    二、db.properties

    #h2 database settings
    jdbc.driverClass=org.h2.Driver
    jdbc.url=jdbc:h2:file:~/.h2/quickstart;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1
    jdbc.user=sa
    jdbc.password=123

    三、pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <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.imooc</groupId>
      <artifactId>springData</artifactId>
      <version>1.0-SNAPSHOT</version>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>6</source>
              <target>6</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
      <dependencies>
    
    
        <!-- Junit 依赖-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
        </dependency>
    
        <!--spring整合JDBC的依赖 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.3.5.RELEASE</version>
        </dependency>
    
        <!--H2数据库-->
        <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
        <dependency>
          <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>RELEASE</version>
         <scope>compile</scope>
        </dependency>
    
    
      </dependencies>
    </project>

    本文来自博客园,作者:编程狸,转载请注明原文链接:https://www.cnblogs.com/ping-pong/p/9827530.html

  • 相关阅读:
    [Apache commons系列]DBUtils 示例代码
    关于syslog
    Phoenix Tips (1)
    Phoenix 常用函数
    Phoenix 的数据类型
    最简单的Phoenix 访问 HBase例子
    SVNKIT 主要方法
    Linux 集群时钟同步
    Linux 集群时钟同步
    Linux 下定时提交SVN
  • 原文地址:https://www.cnblogs.com/ping-pong/p/9827530.html
Copyright © 2011-2022 走看看