0

It may be similar questions to other in SO but most answers were related to the server configuration or IDE Configuration. Here I am trying to achieve the connection to Local Mysql Database using Notepad as text editor for my Classes and CMD to run. So, here I have a Folder called 'Learning' where my register.java and register.class lies. My register.java has:

jbsubmit.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent ae){

        try{
        Class.forName("com.mysql.jdbc.Driver");         
        Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/demo","root","LJV9txSRof44Xmvk");

        String name = tfname.getText();
        String email = tfemail.getText();
        String pass = tpfpass.getText();

        Statement stmt = con.createStatement();

        stmt.executeQuery("INSERT INTO user (name, email, password) VALUES(name, email, pass)");

        }
        catch(Exception e){
            e.printStackTrace();
        }

        }
    });

I am aware that Class.forName("com.mysql.jdbc.Driver"); not required since v4 & my connector version is 5.1.42. When using Class.forName I get ClassNotFoundException: com.mysql.jdbc.Driver and when not using Class.forName I get No suitable driver found for jdbc:mysql://localhost:3306/dbname

I've tried

  • Put the full path to the jarfile in the classpath (including the filename .jar)
  • Checked classpath several times for confirmation
  • using Class.forName("com.mysql.jdbc.Driver").newInstance();
  • Keeping connector in C:\Program Files\Java\jdk1.8.0_121\bin as well as C:\Program Files\Java\jre1.8.0_121\lib\ext
  • Restarted CMD everytime after classpath changes
  • Checked mySql port and connection port above are same
VipiN Negi
  • 2,994
  • 3
  • 24
  • 47
  • How do you run your code exactly? Looks like a classpath issue. – ipper Jul 07 '17 at 09:33
  • @ipper I run file with CMD & this is my classpath : C:\Program Files\Java\jdk1.8.0_121\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Java\jdk1.8.0_121\bin\mysql-connector-java-5.1.42-bin.jar – VipiN Negi Jul 07 '17 at 09:36
  • What is the exact order of arguments? java -cp ... register – ipper Jul 07 '17 at 10:58
  • You probably have other dependencies that you need, hence the other items in your classpath. Have you tried stripping your class to a bare minimum? – ipper Jul 07 '17 at 11:06
  • 1
    What you list as your "classpath" in your previous comment looks an awful lot like the `PATH` environment variable. `PATH` and `CLASSPATH` are not the same thing. – Gord Thompson Jul 07 '17 at 14:38

0 Answers0