HTTP ERROR: 503 javax.servlet.ServletException: Query: can't
sabar May 15, 2003 8:41 AMHi
i am a newer with jboss and mysql and eclipse , i did the following steps to make my servlet connect to mysql :
1.create mysql data base [db.name]
2.copy mm.mysql-2.0.14-bin.jar to [jboss.dir]/server/default/lib
3. server/default/conf/standardjbosscmp-jdbc.xml, replace the Hyper SQL with these 2:
java:/MySqlDS
<datasource-mapping>mySQL</datasource-mapping>
4. server/default/conf/standardjaws.xml, replace the Hyper SQL with these 2:
java:/MySqlDS
<type-mapping>mySQL</type-mapping>
5. copy docs/examples/jca/mysql-service.xml into /server/default/deploy
6. update mysql-service.xml with your MySql login/password, and use the following:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
 <!-- ==================================================================== -->
 <!-- New ConnectionManager setup for mysql using 2.0.11 driver -->
 <!-- Build jmx-api (build/build.sh all) and view for config documentation -->
 <!-- ==================================================================== -->
 <!-- Include a login module configuration named MySqlDbRealm.
 Update your login-conf.xml, here is an example for a
 ConfiguredIdentityLoginModule:
 <application-policy name = "MySqlDbRealm">
 <login-module code = "org.jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required">
 <module-option name = "principal">yourprincipal</module-option>
 <module-option name = "userName">yourusername</module-option>
 <module-option name = "password">yourpassword</module-option>
 <module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS</module-option>
 </login-module>
 </application-policy>
 NOTE: the application-policy name attribute must match SecurityDomainJndiName, and the
 module-option name = "managedConnectionFactoryName"
 must match the object name of the ConnectionManager you are configuring here.
 -->
 <!--uncomment out this line if you are using the MySqlDbRealm above
 MySqlDbRealm
 -->
 <depends optional-attribute-name="ManagedConnectionFactoryName">
 <!--embedded mbean-->
 MySqlDS
 <config-property name="ConnectionURL" type="java.lang.String">jdbc:mysql://localhost:3306/partdb</config-property>
 <config-property name="DriverClass" type="java.lang.String">org.gjt.mm.mysql.Driver</config-property>
 <!--set these only if you want only default logins, not through JAAS -->
 <config-property name="userName" type="java.lang.String">test</config-property>
 <config-property name="password" type="java.lang.String">test</config-property>
<!--Below here are advanced properties -->
 <!--hack-->
 <depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper
 <depends optional-attribute-name="ManagedConnectionPool">
 <!--embedded mbean-->
 0
 50
 5000
 15
 <!--criteria indicates if Subject (from security domain) or app supplied
 parameters (such as from getConnection(user, pw)) are used to distinguish
 connections in the pool. Choices are
 ByContainerAndApplication (use both),
 ByContainer (use Subject),
 ByApplication (use app supplied params only),
 ByNothing (all connections are equivalent, usually if adapter supports
 reauthentication)-->
 ByContainer
 <depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager
 <depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager
 java:/TransactionManager
 <!--make the rar deploy! hack till better deployment-->
 jboss.jca:service=RARDeployer
7. restart jboss
8. write my servlet within eclipse
package jdbctest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
 public class Query extends HttpServlet {
Connection db; // This is the shared JDBC database connection
 public void init() throws ServletException {
 // Read initialization parameters from the web.xml file
 ServletConfig config = getServletConfig();
 String driverClassName = config.getInitParameter("DriverClass");
 String url = config.getInitParameter("ConnectionURL");
 String username = config.getInitParameter("username");
 String password = config.getInitParameter("password");
 // Use those init params to establish a connection to the database
 // If anything goes wrong, log it, wrap the exception and re-throw it
 try {
 Class.forName(driverClassName);
 db = DriverManager.getConnection(url, username, password);
 }
 catch (Exception e) {
 log("Can't create DB connection", e);
 throw new ServletException("Query: can't initialize: " +
 e.getMessage(), e);
 }
 }
/** Close the database connection when the servlet is unloaded */
 public void destroy() {
 try { db.close(); } // Try to close the connection
 catch (SQLException e) {} // Ignore errors; at least we tried!
 }
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws IOException, ServletException
 {
response.setContentType("text/html"); // We're outputting HTML
 PrintWriter out = response.getWriter(); // Where to output it to
 // Output document header and a form for entering SQL queries
 // When the form is submitted, this servlet is reloaded
 out.println("DB Query\n" +
 "<body bgcolor=white><h1>DB Query</h1>\n" +
 "Query: " +
 "<input type=submit>");
 // See if a query was specified in this request.
 String query = request.getParameter("q");
 if (query != null) {
 // display the query text as a page heading
 out.println("<h1>" + query + "</h1>");
 // Now try to execute the query and display the results in a table
 Statement statement = null; // An object to execute the query
 try {
 // Create a statement to use
 statement = db.createStatement();
 // Use it to execute the specified query, and get result set
 ResultSet results = statement.executeQuery(query);
 // Ask for extra information about the results
 ResultSetMetaData metadata = results.getMetaData();
 // How many columns are there in the results?
 int numcols = metadata.getColumnCount();
 // Begin a table, and output a header row of column names
 out.println("<table border=2>");
 for(int i = 0; i < numcols; i++)
 out.print("" + metadata.getColumnLabel(i+1) + "");
 out.println("");
 // Now loop through the "rows" of the result set
 while(results.next()) {
 // For each row, display the the values for each column
 out.print("");
 for(int i = 0; i < numcols; i++)
 out.print("" + results.getObject(i+1) + "");
 out.println("");
 }
 out.println(""); // end the table
 }
 catch (SQLException e) {
 // If anything goes wrong (usually a SQL error) display the
 // error to the user so they can correct it.
 out.println("SQL Error: " + e.getMessage());
 }
 finally { // Whatever happens, always close the Statement object
 try { statement.close(); }
 catch(Exception e) {}
 }
 }
 // Finally, end the HTML output
 out.println("");
 }
}
9- write web.xml
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>
 <servlet-name>Query</servlet-name>
 <display-name>This is the display name of my J2EE component</display-name>
 This is the description of my J2EE component
 <servlet-class>jdbctest.Query</servlet-class>
 <init-param>
 <param-name>ConnectionURL</param-name>
 <param-value>jdbc:mysql://localhost:3306/partdb</param-value>
 </init-param>
 <init-param>
 <param-name>DriverClass</param-name>
 <param-value>org.gjt.mm.mysql.Driver</param-value>
 </init-param>
 <init-param>
 <param-name>userName</param-name>
 <param-value>test</param-value>
 </init-param>
 <init-param>
 <param-name>password</param-name>
 <param-value>test</param-value>
 </init-param>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <error-page>
 <error-code>404</error-code>
 error.jsp
 </error-page>
</web-app>
10- deploy my servlet and run it by
http://localhost:8080/webjdbc/servlet/jdbctest.Query
and got the following error
HTTP ERROR: 503 javax.servlet.ServletException: Query: can't initialize: null
pls tell me what is the wrong?
