如何链接JDBC驱动程序到EJB项目缺少WEB-INF文件夹 [英] How to link JDBC driver to EJB project missing WEB-INF folder
问题描述
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连接:
-
将mysql-connector-java-5.1.40-bin.jar移动到
standalone\deployments $您的WildFly安装的c $ c>目录。
-
启动WildFly服务器,并在 http:// localhost:9990 。您可能需要按照一些说明在此设置一些安全性。
-
点击
配置
标签,然后点击:
a。子系统
b。数据源
c。非XA
-
点击蓝色
添加
按钮,然后按照提示进行操作。
现在,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:
move the mysql-connector-java-5.1.40-bin.jar into the
standalone\deployments
directory of your WildFly installation.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.
click on the
Configuration
tab, and then click on:a. Subsystems
b. Datasources
c. Non-XA
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屋!