简体   繁体   English

数据源中的Wildfly 12错误

[英]Wildfly 12 error in datasource

I'm trying to create a simple datasource in Wildfly 12 in order to connect a mysql database to a Java WEB Application. 我试图在Wildfly 12中创建一个简单的数据源,以将mysql数据库连接到Java WEB应用程序。

I have tried 2 options: 我尝试了2种选择:

  1. Modify the standalone-full.xml to add the datasource like this: 修改standalone-full.xml以添加数据源,如下所示:

     <datasource jndi-name="java:jboss/datasources/MyDS" pool-name="MyDS" enabled="true" use-java-context="true"> <connection-url>jdbc:mysql://localhost:3306/MyDatabaseName</connection-url> <driver>mysql</driver> <pool> <min-pool-size>2</min-pool-size> <max-pool-size>5</max-pool-size> </pool> <security> <user-name>myuser</user-name> <password>mypassword</password> </security> </datasource> <drivers> <driver name="mysql" module="com.mysql"> <driver-class>com.mysql.jdbc.Driver</driver-class> </driver> </drivers> 
    1. Add the datasource using the admin console 使用管理控制台添加数据源

I'm creating a Connection using the datasource without problems: 我正在使用数据源创建连接而没有问题:

Context initCtx = new InitialContext(), envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource) envCtx.lookup("jdbc/MyDS");
        this.connection = ds.getConnection();

The Java Connection works without errors but when I create a servlet to query my database, I got an exception like this: Java Connection可以正常工作,但是当我创建一个servlet查询数据库时,出现了如下异常:

"Table "user" not found; SQL statement: Select * from user where username = 'myusername'" “未找到表“用户”; SQL语句:从用户名*'myusername'的用户中选择*”

I have run some tests and I could notice that Java is connected to the database but not to the specific schema, I run a query like "SELECT DATABASE() FROM DUAL" and the result is "TEST". 我进行了一些测试,我发现Java已连接到数据库,但未连接到特定的架构,我运行了一个查询,例如“ SELECT DATABASE()FROM DUAL”,结果为“ TEST”。 So I guess that the database name param in the URL connection is not working properly. 因此,我猜想URL连接中的数据库名称参数无法正常工作。

How can I solve this problem? 我怎么解决这个问题? I haven't found any additional param to specify the database name in the datasource. 我没有找到任何其他参数来指定数据源中的数据库名称。

Thanks for your time. 谢谢你的时间。

Well, after having tried some modifications, I realized that I had to put the next code in the web.xml file: 好吧,在尝试了一些修改之后,我意识到我必须将下一个代码放入web.xml文件中:

<resource-ref>
    <res-ref-name>jdbc/MyDS/<res-ref-name>
    <jndi-name>jdbc:mysql://localhost:3306/MyDatabaseName</jndi-name>
</resource-ref>

This solved the problem. 这样就解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM