Forum Stats

  • 3,727,349 Users
  • 2,245,373 Discussions
  • 7,852,749 Comments

Discussions

mysql connectivity prob

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited March 2016 in New To Java

Hi,

I am trying to connect with mysql through java program but i am getting error:

My code is:

import java.sql.*;

public class FirstExample {

   // JDBC driver name and database URL

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 

   static final String DB_URL = "jdbc:mysql://localhost/test";

   //  Database credentials

   static final String USER = "root";

   static final String PASS = "z";

public static void main(String[] args) {

   Connection conn = null;

   Statement stmt = null;

   try{

      //STEP 2: Register JDBC driver

      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection

      System.out.println("Connecting to database...");

      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query

      System.out.println("Creating statement...");

      stmt = conn.createStatement();

      String sql;

      sql = "SELECT name, owner FROM pet";

      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set

      while(rs.next()){

         //Retrieve by column name

        

         String name = rs.getString("name");

         String owner = rs.getString("owner");

         //Display values

         System.out.print("name: " + name);

         System.out.print(", owner: " + owner);

        

      }

      //STEP 6: Clean-up environment

      rs.close();

      stmt.close();

      conn.close();

   }catch(SQLException se){

      //Handle errors for JDBC

      se.printStackTrace();

   }catch(Exception e){

      //Handle errors for Class.forName

      e.printStackTrace();

   }finally{

      //finally block used to close resources

      try{

         if(stmt!=null)

            stmt.close();

      }catch(SQLException se2){

      }// nothing we can do

      try{

         if(conn!=null)

            conn.close();

      }catch(SQLException se){

         se.printStackTrace();

      }//end finally try

   }//end try

   System.out.println("Goodbye!");

}//end main

}//end FirstExample



I am getting following error:


D:\Zulfi\java prog\2016\mysql>javac FirstExample.java

D:\Zulfi\java prog\2016\mysql>java FirstExample

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

        at java.lang.Class.forName0(Native Method)

        at java.lang.Class.forName(Class.java:264)

        at FirstExample.main(FirstExample.java:16)

Goodbye!

My path variable is:

>echo %path%

C:\jdk1.8.0_45\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Broadcom

\Broadcom 802.11\Driver;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem

;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\WIDCOMM\Bluetooth

Software\;c:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\

Broadcom\WHL\;C:\Program Files\Broadcom\WHL\syswow64;C:\Program Files\Broadcom\W

HL\SysWow64\;C:\Program Files\Broadcom\WHL\SysWow64\syswow64;D:\download\mysql-c

onnector-java-5.0.8.zip\mysql-connector-java-5.0.8;D:\download

jar file is located in download folder

>

D:\download>dir *.jar

Volume in drive D has no label.

Volume Serial Number is B269-AF38

Directory of D:\download

03/24/2016  09:49 PM           540,852 mysql-connector-java-5.0.8-bin.jar

               1 File(s)        540,852 bytes

               0 Dir(s)  609,787,019,264 bytes free

D:\download>

Somebody please guide me. I am using Windows 7 & mysql version is:

mysql-5.1.68-win32

Zulfi.

Best Answer

  • Unknown
    edited March 2016 Accepted Answer
    Please guide me what should be the extension of file. The extension is bin but the associated comment says that its a jar file.
    

    Do you even read what you, yourself, post?

     Directory of D:\download
    
    
    03/24/2016  09:49 PM           540,852 mysql-connector-java-5.0.8-bin.jar
                   1 File(s)        540,852 bytes
                   0 Dir(s)  609,787,019,264 bytes free
    

    That is what YOU posted!

    How can you POSSIBLY say the 'extension is bin'?

