Results 1 to 13 of 13
  1. #1
    scchia is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default no mysql connection

    :(my pc is installed with fedora-8,LMAP is running.
    i installed netbeans 6.1 ide,the databases detected and the tables expanded
    easily.project ->java deskstop->databases->jdbc:mysqldriver->compile and run well.
    But when come to scripting :(from the tutorial)
    import java.sql.*;

    public class MysqlConnect{
    public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";
    try {
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
    System.out.println("Connected to the database");
    conn.close();
    System.out.println("Disconnected from database");
    } catch (Exception e) {
    System.out.println("Connection fails");
    }
    }
    }
    results :MySQL Connect Example.
    Connection fails.
    why?please help!!
    thank you.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Add a e.printStackTrace() to that catch block so that you can see why it fails.

    In real life do like be told simply "No.", or do you prefer "No, because ...."? It's the same thing with that catch block. Currently you are simply saying "No." without providing any kind of explanation.

  3. #3
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    Its probably because you do not have the driver installed,
    refer to eclipse/10180-connector-j-eclipse-mysql-5-0-x.htm -- That post

  4. #4
    scchia is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    i had tried to add e.printStackTrace() as this
    import java.sql.*;
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "acc";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";
    try {
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
    System.out.println("Connected to the database");
    conn.close();
    System.out.println("Disconnected from database");
    } catch (Exception e) {
    e.printStackTrace();
    }
    // TODO code application logic here
    }

    }
    and results :
    init:
    deps-jar:
    Compiling 1 source file to C:\Documents and Settings\Chia\My Documents\NetBeansProjects\mysql\build\classes
    compile:
    run:
    MySQL Connect Example.
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at mysql.Main.main(Main.java:28)
    BUILD SUCCESSFUL (total time: 0 seconds)
    netbeans comes with build-in mysql-connector-j-5.x.x.x
    the java jdk is 1.6 update 2.
    the above output is similar to winxp/mandriva2008.
    thank you.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    include the mysql jarfile on the classpath when you execute the application.

  6. #6
    scchia is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    sorry for this silly question(as a newbie)
    what is include the mysql jarfile on the classpath when you execute the application.?i really catch no ball!!!
    i really appreciate for details explanation.thank you.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    java -cp <path to mysqljarfile> yourclass
    See Lesson: Common Problems (and Their Solutions) (The Java™ Tutorials > Getting Started)

  8. #8
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    or you can simply download the mysql driver as said above and put it in your java local folder

    ..\Java\jdk1.6.0_05\jre\lib\ext\
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Are sure this is works? I have try this few years back, but it doesn't work actually. Only different is at that time I used jdk 1.4

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Eku View Post
    or you can simply download the mysql driver as said above and put it in your java local folder

    ..\Java\jdk1.6.0_05\jre\lib\ext\
    No. It "works", but you should never place anything in the jre language extension library folder. What happens when you upgrade java? You have to copy all the jarfiles there again. What happens when you have two applications that both use the same Java version, but need different
    versions of the Driver? What happens (when simply placing all jars in the same folder) two different jars have a class of the same name under the same package, but are completely incompatable? Which one will be loaded when that class is referenced? Also, why should this jar be loaded, and searched, when you start an application that has absolutely nothing to do with MySQL?

    You may say this is "far-fetched", but it does happen, and more often than you might think. Some of these are, IMHO, part of the reasons
    why Sun released Derby/JavaDB only as part of the JDK, rather than as a part of the JRE.

    Again, never, never place anything in jre/lib/ext. That is for language extensions not third party libraries, and for good reason.

  11. #11
    scchia is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    thank you for the replies.
    i had checked through the forum for *classpath*.there are plenty/countless of newbie stuck with the classpath pheonomena.
    i yet to try out yours suggestions ,but i sincerely the forum moderator/admin will come up short tutorial regarding this for wins/linux platform.
    thank you.

  12. #12
    scchia is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Thumbs up

    i think the solution found,just putting the mysql driver to ..\java..\ext directory.
    it works for wins / linux.
    thank you.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. mySQL HELP!!!
    By ace84 in forum New To Java
    Replies: 0
    Last Post: 04-19-2008, 11:39 PM
  2. Mysql Help
    By Sumendra Maharjan in forum JDBC
    Replies: 1
    Last Post: 08-08-2007, 02:19 AM
  3. MySql in NetBeans 5.0
    By javagal in forum NetBeans
    Replies: 6
    Last Post: 07-04-2007, 03:34 PM
  4. servlet using MySql
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 02:48 PM
  5. JSP and MySQL
    By Ed in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 06:08 AM

Posting Permissions

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