Find the answer to your Linux question:
Results 1 to 2 of 2
I have recently installed Java in my Ubuntu 9.04 environment so that I can teach myself Java. Basic Java programs work, but I am having trouble connecting to a MySQL ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Location
    Deep in the heart of Texas
    Posts
    40

    Java installation error connecting to MySQL database

    I have recently installed Java in my Ubuntu 9.04 environment so that I can teach myself Java. Basic Java programs work, but I am having trouble connecting to a MySQL database. I installed these packages using Synaptic Package Manager.

    1.gsfonts-x11
    2.java-common
    3.odbcinst1debian1
    4.sun-java6-bin
    5.sun-java6-jdk
    6.sun-java6-jre
    7.unixodbc

    After these packages were installed, I was not sure if I needed to do this, but I downloaded the MySQL connector from MySQL :: MySQL Connectors and installed in a directory called /home/mark/mysql/.

    I then did "sudo gedit /etc/environment" and added a CLASSPATH to where I installed the connector:

    Code:
    CLASSPATH="/home/mark/mysql/mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar"
    I found an example Java program from Using JDBC with MySQL, Getting Started and stripped it down even further to test the MySQL connection. This is the program I am running:

    Code:
    import java.sql.*;
    public class Jdbctest {
      public static void main(String[] args){
        System.out.println("running Jdbctest.java");
        try {
          Statement stmt;
          //Register the JDBC driver for MySQL.
          Class.forName("com.mysql.jdbc.Driver");
          //Define URL of database server for database named mysql 
          // on the localhost with the default port number 3306.
          String url = "jdbc:mysql://localhost:3306/mysql";
          //Get a connection to the database for a
          // user named root with password carrot.
          // This user is the default administrator
          // having full privileges to do anything.
          Connection con = DriverManager.getConnection(url,"root", "carrot");
          //Display URL and connection information
          System.out.println("URL: " + url);
          System.out.println("Connection: " + con);
          //Get a Statement object
          stmt = con.createStatement();
          //Select from a table
          stmt.executeQuery("SELECT host, user FROM mysql.user ");
          con.close();
        }catch( Exception e ) {
          e.printStackTrace();
        }//end catch
      }//end main
    }//end class Jdbctest
    I am getting this error:

    Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: Jdbctest
    Caused by: java.lang.ClassNotFoundException: Jdbctest
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    Could not find the main class: Jdbctest.  Program will exit.
    I am new to Linux, Ubuntu, Java and MySQL, so I have probably left out something that is very obvious. Any help would be greatly appreciated.

  2. #2
    Just Joined!
    Join Date
    May 2009
    Location
    Deep in the heart of Texas
    Posts
    40
    I have resolved the issue. I determined that after creating the CLASSPATH variable, it could no longer find the path to my class that I was running. I added ".:" to the beginning of CLASSPATH so that it would find my class in the current folder. The revised CLASSPATH looks like this:

    Code:
    CLASSPATH=".:/home/mark/mysql/mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...