Answers

  • Unknown
    edited March 2016
    I am getting following error:
    
    D:\Zulfi\java prog\2016\mysql>javac FirstExample.java
    
    
    D:\Zulfi\java prog\2016\mysql>java FirstExample
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    

    Which means that class file is NOT on your classpath

     My path variable is:
    
    . . .
    onnector-java-5.0.8.zip\mysql-connector-java-5.0.8;D:\download
    
    jar file is located in download folder
    

    And there's your problem.

    The PATH is for OS executeable files.

    The CLASSPATH is for Java class and jar files. That 'not found' class is NOT on the CLASSPATH

    See The Java Tutorials trails for PATH and CLASSPATH

    https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited March 2016

    Hi,

    Thanks for your reply. Still i am getting the same sort of errors:

    G:\Zulfi\java prog\2016\mysql>java FirstExample

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

            at java.net.URLClassLoader.findClass(Unknown Source)

            at java.lang.ClassLoader.loadClass(Unknown Source)

            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

            at java.lang.ClassLoader.loadClass(Unknown Source)

            at java.lang.Class.forName0(Native Method)

            at java.lang.Class.forName(Unknown Source)

            at FirstExample.main(FirstExample.java:16)

    Goodbye!

    >echo %CLASSPATH%

    G:\mysqljarfile;.

    >cd \

    G:\>cd mysqljarfile

    G:\mysqljarfile>dir

    Volume in drive G has no label.

    Volume Serial Number is 20F1-0A6A

    Directory of G:\mysqljarfile

    03/25/2016  10:03 AM    <DIR>          .

    03/25/2016  10:03 AM    <DIR>          ..

    03/25/2016  09:19 AM           540,852 mysql-connector-java-5.0.8-bin.jar

                   1 File(s)        540,852 bytes

                   2 Dir(s)  70,458,638,336 bytes free

    G:\mysqljarfile>

    Somebody please guide me.

    Note I did not extract the file from zip directory. I went inside the zip folder and retrieved the file.

    Zulfi.

  • Unknown
    edited March 2016
    Thanks for your reply. Still i am getting the same sort of errors:
    

    Correct.

    Until you follow the advice I gave you and PUT THE JAR FILE on the CLASSPATH you will continue to get that error.

    >echo %CLASSPATH%
    G:\mysqljarfile;.
    

    Do YOU see the name of the jar file on that classpath above?

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited March 2016

    Hi,

    I have updated the classpath variables but now its giving me error related to the name of my main class file,

    >dir

    Volume in drive D has no label.

    Volume Serial Number is B269-AF38

    Directory of

    03/24/2016  09:25 PM    <DIR>          .

    03/24/2016  09:25 PM    <DIR>          ..

    03/27/2016  07:30 PM             2,464 FirstExample.class

    03/24/2016  09:42 PM             1,971 FirstExample.java

    03/24/2016  09:11 PM    <DIR>          java connectivity

    03/24/2016  09:02 PM            13,856 lab.docx

                   3 File(s)         18,291 bytes

                   3 Dir(s)  609,774,575,616 bytes free

    >java FirstExample

    Error: Could not find or load main class FirstExample

    >echo %CLASSPATH%

    D:\download\mysql-connector-java-5.0.8-bin

    >

    Some body please guide me.

    Zulfi.

  • Unknown
    edited March 2016
    I have updated the classpath variables
    

    No - you did NOT set the classpath as you were instructed, twice, to do.

     >echo %CLASSPATH%
    D:\download\mysql-connector-java-5.0.8-bin
    

    Really? Are you SURE that is the name of the jdbc jar file?

    You'd better go back and read your FIRST post and take another look.

    Some body please guide me.
    

    You seem to refuse to actually read the documentation. And you seem unable to follow the advice you were given.

    Until you change your mind you will NOT make much progress working with Java.

    but now its giving me error related to the name of my main class file,
    

    Yes - and I ALREADY TOLD you the problem you will get if you do NOT put jars and classes on the classpath.

    Which means that class file is NOT on your classpath
    

    So for the THIRD TIME - look at the classpath you just posted.

    Is your FirstExample class on that classpath? Please post your answer.

    The Java Tutorials trails I gave you the link to address ALL of your issues.

    2973483 wrote:
    
    Hi,
    I have updated the classpath variables but now its giving me error related to the name of my main class file,
    
    >dir
    Volume in drive D has no label.
    Volume Serial Number is B269-AF38
    
    
    Directory of
    
    
    03/24/2016  09:25 PM    <DIR>          .
    03/24/2016  09:25 PM    <DIR>          ..
    03/27/2016  07:30 PM             2,464 FirstExample.class
    03/24/2016  09:42 PM             1,971 FirstExample.java
    03/24/2016  09:11 PM    <DIR>          java connectivity
    03/24/2016  09:02 PM            13,856 lab.docx
                   3 File(s)         18,291 bytes
                   3 Dir(s)  609,774,575,616 bytes free
    
    
    >java FirstExample
    Error: Could not find or load main class FirstExample
    
    
    >echo %CLASSPATH%
    D:\download\mysql-connector-java-5.0.8-bin
    
    
    >
    
    Some body please guide me.
    
    Zulfi.
    

    https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

    The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, then you must include a dot (.) in the new settings.
    

    Good luck with your (already solved by me) 'problem'.

    Until you are willing to actually READ the documentation, especially the sections I specifically pointed you to, it appears that it is NOT possible for me to help you.

    I suggest you wait to see if others will respond.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited March 2016

    Hi,

    Even without looking your comment, I realized what mistake i was doing. Infact I was thinking actually in the words you have suggested.:

    >echo %ClassPath%

    \mysql-connector-java-5.0.8-bin;.

    >dir

    Volume in drive D has no label.

    Volume Serial Number is B269-AF38

    Directory of D:\Zulfi\java prog\2016\mysql

    03/24/2016  09:25 PM    <DIR>          .

    03/24/2016  09:25 PM    <DIR>          ..

    03/27/2016  07:30 PM             2,464 FirstExample.class

    03/24/2016  09:42 PM             1,971 FirstExample.java

    03/24/2016  09:11 PM    <DIR>          java connectivity

    03/24/2016  09:02 PM            13,856 lab.docx

                   3 File(s)         18,291 bytes

                   3 Dir(s)  609,774,551,040 bytes free

    >java FirstExample

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

            at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

            at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

            at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

            at java.lang.Class.forName0(Native Method)

            at java.lang.Class.forName(Class.java:264)

            at FirstExample.main(FirstExample.java:16)

    Goodbye!

    >

    After adding dot(.) in the CLASSPATH variable, the error has changed but its not new. Its the same error which i posted earlier in my first post. I mean we have come back to the point from where we started. Thanks for your help.

    Somebody please help me now. Please guide me what should be the extension of file. The extension is bin but the associated comment says that its a jar file.

    Zulfi.

  • Unknown
    edited March 2016 Accepted Answer
    Please guide me what should be the extension of file. The extension is bin but the associated comment says that its a jar file.
    

    Do you even read what you, yourself, post?

     Directory of D:\download
    
    
    03/24/2016  09:49 PM           540,852 mysql-connector-java-5.0.8-bin.jar
                   1 File(s)        540,852 bytes
                   0 Dir(s)  609,787,019,264 bytes free
    

    That is what YOU posted!

    How can you POSSIBLY say the 'extension is bin'?

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited March 2016

    Hi,

    <How can you POSSIBLY say the 'extension is bin'?>

    based upon the image which i have attached.windows prob with jar extension.jpg

    Actually windows does not show the jar extension (you can experiment it yourself). Even if you copy the file name using windows copy option, the jar extension will not be copied. I was not thinking this. So i got the problem.Thanks for your answer but my Question about extension was really the problem solver.

    Zulfi.

This discussion has been closed.