管çã³ã³ã½ã¼ã«ã使ç¨ãã¦ãã¼ã¿ã½ã¼ã¹ã®å®ç¾©ãè¡ãæé ã
ç°å¢
ãã£ããã¨
JDBCãã©ã¤ãã®ãããã¤
ojdbc6.jarã%WILDFLY_ROOT%standalone\deploymentsã«é ç½®ããã
JBossãèµ·åãããä¸è¨ã®ããã«ãojdbc6.jarããããã¤ãããã°OK
JBAS015876: Starting deployment of "ojdbc6.jar" (runtime-name: "ojdbc6.jar")
JBAS017100: Listening on 127.0.0.1:9999
JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
JBAS010403: Deploying JDBC-compliant driver class oracle.jdbc.OracleDriver (version 11.2)
JBAS010417: Started Driver service with driver-name = ojdbc6.jar
JBAS018559: Deployed "ojdbc6.jar" (runtime-name : "ojdbc6.jar")
管çã¦ã¼ã¶ã®ä½æ
管çã³ã³ã½ã¼ã«ã§ä½æ¥ããããã®ç®¡çè ã¦ã¼ã¶ãä½æããã
ä¸è¨ã®ããã«ã%WILDFLY_ROOT%\bin\add-user.batãå®è¡ãã¦ä½æãããä½ç®æããå ¥åãæ±ããããã®ã§ããã®ç¹ã¯å¾è¿°ã
>add-user.bat What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) (a): Enter the details of the new user to add. Realm (ManagementRealm) : Username : kagamihoge Password : Re-enter Password : What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: About to add user 'kagamihoge' for realm 'ManagementRealm' Is this correct yes/no? yes Added user 'kagamihoge' to file 'C:\Java\jboss-as\wildfly-8.0.0.Alpha4_ora\stand alone\configuration\mgmt-users.properties' Added user 'kagamihoge' to file 'C:\Java\jboss-as\wildfly-8.0.0.Alpha4_ora\domai n\configuration\mgmt-users.properties' Added user 'kagamihoge' with groups to file 'C:\Java\jboss-as\wildfly-8.0.0.Alp ha4_ora\standalone\configuration\mgmt-groups.properties' Added user 'kagamihoge' with groups to file 'C:\Java\jboss-as\wildfly-8.0.0.Alp ha4_ora\domain\configuration\mgmt-groups.properties' Is this new user going to be used for one AS process to connect to another AS pr ocess? e.g. for a slave host controller connecting to the master or for a Remoting conn ection for server to server EJB calls. yes/no? yes To represent the user add the following to the server-identities definition <sec ret value="YWFhYXNzc3MxXw==" /> ç¶è¡ããã«ã¯ä½ããã¼ãæ¼ãã¦ãã ãã . . .
管çè ã¦ã¼ã¶ï¼aï¼ãã¢ããªã±ã¼ã·ã§ã³ã¦ã¼ã¶ï¼bï¼ããé¸æãããããã©ã«ãï¼ï¼ä½ãå ¥åããªãï¼ã¯ï¼aï¼ãªã®ã§ã³ã¬ãé¸ã¶ã
(a):
ã¦ã¼ã¶IDãä»»æã®ãã®ã
Username : kagamihoge
ãã¹ã¯ã¼ãã¨å ¥å確èªãè±æ°åè¨å·å¿ é ã
Password : Re-enter Password :
æå±ã°ã«ã¼ããã¨ããããä½ãå ¥åããã¨ã³ã¿ã¼ã§OK
What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]:
å®è¡ç¢ºèªããããããããã°yesã¨å ¥åã
Is this correct yes/no? yes
æ°ããã¦ã¼ã¶ãASã®ããã»ã¹ããå¥ã®ããã»ã¹ã«æ¥ç¶ã§ãã¦ããããï¼ãã¨ããªãã¨ãããã¨ãã°EJBã®ãªã¢ã¼ãã³ã¼ã«ãããã¬ãã¨ããªãã¨ã*1ã ããã¨ããããé¢ä¿ãªãã®ã§yesã«ãã¦ãã¾ãã
Is this new user going to be used for one AS process to connect to another AS pr ocess? e.g. for a slave host controller connecting to the master or for a Remoting conn ection for server to server EJB calls. yes/no? yes
Administration ConsoleããJDBC Datasourceã追å
http://localhost:8080/console ã«ãã©ã¦ã¶ããã¢ã¯ã»ã¹ãããã£ãä½ã£ãã¦ã¼ã¶IDã§ãã°ã¤ã³ãããã¨ãããããããªæãã®ç»é¢ã表示ãããã
å·¦ä¸ã®Profile -> å·¦å´ããªã¼ã®Subsystems -> Connector -> Datasources ãé¸æãããããã©ã«ãã§å
¥ã£ã¦ããH2ã®ãã¼ã¿ã½ã¼ã¹ï¼ExampleDSï¼ãè¦ã¦åããã
Addãæ¼ããæ°è¦ãã¼ã¿ã½ã¼ã¹ã®ååã¨ãJNDIåãå
¥åãããã¨ãããããããã§ã¯Oracle11gXEDSã¨java:jboss/datasources/Oracle11gXEDSã«ãã¦ããã
JDBCãã©ã¤ããé¸æããããã£ããããã¤ããojdbc6.jarãã¯ãªãã¯ãã¦é¸æç¶æ
ã«ãã¦Nextæ¼ãã
JDBC URLãIDããã¹ã¯ã¼ããªã©æ¥ç¶è¨å®ãå
¥åããã
å
¥åãçµããã¨ããããªæãã«ãªã¹ãã«è¿½å ãããããã ããEnableã®æ¬ã使ç¨ä¸å¯ã«ãªã£ã¦ããã¨ããã使ããã¨ã¯åºæ¥ãªããã®ã§ã追å ãããã¼ã¿ã½ã¼ã¹ãé¸æãã¦è¡¨ã®å³ä¸ã«ããEnableãæ¼ãã
確èªãã¤ã¢ãã°ãåºãã®ã§å®è¡ããã
æå¾ã«ãæ¥ç¶ç¢ºèªãããããã¼ã¸ä¸é¨ã®Connectionã¿ããéãã¦Test Connectionãæ¼ããåé¡ãç¡ããã°ãä¸å³ã®ããã«JDBCã®Connectionãä½æã§ãã¾ããããã¨ã¡ãã»ã¼ã¸ãåºãããªãããªããã¨ã©ã¼ããã£ãå ´åã¯Wildflyã®ãã°ã«åºããex. ãã¹ã¯ã¼ããéã£ã¦ã¾ããã¨ãã
æ¥ç¶ç¢ºèª
ãããã¼ãªãµã¼ãã¬ããã§ãä½ã£ã¦ã¿ã¦Javaããæ¥ç¶ç¢ºèªãããã
ã¨ããããæ°ããæãã®servletã®dependencyã追å ããã
<dependency> <groupId>org.jboss.spec.javax.servlet</groupId> <artifactId>jboss-servlet-api_3.1_spec</artifactId> <version>1.0.0.Beta1</version> </dependency>
é©å½ãªæãã®Javaãæ¸ããããã¼ã¯Java EEã ã¨JPAçµç±ã«ãªãã¨æããã§ãDataSourceããConnectionåå¾ã¨ããããªãã¨ã¯æããã©ãã¾ãå®é¨ã¨ãããã¨ã§ã
package kagamihoge.jboss8ora; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; @WebServlet("/hoge") public class HogeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:jboss/datasources/Oracle11gXEDS"); try (Connection connection = ds.getConnection(); PreparedStatement sql = connection.prepareStatement("select * from v$version"); ResultSet r = sql.executeQuery();) { while (r.next()) { String banner = r.getString(1); System.out.println(banner); } } catch (SQLException e) { e.printStackTrace(); } } catch (NamingException e) { e.printStackTrace(); } } }
ãã°ã«SQLã®å®è¡çµæã表示ããããã°OK
*1:è¯ãç解ãã¦ããªã