Armando Singer wrote:
>
> I recently downloaded, unpacked, etc, the mm.mysql JDBC driver and I
> can't get it to work. I get the follow error from my exception handling:
>
> java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
>
> My java source code is below. I'm running Red Hat Linux 6 with mysql Ver
> 9.32 Distrib 3.22.22, for pc-linux-gnu (i686). I'm using the Blackdown
> JDK 1.2 pre. 2 release for glibc2.1 for Linux. I have the mm.mysql
> driver directory at /usr/local/mm.mysql.jdbc-1.2a, and I have the
> mysql.jar file in my CLASSPATH like this:
>
> # .bash_profile
>
> # Get the aliases and functions
> if [ -f ~/.bashrc ]; then
> . ~/.bashrc
> fi
>
> # User specific environment and startup programs
> BASH_ENV=$HOME/.bashrc
> DISPLAY=:0
> PATH=$HOME/bin:/usr/local/jdk1.2-pre2/bin:/usr/local/JSDK2.0/bin:$PATH
>
> CLASSPATH=.:/usr/local/JSDK2.0/lib/jsdk.jar:/usr/local/xml-tr2/xml.jar:/usr/local/mm.mysql.jdbc-1.2a/mysql.jar
> JAVA_HOME=/usr/local/jdk1.2-pre2
>
> export USERNAME BASH_ENV PATH DISPLAY CLASSPATH JAVA_HOME
> # end of .bash_profile
>
> And I did a quick check to make sure that the jar
> file had the proper files with:
>
> [169M /usr/local/mm.mysql.jdbc-1.2a]$ jar -tvf mysql.jar
> 2604 Sun Dec 31 06:46:16 EST 1995 META-INF/MANIFEST.MF
> 6534 Sun Dec 31 06:44:32 EST 1995 org/gjt/mm/mysql/Buffer.class
> 9720 Sun Dec 31 06:44:32 EST 1995 org/gjt/mm/mysql/Connection.class
> 36659 Sun Dec 31 06:44:38 EST 1995
> org/gjt/mm/mysql/DatabaseMetaData.class
> 2952 Sun Dec 31 06:44:38 EST 1995 org/gjt/mm/mysql/Debug.class
> 5893 Sun Dec 31 06:44:38 EST 1995 org/gjt/mm/mysql/Driver.class
> 5229 Sun Dec 31 06:44:38 EST 1995
> org/gjt/mm/mysql/EscapeProcessor.class
> 2875 Sun Dec 31 06:44:38 EST 1995 org/gjt/mm/mysql/Field.class
> 3556 Sun Dec 31 06:44:38 EST 1995 org/gjt/mm/mysql/MysqlDefs.class
> 12199 Sun Dec 31 06:44:40 EST 1995 org/gjt/mm/mysql/MysqlIO.class
> 18828 Sun Dec 31 06:44:42 EST 1995
> org/gjt/mm/mysql/PreparedStatement.class
> 1013 Sun Dec 31 06:44:42 EST 1995
> org/gjt/mm/mysql/PushBackTokenizer.class
> 15369 Sun Dec 31 06:44:44 EST 1995 org/gjt/mm/mysql/ResultSet.class
> 4465 Sun Dec 31 06:44:44 EST 1995
> org/gjt/mm/mysql/ResultSetMetaData.class
> 3709 Sun Dec 31 06:44:44 EST 1995 org/gjt/mm/mysql/SQLError.class
> 6058 Sun Dec 31 06:44:44 EST 1995 org/gjt/mm/mysql/Statement.class
> 490 Sun Dec 31 06:44:46 EST 1995 org/gjt/mm/mysql/Token.class
> 2839 Sun Dec 31 06:44:46 EST 1995 org/gjt/mm/mysql/Util.class
>
> I created a MySQL database called menagerie (as shown in the tutorial).
> It contains a table named pet with this data:
>
> mysql> select * from pet;
> +----------+--------+---------+------+------------+------------+
> | name | owner | species | sex | birth | death |
> +----------+--------+---------+------+------------+------------+
> | Fluffy | Harold | cat | f | 1993-02-04 | NULL |
> | Claws | Gwen | cat | m | 1994-03-17 | NULL |
> | Buffy | Harold | dog | f | 1989-05-13 | NULL |
> | Fang | Benny | dog | m | 1990-08-27 | NULL |
> | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
> | Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
> | Whist | Gwen | bird | NULL | 1997-12-09 | NULL |
> | Slim | Benny | snake | m | 1996-04-29 | NULL |
> | Puffball | Diane | hamster | f | 1999-03-30 | NULL |
> +----------+--------+---------+------+------------+------------+
> 9 rows in set (0.08 sec)
>
> The database is on the machine that I'm working on (not a remote
> machine). My code to test if the driver works is a below (it's a
> servlet). Any ideas why I get the ClassNotFoundException and cannot load
> the driver? Thanks in advance.
>
> Armando Singer
>
> /**
> * Jdbc-test
> *
> * Created: Mon Jun 7 15:54:58 1999
> *
> * @author Armando Singer
> * @version
> */
>
> import java.io.*;
> import java.sql.*;
> import java.util.*; // Properties needs this
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class Jdbctest extends HttpServlet {
>
> public void doGet(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
>
> Connection con = null;
> Statement stmt = null;
> ResultSet rs = null;
>
> res.setContentType("text/html");
> PrintWriter out = res.getWriter();
>
> try {
>
> // Note (from mm.mysql JDBC driver documentation): If you forget
> the
> // newInstance()
> // call your application will not be able to use MM.MySQL in
> // applets because of a class-loading bug in both MS's and
> // Netscape's JVM for their browsers. Therefore be safe and
> // always add the newInstance() call when registering any JDBC
> driver.
>
> Class.forName("org.gjt.mm.mysql.Driver").newInstance();
>
> // Register the driver via the system properties variable
> // "jdbc.drivers"
>
> Properties P = System.getProperties();
> P.put("jdbc.drivers", "org.gjt.mm.mysql.Driver");
> System.setProperties(P);
>
> con = DriverManager.getConnection("jdbc:mysql:menagerie");
> // The URL string for the getConnection() method has the following
> format:
> //
> jdbc:mysql://hostname[:port]/database[?extra_param1=value][&extra_param2=value]
>
> stmt = con.createStatement();
> rs = stmt.executeQuery("select name, birth from pets");
>
>
> out.println("<html><head><title>Menagerie</title></head>");
> out.println("<body>");
> out.println("<ul>");
> while(rs.next()) {
> out.println("<li>" + rs.getString("name") + " ---> " +
> rs.getString("birth"));
> }
> out.println("</ul></body></html>");
> }
>
> catch (ClassNotFoundException e) {
> out.println("Couldn't load JDBC driver: " + e.getMessage());
> }
>
> catch (SQLException e) {
> out.println("SQLException caught: " + e.getMessage());
> }
>
> // InstantiationException and IllegalAccessException must be caught
> // because of the newInstance() method.
> catch (InstantiationException e) {
> out.println("InstantiationException caught: " + e.getMessage());
> }
>
> catch (IllegalAccessException e) {
> out.println("IllegalAccessException caught: " + e.getMessage());
> }
>
> finally {
> try {
> if (con != null) con.close();
> }
> catch (SQLException ignored) { }
> }
> }
> } // Jdbc-test
Hi Armando
This could be a problem with the servlet engine you use.
If you use Apache JServ for example, it will not use the system CLASSPATH by default.
What servlet engine are you using (name + version please)?
Also your connect URL is wrong.
It has to be:
con = DriverManager.getConnection("jdbc:mysql://localhost/menagerie");
Also I would suggest to give an user/password pair like:
con = DriverManager.getConnection("jdbc:mysql://localhost/menagerie"
"mysqlServlet", "servletPasswd");
Tschau
Christian