4 Replies Latest reply on Dec 9, 2003 8:34 AM by jonlee

    Connecting to MySQL

    gregsmith

      I use the following command to connect to MySQL:

      Connection conn =DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=mysqlpass");

      JBoss 3.2.2 gives the following error:

      15:36:25,236 ERROR [STDERR] java.sql.SQLException: No suitable driver
      15:36:25,236 ERROR [STDERR] at java.sql.DriverManager.getConnection(DriverMa
      nager.java:532)
      15:36:25,236 ERROR [STDERR] at java.sql.DriverManager.getConnection(DriverMa
      nager.java:193)

      I have the following mysql-ds.xml


      <local-tx-datasource>
      <jndi-name>DefalutDS</jndi-name>
      <connection-url>jdbc:mysql://localhost/test</connection-url>
      <driver-class>org.gjt.mm.mysql.Driver</driver-class>
      <user-name>root</user-name>
      mysqlpass
      </local-tx-datasource>


      What am i missing?

      greg

        • 1. Re: Connecting to MySQL
          jonlee

          Did you put the MySQL JDBC driver JAR in JBOSS_HOME/server/default/lib or whatever is appropriate for your run-time instance - normally default is the run-time instance unless you override the bootstrap with an option for run.sh.

          • 2. Re: Connecting to MySQL
            gregsmith

            Yes. I put mysql-java-connector-java-3.0.9-stable.bin.jar in JBOSS_HOME/server/default/lib

            greg

            • 3. Re: Connecting to MySQL
              jonlee

              My bad. I've misinterpreted what you are trying to achieve.

              I assume that code fragment is what you are using to try and connect? Should that be the case, it won't work because the DataSource is the containing object for connections.

              In order to use the connection pool declared, you need to lookup the datasource and then use the connection issued from the datasource. You can only access the DataSource by performing a JNDI lookup from within the JVM that JBoss is running.

              DataSource ds = (new InitialContext()).lookup("java:/DefaultDS");
              Connection connection = ds.getConnection(); // get connection from datasource (connection pool)
              ...
              connection.close(); // release connection back to pool

              Hope that helps.

              • 4. Re: Connecting to MySQL
                jonlee

                A slight mistake in the first line. The lookup returns an object which you should cast as a DataSource.