如何链接JDBC驱动程序到EJB项目缺少WEB-INF文件夹 [英] How to link JDBC driver to EJB project missing WEB-INF folder

查看:183
本文介绍了如何链接JDBC驱动程序到EJB项目缺少WEB-INF文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在霓虹Eclipse中有企业应用解决方案,其中包含EJB和Servlet项目并部署到Wildfly 10服务器。

Servlet调用访问DB的EJB。

如果我把JDBC Servlet项目的WEB-INF文件夹中的驱动程序一切顺利,但是如果我将代码移动到EJB并将驱动程序作为外部JAR链接(见截图),我会收到错误:

  java.lang.ClassNotFoundException:com.mysql.jdbc.Driver从[Moduledeployment.NeoflexBank.ear.Neoflex.jar:mainfrom Service Module Loader] 

以下是我用于访问数据库的代码:

  public String test(){
String output =;

try {
Class.forName(com.mysql.jdbc.Driver)。newInstance();
} catch(Exception e){
e.printStackTrace();
}

String url =jdbc:mysql:// localhost:3306 / neoflex;
String username =root;
String password =;
String query =select * from clients;
连接conn;
try {
conn =(Connection)DriverManager.getConnection(url,username,password);
语句stmt =(Statement)conn.createStatement();
ResultSet rs = stmt.executeQuery(query);

while(rs.next())
{
输出+ = rs.getInt(id);
输出+ = rs.getString(username);
output + = rs.getString(birth_date);
output + = rs.getString(name);
output + = rs.getString(surename);
}
} catch(SQLException e){
// TODO自动生成的catch块
return e.toString();
}

返回输出;
}

解决方案

在Java EE服务器中执行此操作的方法是定义一个DataSource,然后使用它来获取JDBC连接:


  1. 将mysql-connector-java-5.1.40-bin.jar移动到 standalone\deployments 目录。


  2. 启动WildFly服务器,并在 http:// localhost:9990 。您可能需要按照一些说明在此设置一些安全性。


  3. 点击配置标签,然后点击:



    a。子系统



    b。数据源



    c。非XA


  4. 点击蓝色添加按钮,然后按照提示进行操作。


现在,EJB中的代码看起来有点像:

  @Stateless 
public class SomeEJB {

@Resource(name =java:/ MySqlDS)
private DataSource ds;

public String test(){
String output =;

String query =select * from clients;

尝试{
try(Connection conn = ds.getConnection();
语句stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query )){

while(rs.next())
{
输出+ = rs.getInt(id);
输出+ = rs.getString(username);
output + = rs.getString(birth_date);
output + = rs.getString(name);
output + = rs.getString(surename);
}
}
} catch(SQLException e){
return e.toString();
}

返回输出;
}
}

这将您的应用程序与数据库配置分离。 DataSource配置也可以从命令行脚本完成。


I have Enterprise Appilcation Solution in Neon Eclipse, which envelope EJB and Servlet projects and deployed to Wildfly 10 server.
Servlet calls EJB for access DB.
If I put JDBC driver in WEB-INF folder of Servlet project, all is going well, but if I move the code to EJB and link the driver as external JAR (see screenshot), I get error:

 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from [Module "deployment.NeoflexBank.ear.Neoflex.jar:main" from Service Module Loader]

Here is the code I use for access the DB:

    public String test() {
        String output = "";

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String url="jdbc:mysql://localhost:3306/neoflex";
        String username="root";
        String password="";
        String query="select * from clients";
        Connection conn;
        try {
            conn = (Connection) DriverManager.getConnection(url, username, password);
            Statement stmt = (Statement) conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            while(rs.next())
            {
                output += rs.getInt("id");
                output += rs.getString("username");
                output += rs.getString("birth_date");
                output += rs.getString("name");
                output += rs.getString("surename");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            return e.toString();
        }

        return output;
    }

解决方案

The way to do this in a Java EE server is to define a DataSource and then use that for getting JDBC connections:

  1. move the mysql-connector-java-5.1.40-bin.jar into the standalone\deployments directory of your WildFly installation.

  2. start up your WildFly server and open the admin console at http://localhost:9990. You may have to follow some instructions to set up a bit of security here.

  3. click on the Configuration tab, and then click on:

    a. Subsystems

    b. Datasources

    c. Non-XA

  4. Click the blue Add button and follow the prompts.

Now, the code in your EJB will look a bit like:

@Stateless
public class SomeEJB {

   @Resource(name="java:/MySqlDS")
   private DataSource ds;

   public String test() {
        String output = "";

        String query="select * from clients";

        try {
            try (Connection conn = ds.getConnection();
                 Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(query)) {

                while(rs.next())
                {
                    output += rs.getInt("id");
                    output += rs.getString("username");
                    output += rs.getString("birth_date");
                    output += rs.getString("name");
                    output += rs.getString("surename");
                }
            } 
        } catch (SQLException e) {
            return e.toString();
        }

        return output;
    }
}

This decouples your application from the database configuration. The DataSource configuration can also be done from a command line script.

这篇关于如何链接JDBC驱动程序到EJB项目缺少WEB-INF文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