Java 中带有 SQLite 的 sql2o NoInitialContextException
sql2o with SQLite in Java NoInitialContextException
到目前为止我得到了以下代码片段:
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db");
try (Connection connection = sql2o.open()) {
for (Column column : connection.createQuery("SELECT * FROM sometable").executeAndFetchTable().columns())
System.out.println(column.toString());
}
我正在使用这些依赖项:
<dependency>
<groupId>org.sql2o</groupId>
<artifactId>sql2o</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.15.1</version>
</dependency>
但是,这会在执行时抛出一个NoInitialContextException。堆栈跟踪:
Exception in thread "main" java.lang.RuntimeException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:27)
at org.sql2o.Sql2o.<init>(Sql2o.java:36)
at me.mypackage.sqlitetest.SqliteTest.main(SqliteTest.java:11)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:24)
... 2 more
我该如何解决这个问题?
尝试替换此行
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db");
和
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db", null, null);
Sql2o
class 有一个只接受单个字符串的构造函数,但是该单个字符串是 JNDI 名称,而不是 JDBC 连接 URL。还有另一个构造函数采用三个字符串,用于连接 URL、用户名和密码。 SQLite 不使用用户名和密码,因此我们可以为它们指定 null
值。
到目前为止我得到了以下代码片段:
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db");
try (Connection connection = sql2o.open()) {
for (Column column : connection.createQuery("SELECT * FROM sometable").executeAndFetchTable().columns())
System.out.println(column.toString());
}
我正在使用这些依赖项:
<dependency>
<groupId>org.sql2o</groupId>
<artifactId>sql2o</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.15.1</version>
</dependency>
但是,这会在执行时抛出一个NoInitialContextException。堆栈跟踪:
Exception in thread "main" java.lang.RuntimeException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:27)
at org.sql2o.Sql2o.<init>(Sql2o.java:36)
at me.mypackage.sqlitetest.SqliteTest.main(SqliteTest.java:11)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:24)
... 2 more
我该如何解决这个问题?
尝试替换此行
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db");
和
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db", null, null);
Sql2o
class 有一个只接受单个字符串的构造函数,但是该单个字符串是 JNDI 名称,而不是 JDBC 连接 URL。还有另一个构造函数采用三个字符串,用于连接 URL、用户名和密码。 SQLite 不使用用户名和密码,因此我们可以为它们指定 null
值。