• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

my servlet can't initialize: null  RSS feed

Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
4. server/default/conf/standardjaws.xml, replace the Hyper SQL with these 2:
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 -->
<!-- ==================================================================== -->
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=MySqlDS">

<!-- Include a login module configuration named MySqlDbRealm.
Update your login-conf.xml, here is an example for a
<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>
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
<attribute name="SecurityDomainJndiName">MySqlDbRealm</attribute>
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=MySqlDS">
<attribute name="JndiName">MySqlDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<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 -->
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=MySqlDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<!--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
<attribute name="Criteria">ByContainer</attribute>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<!--make the rar deploy! hack till better deployment-->

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 {
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("<head><title>DB Query</title></head>\n" +
"<body bgcolor=white><h1>DB Query</h1>\n" +
"<form><b>Query: </b><input name='q'>" +
"<input type=submit></form>");
// 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><tr>");
for(int i = 0; i < numcols; i++)
out.print("<th>" + metadata.getColumnLabel(i+1) + "</th>");
// Now loop through the "rows" of the result set
while(results.next()) {
// For each row, display the the values for each column
for(int i = 0; i < numcols; i++)
out.print("<td>" + results.getObject(i+1) + "</td>");
out.println("</table>"); // 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
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. -->
<display-name>This is the display name of my J2EE component</display-name>
<description>This is the description of my J2EE component</description>





10- deploy my servlet and run it by
and got the following error
HTTP ERROR: 503 javax.servlet.ServletException: Query: can't initialize: null
pls tell me what is the wrong?
A feeble attempt to tell you about our stuff that makes us money
Create Edit Print & Convert PDF Using Free API with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!