Glassfish - 当试图连接数据源时,没有合适的mysql驱动程序 [英] Glassfish - No suitable driver for mysql when trying to connect with datasource

查看:140
本文介绍了Glassfish - 当试图连接数据源时,没有合适的mysql驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成本教程:

https://netbeans.org/kb/docs/javaee/ecommerce/connect-db.html



零件它建立了一个数据库,并尝试使用数据源和连接池建立连接。



我做了本教程中的所有内容,但是当我尝试运行我的代码时我收到以下错误:

javax.servlet.jsp.JspException:无法获取连接,DataSource无效:java.sql.SQLException:没有合适的驱动程序找到jdbc / affablebean



以下是引发它的代码:

 < sql:query var =resultdataSource =jdbc / affablebean> 
SELECT * FROM category,product
WHERE category.id = product.category_id
< / sql:query>

我试图连接到数据库而不使用连接池和数据源,它的工作方式就像一个魅力。 / p>

  try {
Class.forName(com.mysql.jdbc.Driver);
System.out.println(Driver registered);

catch(ClassNotFoundException ex){
Logger.getLogger(SqlService.class.getName()).log(Level.SEVERE,null,ex);
}

这意味着驱动程序位于glassfish目录的右边的lib文件夹中该问题必须在其他地方。



本教程有一个疑难解答部分,他们描述如果我得到这种错误找不到适合jdbc / affablebean的驱动程序意味着我在我的web.xml中没有资源引用。那么...我有一个,这里是:

 < resource-ref> 
< description>连接到AffableBean应用程序的数据库< / description>
< res-ref-name> jdbc / affablebean< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< res-sharing-scope>可分享< / res-sharing-scope>
< / resource-ref>

我一直试图解决这个问题,每天16小时超过两天,但仍然没有运气。



你们有什么想法吗?
我浏览了所有的google和stackoverflow,发现了类似的问题,但是他们提供的解决方案是确保mysql驱动程序位于服务器上的正确文件夹中。



为什么我连接到没有dataSource对象的数据库,但无法连接到一个?

解决方案

所以我找到了解决我完全一样的问题。
所以看起来问题是在web.xml中不包含对数据源的引用。
只需简单地添加

 < resource-ref> 
< description> AffableBean DataSource< / description>
< res-ref-name> jdbc / affablebean< / res-ref-name>
< res-type> javax.sql.ConnectionPoolDataSource< / res-type>
< res-auth>容器< / res-auth>
< / resource-ref>

添加到您的web.xml文件并保存并运行文件(右键单击文件并按运行文件)。



然后,您应该看到数据库选项卡在您选择的浏览器中弹出。


I am trying to complete this tutorial:

https://netbeans.org/kb/docs/javaee/ecommerce/connect-db.html

Part of it is setting up a database and trying to establish a connection using datasource and connection pooling.

I did everything that is in the tutorial but when i try to run my code i get the following error:

javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/affablebean"

Here is the code that throws it:

<sql:query var = "result" dataSource = "jdbc/affablebean">
        SELECT * FROM category, product
        WHERE category.id = product.category_id
</sql:query>

I tried to connect to the database without using connection pooling and datasource and it worked like a charm.

try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver registered");
    } 
    catch (ClassNotFoundException ex) {
        Logger.getLogger(SqlService.class.getName()).log(Level.SEVERE, null, ex);
    }

That means that the driver is in the right lib folder in the glassfish directory so the problem must be somewhere else.

The tutorial has a troubleshooting section where they describe that if i get this kind of error "No suitable driver found for jdbc/affablebean" that means that i do not have resource reference in my web.xml. Well... I DO HAVE ONE and here it is:

<resource-ref>
    <description>Connects to database for AffableBean application</description>
    <res-ref-name>jdbc/affablebean</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

I have been trying to solve this problem 16 hours a day for more than two day but still NO luck.

Do you guys have some idea what is wrong? I browsed all the google and stackoverflow and found similar problems but the solution they give is "Make sure the mysql driver is in the right folder on the server".

Why do i connect to the database without dataSource object but can not connect with one?

解决方案

So i found the solution to the exact same problem I was having. So it appears that the issue was within web.xml not containing a reference to the datasource. Simply add,

<resource-ref>
    <description>AffableBean DataSource</description>
    <res-ref-name>jdbc/affablebean</res-ref-name>
    <res-type>javax.sql.ConnectionPoolDataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

to your web.xml file and save and run file(right click inside the file and press "Run File").

Afterwards you should see the database tab pop up in your browser of choice.

这篇关于Glassfish - 当试图连接数据源时,没有合适的mysql驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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