1. To use MySQL in jboss4, first put the JDBC driver of MySQL into classpath.
Similarly, copy it to the/Server/default/lib directory,
(MySQL-connector-Java-is required- 3.0.16 -The ga.zip package extracts the jar files)
II, Then copy the/docs/examples/JCA/mysql-ds.xml to the/Server/default/deploy directory. Modify the mysql-ds.xml file, set it to com. MySQL. JDBC. Driver, and then set it to JDBC: mysql: //, where the Database Host Name is the database name.
Example: <CES>
<Local-TX-datasource>
<JNDI-Name> mysqlds </JNDI-Name>
<Connection-URL> JDBC: mysql: // localhost: 3306/wwx </connection-URL> // wwx
<Driver-class> com. MySQL. JDBC. Driver </driver-class>
<User-Name> root </user-Name>
<Password> root </password>
</Local-TX-datasource>
</Datasources>
3. ,Then you need to set the and elements of the standardjaws. XML (or Jaws. xml Note:/Server/default/conf directory) file:
<Jaws>
<Datasource> JAVA:/mysqlds </datasource>
<Type-mapping> mysql </type-mapping>
.......
</Jaws>
Thu, You also need to set the and element of the standardjbosscmp-jdbc.xml (or jbosscmp-jdbc.xml file note:/Server/default/conf directory) to the following:
<Jbosscmp-jdbc>
<Defaults>
<Datasource> JAVA:/mysqlds </datasource>
<Datasource-mapping> mysql </datasource-mapping>
</Defaults> </jbosscmp-jdbc>
V., And finally modify the login-config.xml (/Server/default/conf directory) file to use MySQL:
<Application-Policy Name = "mysqldbrealm">
<Authentication>
<Login-module code = "org. JBoss. Resource. Security. configuredidentityloginmodule" flag = "required">
<Module-option name = "principal"> wwx </Module-option>
<Module-option name = "username"> root </Module-option>
<Module-option name = "password"> root </Module-option>
<Module-option name = "managedconnectionfactoryname"> JBoss. JCA: service = localtxcm, name = mysqlds
</Module-option>
</Login-module>
</Authentication>
</Application-Policy>
Sat., Modified the mysql-ds.xml, standardjaws. XML, standardjbosscmp-jdbc.xml, and login-config.xml files can use MySQL on JBoss.
VII., Debugging code
Debugging is successfully called in servlet Init ().
Context CTX = new initialcontext (); // get the initialization Context
Object OBJ = CTX. Lookup ("Java:/mysqlds"); // query the connection pool
Datasource DS = (datasource) OBJ; // convert to datasource
Connection connect = Ds. getconnection (); // obtain a connection from the connection pool.
// System. Out. println ("Success connect MySQL connection
// Pool! ");
Statement stmt = connect. createstatement ();
Resultset rs = stmt.exe cutequery ("select * From userinfor ");
While (Rs. Next ()){
Temp = Rs. getstring ("name ");
}
} Catch (exception e ){
// System. Out. Print ("Get data error! ");
E. printstacktrace ();
}