gpt4 book ai didi

java - Java应用程序中的数据源连接

转载 作者:行者123 更新时间:2023-12-02 04:47:17 25 4
gpt4 key购买 nike

如何为打包到 .jar 文件中的常规 Java 应用程序配置 JNDI 数据源连接?

我有这样的数据源 [FooDataSource.xml]:

<datasources>
<local-tx-datasource>
<jndi-name>FooDS</jndi-name>
<connection-url>jdbc:postgresql://127.0.0.1:5432/foo</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<user-name>admin</user-name>
<password>admin</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
</local-tx-datasource>
</datasources>

我已将此文件放入 Resouces 文件夹中。

我正在尝试像这样初始化连接:

    DataSource dataSource = null;
InitialContext initialContext = new InitialContext();
dataSource = (DataSource) initialContext.lookup("java:/FooDS");
Connection conn = dataSource.getConnection();
return conn;

但是它根本不起作用。我在调用 InitialContext initialContext = new InitialContext();

时遇到这样的错误
javax.naming.NoInitialContextException: Cannot instantiate class:   org.osjava.sj.SimpleContextFactory [Root exception is    java.lang.ClassNotFoundExcepti
on: org.osjava.sj.SimpleContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:175)
at ...DBUtil.getConnection(DBUtil.java:65)
at ...DBUtil.getFirstFleetCardId(DBUtil.java:133)
at ...sheduler.MainEntrance.execute(MainEntrance.java:76)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.ClassNotFoundException: org.osjava.sj.SimpleContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
... 8 more

我缺少什么?

编辑2现在我收到这样的错误:

java.lang.NullPointerException
at ...DBUtil.getConnection(DBUtil.java:67)
at ...DBUtil.getFirstFleetCardId(DBUtil.java:128)
at ...MainEntrance.execute(MainEntrance.java:76)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

第 67 行是:Connection conn = dataSource.getConnection();

最佳答案

您在类路径中缺少一个类:java.lang.ClassNotFoundException: org.osjava.sj.SimpleContextFactory。您可能应该将 simple-jndi-x.xx.jar 添加到您的类路径中。

关于java - Java应用程序中的数据源连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29560059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com