Results 1 to 3 of 3
  1. #1
    Josisco is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default NoClassDefFoundError

    I'm trying to follow Horstmann's 'Database Programming in Java' in Big Java.
    I've copied it exactly, but can't get it working and get NoClassDefFoundError.

    Here is the code:
    SimpleDataSource:
    Java Code:
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    public class SimpleDataSource
    {
        public static void init(String fileName) throws IOException, ClassNotFoundException
        {
            Properties props = new Properties();
            FileInputStream in = new FileInputStream(fileName);
            props.load(in);
    
            String driver = props.getProperty("jdbc.Driver");
            url = props.getProperty("jdbc.url");
            username = props.getProperty("jdbc.username");
            if (username == null)
                username = "";
            password = props.getProperty("jdbc.password");
            if (password == null)
                password = "";
            if (driver != null)
                Class.forName(driver);
        }
    
        public static Connection getConnection() throws SQLException
        {
            return DriverManager.getConnection(url,username,password);
        }
        private static String url;
        private static String username;
        private static String password;
    }
    TestDB.java
    Java Code:
    import java.io.File;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class TestDB
    {
        public static void main(String[] args) throws Exception
        {
            if (args.length == 0)
            {
                System.out.println("Usage: java -classpath driver_class_path"
                        +File.pathSeparator
                        +". TestDB database.properties");
                return;
            }
            else
                SimpleDataSource.init(args[0]);
    
            Connection conn = SimpleDataSource.getConnection();
            try
            {
                Statement stat = conn.createStatement();
    
                stat.execute("CREAT TABLE Test (Name CHAR(20))");
                stat.execute("INSERT INTO Test VALUES('Romeo')");
    
                ResultSet result = stat.executeQuery("SELECT * FROM Test");
                result.next();
                System.out.println(result.getString("Name"));
    
                stat.execute("DROP TABLE Test");
            }
            finally
            {
                conn.close();
            }
        }
    }
    database.properties
    Java Code:
    jdbc.url = jdbc:mysql://localhost:3306/university;create=true
    jdbc.username = admin
    jdbc.password = password
    jdbc.driver = com.mysql.jdbc.Driver

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    do you have the mysql connector jar file, and then have this added to the classpath ?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. noClassDefFoundError
    By imorio in forum Eclipse
    Replies: 11
    Last Post: 08-21-2010, 08:45 PM
  2. NoClassDefFoundError
    By Dbirge in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 06:01 PM
  3. help with NoClassDefFoundError
    By chikoyzki06 in forum New To Java
    Replies: 2
    Last Post: 11-25-2009, 12:38 PM
  4. Message NoClassDefFoundError
    By karim in forum Eclipse
    Replies: 1
    Last Post: 03-20-2009, 04:43 PM
  5. NoClassDefFoundError
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 01:20 PM

Posting Permissions

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