为什么Wildfly找不到Postgresql驱动程序

时间:2019-07-08 07:20:59

标签: java postgresql wildfly

我正在使用Java开发多层软件。中间层是访问postgresql数据库的java ejb。但是,当我运行客户端Java应用程序时,出现以下错误消息:

INFO: EJBCLIENT000069: Using legacy jboss-ejb-client.properties security configuration
Exception in thread "main" javax.ejb.EJBException: WFLYEJB0442: Unexpected Error
....
Caused by: java.lang.NoClassDefFoundError: org/postgresql/Driver
...
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver

module.xml文件的内容为:

<?xml version="1.0" encoding="utf-8" ?> 
<module xmlns="urn:jboss:module:1.3" name="org.postgresql">
<resources>
<!--the name of your driver -->
<resource-root path="postgresql-42.2.6.jar"/>
</resources>
<dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
</dependencies>
</module>

以下行已添加到standalone.xml文件中:

<driver name="postgresql" module="org.postgresql">
<driver-class>org.postgresql.Driver</driver-class>
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource- 
class>
</driver>

驱动程序和module.xml位于文件夹\ modules \ org \ postgresql \ main中。通过将代码移到单层Java应用程序中,我已经成功测试了我的代码。我正在使用Eclipse jee最新版本,wildfly 16,postgresql11。驱动程序是postgresql-42.2.6.jar

我已经花了几天的时间来解决这个问题,但是没有成功。帮助最欢迎。

package loginPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import org.postgresql.Driver;
import java.sql.SQLException;


public class LoginDao {
    static String errorMessage;
    static String connectionResult;

    public static String getConnectionResult() {
        return connectionResult;
    }

    public static void setConnectionResult(String connectResult) {
        connectionResult = connectResult;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public static void setErrorMessage(String errorMsg) {
        errorMessage = errorMsg;
    }
    public static void LoginCheck(String userCode, String userPswd) {

        setConnectionResult(userConnect(userCode, userPswd));


    }

    public static  String userConnect(String userCode, String userPaswd) {

            try {
                //Connection conn = null;
                setConnectionResult("OK");
                setErrorMessage("OK");
                Driver driver = new org.postgresql.Driver();
                DriverManager.registerDriver(driver);

                Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/TestDb", userCode, userPaswd);

                System.out.println("Connected to PostgreSQL database!");
                return "SUCCESS";    
            }  

            catch (Exception e) {
                System.out.println("Failed to create JDBC db connection " + e.toString() + e.getMessage());
                setErrorMessage(userCode + ", " + userPaswd + ", " + e.toString() + ", " + e.getMessage() + ", " + e.getStackTrace());
                return "FAILURE" ;
            }
        }
    }

0 个答案:

没有答案