#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    3
    Rep Power
    0

    MySQL and Connector/J


    Hello All,

    I've got a MySQL 4.0.10(Gamma) database running with a table called demo in a database called emp.

    I'm attempting to use the Connector/J 3.0.6 to get data from the database in a Java application, eventually i'll be using Servlets, but for now I'm just attempting to get anything to work.

    I'm using the following code:


    Code:
    //Program1.java
    // the very simplest JDBC program
    // make JDBC package available
    import java.sql.*;
    class Program1 {
    	public static void main(String argv[]) {
    		try {
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			
    			//Connect to MySQL giving user id and password
    			String connectionURL = "jdbc:mysql://localhost:3306/emp";
    			Connection connection = null;
    			connection = DriverManager.getConnection(connectionURL,"root","");
    			
    			// set up and execute SQL retrieval statement
    			Statement stmt = connection.createStatement();
    			String SQLStatement = "SELECT * FROM emp.demo";
    			ResultSet rs = stmt.executeQuery(SQLStatement);
    			
    			// move to first row of result set
    			rs.next();
    			
    			// extract name from first row and print
    			String theName = rs.getString("name");
    			System.out.println("the name is : " + theName);
    			
    			// tidy up and disconnect
    			rs.close();
    			stmt.close();
    			connection.close();
    		} catch( ClassNotFoundException e ) {
    			// print trace of error
    			System.err.println("Couldn't find the the MySQL " + "database driver: " + e.getMessage());
    		} catch( Exception e){	
    			e.printStackTrace();
    		}
    	}
    }

    I've got J2SE 1.4.0 and J2EE 1.3 installed the code above compiles without a problem but on execution i get:


    Code:
    C:\Java>java Program1
    Exception in thread "main" java.lang.NoClassDefFoundError: Program1
    
    C:\Java>

    I'm new to this whole databases aspect of Java so any help would be great!

    Thanks in advance everyone!

    Chris
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    It probably cannot find the com.mysql.jdbc.Driver object. Make sure this is in your classpath. To run a program in main, you can assign it to the CLASSPATH environment variable or you can set it at execution time using this on the command line:
    Code:
    java Program1 -cp c:\where\the\jar\file\is\mysqldriver.jar
    When doing this in your servlet you want to make sure the jar file is in WEB-INF/lib.

    Hope this helps

IMN logo majestic logo threadwatch logo seochat tools logo