Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
找不到类[com.mysql.jdbc.Driver]_Mysql_Jakarta Ee_Jpa_Ejb_Client - Fatal编程技术网

找不到类[com.mysql.jdbc.Driver]

找不到类[com.mysql.jdbc.Driver],mysql,jakarta-ee,jpa,ejb,client,Mysql,Jakarta Ee,Jpa,Ejb,Client,我正在进行以下项目: 文件model.Clienti是一个“JPA实体”,main.main是一个带有main方法的pojo,它是model.Clienti文件的客户机。main.main具有以下代码: package main; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Persistence; import model.Clienti; public

我正在进行以下项目:

文件model.Clienti是一个“JPA实体”,main.main是一个带有main方法的pojo,它是model.Clienti文件的客户机。main.main具有以下代码:

package main;


import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import model.Clienti;

public class Main {
    public static void main(String[] args) {

        EntityManager entityManager = Persistence.createEntityManagerFactory("JPATestEJB").createEntityManager();
        List<Clienti> list = entityManager.createQuery("select c from Clienti c", Clienti.class).getResultList();

        for (Clienti clienti : list) {
            System.out.println(clienti.getNume());
        }
    }

}
还有员工会话Bean

package pachet;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import model.Clienti;

@Stateless
public class EmployeeSessionBean implements EmployeeSession {

    public String getEmplastname(Integer empno) {

        try {
            EntityManager em = Persistence.createEntityManagerFactory("JPATestEJB").createEntityManager();

            List<Clienti> list = em.createQuery("select c from Clienti c", Clienti.class).getResultList();

            for (Clienti clienti : list) {
                System.out.println(clienti.getNume());
            }
        } catch (EJBException e) {
            e.printStackTrace();
        }
        return "a mers";
    }

    public String test() {
        return "works";
    }


}
然而,当我尝试运行Main时(这个来自默认包),我得到了以下输出

可以看出,连接到EJB没有问题,因为thing.test();它返回“works”,我认为对于客户端所需的库没有问题,但在这之后会出现异常

EclipseLink中可以看到的延续没有找到com.mysql.jdbc.Driver

所以问题是:EJB项目中的main.main如何能够正确运行并显示来自db的信息,而EJB客户机中的main却不能

更准确地说,问题出在EJB EmployeeSessionBean中,如果它具有以下结构:

package pachet;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import model.Clienti;

@Stateless
public class EmployeeSessionBean implements EmployeeSession {

    public String getEmplastname(Integer empno) {

        try {
           String b = "nothing"; 
        } catch (EJBException e) {
            e.printStackTrace();
        }
        return "a mers";
    }

    public String test() {
        return "works";
    }
具有以下输出:(注意,我删除了所有JPA‘business’代码) }

可以看出,来自客户机的两种方法都有效。这意味着问题是因为ejb EmployeeSessionBean不能像main.main文件一样使用JPA,但问题是为什么?它们肯定在同一个项目中,可以看出我有EclipseLink和mysql jdbc所需的库

如果其中一个是错误的,那么main.main也无法连接,对吗? ' 谢谢你抽出时间

下面看一下我的persistence.xml文件,尽管它似乎工作正常。 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

model.Clienti

您需要将MySQL jar部署到Glassfish服务器上。可以将其与应用程序一起部署,也可以将其放在服务器上的applib目录中


通常在Glassfish中,会直接使用服务器数据源而不是JDBC,因此您可能也想尝试一下。

我已经做了一个全新的项目,这次使用的是服务器上的数据源和mysql jar,而不是我想要的应用程序上的数据源和mysql jar,它可以正常工作。谁知道是什么导致了这个问题……谢谢您的时间!
import javax.naming.InitialContext;

import pachet.EmployeeSession;

public class Main {


    public static void main(String[] args) {


        try {
            InitialContext ic = new InitialContext();
            EmployeeSession thing = (EmployeeSession) ic.lookup("pachet.EmployeeSession");
            System.out.println(thing.test());
            System.out.println("It seems it runs: " + thing.getEmplastname(1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
package pachet;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import model.Clienti;

@Stateless
public class EmployeeSessionBean implements EmployeeSession {

    public String getEmplastname(Integer empno) {

        try {
           String b = "nothing"; 
        } catch (EJBException e) {
            e.printStackTrace();
        }
        return "a mers";
    }

    public String test() {
        return "works";
    }
    <class>model.Clienti</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/comenzi"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value=""/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>

</persistence-unit>