解决ClassNotFoundException: org.h2.Driver
2018-10-23 03:05:24来源:博客园 阅读 ()
解决方案:
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)); } } }
二、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>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 聊聊微服务架构及分布式事务解决方案! 2020-06-10
- 解决IDEA Maven下载依赖包速度慢问题 2020-06-05
- Java生鲜电商平台-生鲜电商中配送订单解决方案(小程序/APP) 2020-05-31
- 一口气说出8种幂等性解决重复提交的方案,面试官懵了! 2020-05-30
- springboot 启动报错"No bean named 'org. 2020-05-29
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash