2 Replies Latest reply on Aug 24, 2006 8:01 PM by jbossnatasha

    Jboss with mySql

    jbossnatasha

      Hello,

      I need urgent help with my application. I am trying to connect to MySql with jboss and getting error.

      My mysql-ds.xml looks like this:

      <?xml version="1.0" encoding="UTF-8"?>

      <!-- $Id: mysql-ds.xml,v 1.3.2.3 2006/02/07 14:23:00 acoliver Exp $ -->
      <!-- Datasource config for MySQL using 3.0.9 available from:
      http://www.mysql.com/downloads/api-jdbc-stable.html
      -->


      <local-tx-datasource>
      <jndi-name>MySqlDS</jndi-name>
      <connection-url>jdbc:mysql://desktop/employee</connection-url>
      <driver-class>org.gjt.mm.mysql.Driver</driver-class>
      <user-name>root</user-name>
      password
      <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
      <!-- should only be used on drivers after 3.22.1 with "ping" support
      <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
      -->
      <!-- sql to call when connection is created
      <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
      <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
      <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->

      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->

      <type-mapping>mySQL</type-mapping>

      </local-tx-datasource>



      _-----------------------------------------

      I have all the jndi.jar, fsconfig.dar, providerURL.jar in the JAVA_HOME/jre/lib/ext folder.

      The jdbc driver is set correctly, I know, as I can connect to the database usin the DriverManager code.
      -----------------------------------------------

      my application code is:


      import java.io.*;
      import javax.naming.*;
      import java.net.*;
      import java.sql.*;
      import java.text.*;
      import java.util.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      import javax.sql.*;

      public class ShowBedrock extends HttpServlet
      {
      /**
      *
      */
      private static final long serialVersionUID = 4920882690562885989L;

      public String getServletInfo()
      {
      return "Servlet connects to MySQL database and displays result of a SELECT";
      }

      // Use http GET

      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException
      {

      Context ctx;

      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
      "com.sun.jndi.fscontext.RefFSContextFactory");
      env.put(Context.PROVIDER_URL, "jdbc:mysql://desktop/employee");

      // Output stream to STDOUT
      PrintWriter out = response.getWriter();
      try
      {
      ctx = new InitialContext(env);
      response.setContentType("text/html"); // Response mime type

      out.println("Bedrock");
      out.println("<H1>Bedrock</H1>");

      String name = "java:MySqlDS";
      DataSource ds = (DataSource) ctx.lookup(name);

      Connection conn;

      Statement statement;

      conn = ds.getConnection();

      String query = "SELECT name, dept, ";
      query += " jobtitle ";
      query += "FROM employee ";

      statement = conn.createStatement();

      // Perform the query
      ResultSet rs = statement.executeQuery(query);

      out.println("<TABLE border>");

      // Iterate through each row of rs
      while (rs.next())
      {
      String m_name = rs.getString("name");
      String m_dept = rs.getString("dept");
      String m_jobtitle = rs.getString("jobtitle");
      out.println("" +
      "" + m_name + "" +
      "" + m_dept + "" +
      "" + m_jobtitle + "" +
      "");
      }

      out.println("");

      rs.close();
      statement.close();
      conn.close();
      }

      catch (NamingException e) {
      System.err.println("Problem looking up " + ": " + e);
      }

      catch (SQLException ex) {
      while (ex != null) {
      System.out.println ("SQL Exception: " + ex.getMessage ());
      ex = ex.getNextException ();
      } // end while
      } // end catch SQLException

      catch(java.lang.Exception ex)
      {
      out.println("" +
      "" +
      "Bedrock: Error" +
      "\n" +
      "SQL error in doGet: " +
      ex.getMessage() + "");
      return;
      }
      out.close();
      }
      }


      ------------------------------------------

      I am getting a runtime error

      Am I accessing the jndi object correctly in my code

        • 1. Re: Jboss with mySql
          peterj

          The code is placing JDBC information into a hash table and passing it to the InitialContext. I believe that since the code is running within the app server that there is no need to pass any properties to InitialContext. As an aside, if the code did need to provide properties to InitialContext it would be JNDI properties, not JDBC properties.

          Also, if this suggestion doesn't fix the problem, post the error you are getting.

          • 2. Re: Jboss with mySql
            jbossnatasha

            Thank you. It worked with no environment properties required