Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2006
    Posts
    41

    Unanswered: Need help setting up JDBC with mySQL and Netbeans

    Hi,

    I need some help setting up JDBC in netbeans to interact with a database in mySQL.

    I've successfully got mySQL up and running and created a few simple test databases. I've been following the instructions on this website (http://www.stardeveloper.com/article...3090401&page=1 ) to get JDBC working but have had little luck.

    I've downloaded JDBC from mySQL website, extracted the mysql-connector-java-3.1.13-bin.jar file and changed the class path to :

    C:\Program Files\Java\jre1.5.0_06\lib\ext\mysql-connector-java-3.1.13-bin

    which is where I placed the .jar file.

    I then used the following code provided on stardeveloper.com to test a connection with the test database.

    Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
     
    public class JdbcExample2 {
     
      public static void main(String args[]) {
        Connection con = null;
     
        try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          con = DriverManager.getConnection("jdbc:mysql:///test",
            "root", "secret");
     
          if(!con.isClosed())
            System.out.println("Successfully connected to " +
              "MySQL server using TCP/IP...");
     
        } catch(Exception e) {
          System.err.println("Exception: " + e.getMessage());
        } finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
        }
      }
    }


    It compiles but when run I get the error message :

    Code:
    java.lang.NoClassDefFoundError: testdatabase/JdbcExample2
    Exception in thread "main"

    I assume this means that my classpath isn't setup correctly but I have no idea what's up and how to correct it.

    Can someone PLEASE tell me how to setup jdbc and what I've done wrong.

    Thanks in advance

  2. #2
    Join Date
    Jan 2005
    Posts
    362
    You must add the driver to the project's library from project properties.
    DImis

  3. #3
    Join Date
    Mar 2006
    Posts
    41
    Thanks for that but it still doesn't work.

    I created a new project called Databases, I then right clicked the project name and selected properties. Clicked Libraries and under Compile I clicked Add JAR/Folder and added mysql-connector-java-3.1.13-bin.jar (the driver).

    I used the following code to test the conection:

    Code:
    import java.sql.*;
    
       public class Connect
       {
           public static void main (String[] args)
           {
               Connection conn = null;
    
               try
               {
                   String userName = "Nick";
                   String password = "0121429";
                   String url = "jdbc:mysql://localhost/test";
                   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                   conn = DriverManager.getConnection (url, userName, password);
                   System.out.println ("Database connection established");
               }
               catch (Exception e)
               {
                   System.err.println ("Cannot connect to database server");
               }
               finally
               {
                   if (conn != null)
                   {
                       try
                       {
                           conn.close ();
                           System.out.println ("Database connection terminated");
                       }
                       catch (Exception e) { /* ignore close errors */ }
                   }
               }
           }
       }
    but still get the error message:

    Code:
    java.lang.NoClassDefFoundError: database/Connect
    Exception in thread "main" 
    Java Result: 1
    Have I "installed" the driver incorrectly or am I using the wrong driver or is it something else??

    Also, when I setup mySQL I wansn't asked to supply a username but did give a password. Do I use my windows admin name as a username.

    Sorry for the trouble and thanks for any help

  4. #4
    Join Date
    Jan 2005
    Posts
    362
    1. Is Mysql service "up"? (mysqld - install)
    2. Username Nick for mysql exists?
    dimis

  5. #5
    Join Date
    Mar 2006
    Posts
    41
    ok I've made some progress but my java code still wont successfully execute.

    Within mySQL I created a new user called uraknai with password n0121429 and granted him acess to the database pets.

    Then, in netbeans, I clicked on the Runtime tab and clicked Databases then right clicked drivers. I added the information for the driver and databases and clicked ok. It the connectd to the database

    I then ran the following java code:
    Code:
    import java.sql.*;
    
    public class Connect
    {
      public static void main(String args[]) {
        try {
          /* Test loading driver */
    
          String driver = "com.mysql.jdbc.Driver";
    
          System.out.println( "=> loading driver:" );
          Class.forName( driver );
          System.out.println( "OK" );
    
          /* Test the connection */
    
          String url = "jdbc:mysql://localhost/pet";
    
          System.out.println( "=> connecting:" );
          DriverManager.getConnection( url, "uraknai", "n0121429" );
          System.out.println( "OK" );
        }
        catch( Exception x ) {
          x.printStackTrace();
        }
      }
    }
    in an attempt to test the connection with the database and was given the error messge:

    Code:
    java.lang.NoClassDefFoundError: database/Connect (wrong name: Database/Connect)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
            at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    Exception in thread "main" 
    Java Result: 1
    The code compiles and it should work so I assume there's a problem with my netbeans setup.

    Can someone please help

  6. #6
    Join Date
    Jan 2005
    Posts
    362
    Quote Originally Posted by uraknai

    Within mySQL I created a new user called uraknai with password n0121429 and granted him acess to the database pets.


    Code:
    
          String url = "jdbc:mysql://localhost/pet";
    You say that you created db pets but at your code you wrote pet.

  7. #7
    Join Date
    Mar 2006
    Posts
    41
    Quote Originally Posted by dimis2500
    You say that you created db pets but at your code you wrote pet.
    oh sorry, that was a mistake. The database is actually called pet

  8. #8
    Join Date
    Jan 2005
    Posts
    362
    Try to "play" with the conection string, this is the problem.
    I used
    Code:
    Class.forName("org.gjt.mm.mysql.Driver");
    ocon = DriverManager.getConnection("jdbc:mysql:///some", "some", so");
    http://www.developer.com/java/data/a...ple%20Programs
    http://hashmysql.org/index.php?title=Java_Tutorial

  9. #9
    Join Date
    Jul 2006
    Posts
    8
    The problem is not the connection to the database ...
    You have a problem with you Main Class in Java...

    java.lang.NoClassDefFoundError: testdatabase/JdbcExample2
    Exception in thread "main"

    Every application has to have a Main Class, NetBeans builds a Jar File, then the main class should be into a Manifest.mf file.

    Maybe that is a wrong installation, or maybe you have to Define The "Main Project", select the project and File/Set main ... etc.

  10. #10
    Join Date
    Jul 2006
    Posts
    4

    Hi

    Quote Originally Posted by uraknai
    oh sorry, that was a mistake. The database is actually called pet
    Don't bothor about that ya

  11. #11
    Join Date
    Jul 2006
    Posts
    8
    Just for the record.
    Because im shure the problem was not resolved, by this thread.
    Anyway... ! Hi !

Posting Permissions

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