在我提出这个问题之前,我已经阅读了有关此问题的所有帖子,我可以在Stack Overflow上找到。
我在myven pom.xml中的classpath中定义了SQLite jdbc驱动,如下所示
<!-- SQLite uploads -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.7</version>
</dependency>
错误堆栈跟踪:
Caused by: java.sql.SQLException: No suitable driver found for jdbc:sqlite:C:\Users\zk5426x\AppData\Local\Temp\cms8026785111405543526.db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
我能够验证文件C:\Users\zk5426x\AppData\Local\Temp\cms8026785111405543526.db
是否可写。
我的代码如下:
// Writing byte array to the db file.
public Set<CmsXServiceContent> readContentFromSqliteStream(byte[] bytes) throws Exception {
Path path = null;
try {
path = Files.createTempFile("cms", ".db");
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Temporary file created = " + path.toString());
}
try (ByteArrayInputStream is = new ByteArrayInputStream(bytes);) {
Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING);
Class.forName("org.sqlite.JDBC");
try(
Connection conn =
DriverManager.getConnection(
"jdbc:sqlite:" + path.toFile().getCanonicalPath());) {
// Prepared Statements and result set query.
}
}
} catch (IOException e) {
LOGGER.error("Unable to create a temporary file: cms.db");
throw e;
} finally {
if (path != null) {
path.toFile().deleteOnExit();
}
}
}
以下声明会生成错误。在此声明中C:\Users\zk5426x\AppData\Local\Temp\cms8026785111405543526.db
是可写的。
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + path.getFileName());
代码编译。我想弄清楚的是如何解决这个问题?我已经使用Maven将SQLite-jdbc jar添加到了我的类路径中。
(更新) Youcef Laidani建议我从.m2目录中删除二进制文件。执行此操作并执行mvn test
命令后,我看到以下堆栈跟踪。
[INFO] ------------------------------------------------------------------------
[INFO] Downloading: https://retail-devops-maven.mycompany.com/content/groups/public/org/xerial/sqlite-jdbc/3.8.7/sqlite-jdbc-3.8.7.pom
[INFO] Downloaded: https://retail-devops-maven.mycompany.com/content/groups/public/org/xerial/sqlite-jdbc/3.8.7/sqlite-jdbc-3.8.7.pom (5 KB at 3.2 KB/sec)
[INFO] Downloading: https://retail-devops-maven.mycompany.com/content/groups/public/org/xerial/sqlite-jdbc/3.8.7/sqlite-jdbc-3.8.7.jar
[INFO] Downloaded: https://retail-devops-maven.mycompany.com/content/groups/public/org/xerial/sqlite-jdbc/3.8.7/sqlite-jdbc-3.8.7.jar (3867 KB at 1539.8 KB/sec)
. . . . . . . . . . MAVEN LOGS . . . . . . .
Tests run: 38, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 18.936 sec
<<< FAILURE!
testReadContentFromSqliteStream(com.mycompany.cms.tools.service.CmsContentServiceImplTest) Time elapsed: 0.012 sec <<< FAILURE!
com.mycompany.cms.tools.dao.ContentRetrievalException: Unable to retrieve content for Unable to parse file : cms6986233222629132337.db
at com.mycompany.cms.tools.service.CmsContentServiceImpl.readContentFromSqliteStream(CmsContentServiceImpl.java:152)
at com.mycompany.cms.tools.service.CmsContentServiceImplTest.testReadContentFromSqliteStream(CmsContentServiceImplTest.java:75)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:sqlite:C:\Users\zk5426x\AppData\Local\Temp\cms6986233222629132337.db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.mycompany.cms.tools.dao.CmsLocalContentDao.readContentsFromResource(CmsLocalContentDao.java:50)
... 39 more