0

I am trying to do a simple select statement from MySQL, but I keep getting a ClassNotFound exception for some reason. All of the examples I see online use this process so I do not know what is wrong.

NOTE: I am NOT trying to connect to a localhost. I am trying to connect to the IP that you see.

try {

            String myDriver = "com.mysql.jdbc.Driver";
            String myUrl = "jdbc:mysql://192.168.1.28/IT252";
            Class.forName(myDriver);
            Connection conn = DriverManager.getConnection(myUrl, "school", "sch00lp4ss");

            String query = "SELECT * FROM representative";

            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                int id = rs.getInt("rep_id");
                String firstName = rs.getString("rep_firstname");
                String lastName = rs.getString("rep_lastname");

                System.out.printf("%d, %s, %s %n", id, firstName, lastName);
            }
            st.close();
        } catch (Exception e) {

            e.printStackTrace();
        }

The stacktrace.

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: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 it252.gui.addSale.<init>(addSale.java:48)
    at it252.gui.mainWindow.lambda$new$2(mainWindow.java:37)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I do not know how to check to see if the Driver is installed and I do not know how to add the Driver to the classpath in any case. The JDBC should include this driver should it not? Also, how would I go about adding the Driver to the classpath? I thought this was all supposed to be built in.

Cœur
  • 37,241
  • 25
  • 195
  • 267
MAD-HAX
  • 173
  • 1
  • 7

2 Answers2

0

Most of the time ClassNotFoundException arises, when it does not find the required Driver. Make sure you have connector installed correctly and the CLASSPATH set up. try using this Class.forName("com.mysql.jdbc.Driver").newInstance();

0

you probably haven't added mysqlconnector to your library either. you have to download it from https://dev.mysql.com/downloads/connector/j/ and then put it inside the folder of your project. from there you can add it to the library.for example in eclipse you have to right click on your project-> properties-> java build path-> add jars-> from there find the j-connector inside the project folder and add it

i hope this could help. btw make sure that your sql query is not wrong and you dont have any syntax errors

parsa
  • 987
  • 2
  • 10
  • 23
  • The first paragraph makes no sense. The IP address or hostname does not need to be 127.0.01/localhost. – Mark Rotteveel Feb 12 '18 at 09:55
  • if he wants to connect to a local database it has to be either 127.0.0.1 or localhost. i figured since he used 192.168.1.28 he was trying to connect to localhost. but you are right my sentence doesn't make sense since you can connect to any server's database as well. i'll correct it – parsa Feb 12 '18 at 11:46
  • Given the OP didn't specify that he wanted to connect local (and since has explicitly updated to explicitly say he doesn't want to connect locally), that shouldn't be something that would be the opening of your answer. – Mark Rotteveel Feb 12 '18 at 12:06