How to access SQL datasource?

115 views
Skip to first unread message

Jannick Oursin

unread,
Apr 11, 2022, 10:51:11 AM4/11/22
to WildFly
Hi everyone,

I am a total Wildfly/JBOSS-Noob and I did not find any documentation about how to access a datasource, maybe execute a regular SQL statement from a deployed module.

Any ideas?

Thanks a lot for any response :-)

Alessio Gennari

unread,
Apr 11, 2022, 10:56:52 AM4/11/22
to Jannick Oursin, WildFly
What kind of SQL do you need to connect to?

--
You received this message because you are subscribed to the Google Groups "WildFly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wildfly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wildfly/7e4b37af-889c-4fee-8d66-937c185a9521n%40googlegroups.com.

Jannick Oursin

unread,
Apr 11, 2022, 11:03:04 AM4/11/22
to WildFly
Oracle SQL 19.

Suppose I would want to execute:

SELECT 1 AS someNum , 'bar' AS someVarchar, CURRENT_DATE AS someDateTime FROM DUAL

How would I do that? I know about Prepared Statements in JDBC.

Wolfgang

unread,
Apr 11, 2022, 3:52:40 PM4/11/22
to WildFly
Well, the answer to this question could fill books ;-).

Do you want to execute a query, return a ResultSet and loop over the reader yourself? In this case, it is probably the best approach NOT to manage the database connection yourself, but to use the injection feature of JavaEE.
If found this links which gives a hint on how to get the "java.sql.Connection" from the "EntityManager" which is injected by WildFly: https://stackoverflow.com/questions/28155496/get-jdbc-connection-from-entitymanager

Or do you want to write the result of the query to a Java class?
If the latter is true, you might take a look at JPA native queries - see this article for a beginning: http://www.mastertheboss.com/hibernate-jpa/query/using-native-queries-with-hibernate-and-jpa/

Both links mention an interface "EntityManager", wich is used to perform your database access.
This "EntityManager" is configured through a file "persistence.xml" in your deployment. And "persistence.xml" points to a datasource configured in the WildFly file "standalone.xml" - the first link contains also a datasource configuration snippet for this file.

Best regards

Wolfgang
Reply all
Reply to author
Forward
0 new messages