title: can't display the image in GUI from mysql - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
body: <p>I have a MySQLConnect class which derives the image from MySQLServer and converts it from BLOB to a image, the MySQLConnect itself is working but i want to create a instance of it in Controller.java in order to be able to view the derived image from DB in the GUI </p>
  
  <p>Here is the code for MySQLConnect:</p>
  
  <pre><code>package sample;
  
  import javax.imageio.ImageIO;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.image.BufferedImage;
  import java.io.InputStream;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.Statement;
  
  
  public class MysqlConnect {
      /*
       * set the default variables for mysql connection
       * */
      private static final String url ="";
  
      private static final String user = "";
  
      private static final String password = "";
  
  
      public BufferedImage image()
      {
          System.out.println("I am in Image");
          try {
              System.out.println("trying to find  com.mysql.jdbc.driver");
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection(url, user, password);
              System.out.println("Success");
  
              Statement stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery("select image from images where state = 1");
  
              if (rs.next())
              {
                  java.sql.Blob blob = rs.getBlob(1);
                  InputStream in = blob.getBinaryStream(1,blob.length());
                  System.out.println(!rs.next());
                  System.out.println(in);
  
                  BufferedImage image = ImageIO.read(in);
                  System.out.println(image);
  
  
  
                  /*
                  Display the image
                   */
                  ImageIcon icon = new ImageIcon(image);
                  JFrame frame=new JFrame();
                  frame.setLayout(new FlowLayout());
                  frame.setSize(500,800);
                  JLabel lbl=new JLabel();
                  lbl.setIcon(icon);
                  frame.add(lbl);
                  frame.setVisible(true);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
                  return image;
  
              }
  
              con.close();
              System.out.println("reached here");
  
  
          } catch (Exception e)
          {
              e.printStackTrace();
          }
  
          return null;
      }
  
  }
  </code></pre>
  
  <p>Code for Controller.java:</p>
  
  <pre><code>package sample;
  import javafx.scene.image.Image;
  import javafx.scene.image.ImageView;
  
  import java.awt.image.BufferedImage;
  
  
  public class Controller
  {
      private ImageView imageView;
  
  
  
      public void  SubmitButtonClicked(){System.out.println("it is Submitted ");}
  
      public void NextButtomClicked()
      {
          MysqlConnect image = new MysqlConnect();
  
          BufferedImage img = image.image();
  
          imageView.setImage(Image.impl_fromPlatformImage(img));
  
      }
  
  
  
  }
  </code></pre>
  
  <p>Code for Sample.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.TextField?&gt;
  &lt;?import javafx.scene.image.ImageView?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  &lt;?import javafx.scene.layout.Region?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  
  &lt;BorderPane prefHeight="576.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"&gt;
     &lt;bottom&gt;
        &lt;VBox prefHeight="98.0" prefWidth="600.0" BorderPane.alignment="CENTER"&gt;
           &lt;children&gt;
              &lt;HBox prefHeight="159.0" prefWidth="648.0"&gt;
                 &lt;children&gt;
                    &lt;TextField promptText="Name:"&gt;
                       &lt;HBox.margin&gt;
                          &lt;Insets bottom="20.0" left="20.0" right="20.0" top="20.0" /&gt;
                       &lt;/HBox.margin&gt;
                    &lt;/TextField&gt;
                    &lt;Region HBox.hgrow="ALWAYS" /&gt;
                    &lt;Button mnemonicParsing="false" text="Decline"&gt;
                       &lt;HBox.margin&gt;
                          &lt;Insets left="25.0" right="25.0" top="50.0" /&gt;
                       &lt;/HBox.margin&gt;
                    &lt;/Button&gt;
                    &lt;Button layoutX="181.0" layoutY="10.0" mnemonicParsing="false" onAction="#SubmitButtonClicked" text="Submit"&gt;
                       &lt;HBox.margin&gt;
                          &lt;Insets right="10.0" top="50.0" /&gt;
                       &lt;/HBox.margin&gt;
                    &lt;/Button&gt;
                 &lt;/children&gt;
              &lt;/HBox&gt;
           &lt;/children&gt;
        &lt;/VBox&gt;
     &lt;/bottom&gt;
     &lt;left&gt;
        &lt;ImageView fx:id="imageView" fitHeight="341.0" fitWidth="284.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER_RIGHT"&gt;
           &lt;BorderPane.margin&gt;
              &lt;Insets bottom="20.0" left="20.0" right="20.0" top="8.0" /&gt;
           &lt;/BorderPane.margin&gt;
  
        &lt;/ImageView&gt;
     &lt;/left&gt;
     &lt;top&gt;
        &lt;HBox prefHeight="94.0" prefWidth="600.0" BorderPane.alignment="CENTER"&gt;
           &lt;children&gt;
              &lt;Button mnemonicParsing="false" prefHeight="30.0" prefWidth="74.0" text="Previous"&gt;
                 &lt;HBox.margin&gt;
                    &lt;Insets left="10.0" right="10.0" top="8.0" /&gt;
                 &lt;/HBox.margin&gt;
              &lt;/Button&gt;
              &lt;Region prefHeight="166.0" prefWidth="144.0" /&gt;
              &lt;Button mnemonicParsing="false" onAction="#NextButtomClicked" prefHeight="30.0" prefWidth="74.0" text="Next"&gt;
                 &lt;HBox.margin&gt;
                    &lt;Insets top="8.0" /&gt;
                 &lt;/HBox.margin&gt;
              &lt;/Button&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
     &lt;/top&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  
  <p>Error i get once i click the next button:</p>
  
  <pre><code>it is Submitted 
  I am in Image
  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  trying to find  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:335)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:264)
      at sample.MysqlConnect.image(MysqlConnect.java:30)
      at sample.Controller.NextButtomClicked(Controller.java:27)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Node.fireEvent(Node.java:8413)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:417)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
      at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
      at java.lang.Thread.run(Thread.java:748)
  Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Node.fireEvent(Node.java:8413)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:417)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
      at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
      ... 48 more
  Caused by: java.lang.NullPointerException
      at sample.Controller.NextButtomClicked(Controller.java:29)
      ... 58 more
  </code></pre>
  
  <p>THANK YOU IN ADVANCE</p>