0

I'm making a database application for a school assignment and for some reason, the application just won't connect with the mysql database. I'm using the mysql workbench, version 8+ and the connector file's version is 8.0.27. Using netbeans, I followed all the steps to make the connection but yet, in the output window, when I click the add new record or update button, it doesn't respond.

Here's the code that I think is relevant to this question

//1st excerpt
public class WarehouseDBM extends javax.swing.JFrame {

private static final String username ="root";
private static final String password ="";
private static final String dataConnection = "jdbc:mysql://localhost:3306/Warehouse_Database";


Connection sqlConnection = null;
PreparedStatement prepStat = null;
ResultSet resultSet = null;
//2nd excerpt
   private void btnAddNewActionPerformed(java.awt.event.ActionEvent evt) {                                          
       
    try
    {
        Class.forName("com.mysql.cj.jdbc.driver");
        
        sqlConnection = DriverManager.getConnection(dataConnection, username, password);
        prepStat = sqlConnection.prepareStatement("Insert into Warehouse_Data ( Item_ID, Item_Name, Existing_Quantity, Quantity_Added, Quantity_Used) values"
                + "(?,?,?,?,?)");
        prepStat.setString(1,txtItemId.getText());
        prepStat.setString(2,txtItemName.getText());
        prepStat.setString(3,txtExistingQuantity.getText());
        prepStat.setString(4,txtQuantityAdded.getText());
        prepStat.setString(5,txtQuantityUsed.getText());
        System.out.println("Connected Successfully");
       
        prepStat.executeUpdate();
        JOptionPane.showMessageDialog(this, "Item record added");
        
    }
    catch (ClassNotFoundException | SQLException ex) {
        java.util.logging.Logger.getLogger(WarehouseDBM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
   
            
}

Once I click the add new item button, the console shows the following error

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:377)
at warehousesterling.WarehouseDBM.btnAddNewActionPerformed(WarehouseDBM.java:269)
at warehousesterling.WarehouseDBM$4.actionPerformed(WarehouseDBM.java:206)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6379)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Also do note, I have included the jar file in the library of this application, and I still face this error. I've tried multiple solutions given on the net but to no avail. If there is anything I have missed, please do tell me.

Edit: To add it to the library i followed the following steps: Project--> right click on libraries--> add jar/folder --> selected the jar file.

Achint
  • 1
  • 1
  • [Connect Java to a MySQL database](https://stackoverflow.com/a/2840358) – 001 Nov 23 '21 at 13:28
  • 1
    Did you just post your password on a public website? – rzwitserloot Nov 23 '21 at 13:29
  • 1
    You did not include the jar file - hence that error. Your code is fine (well, it's got various problems, but it doesn't explain the error you got). The problem is in whatever you did to 'include the jar file' - you didn't do it correctly. The question doesn't contain any information about what you did, so I don't think anyone can answer as is. Perhaps update it, forget about the code, talk about what you're doing to 'include the library'. – rzwitserloot Nov 23 '21 at 13:30
  • 2
    `com.mysql.cj.jdbc.driver` != `com.mysql.cj.jdbc.Driver`, class names are case-sensitive. – M. Deinum Nov 23 '21 at 14:36
  • @rzwitserloot yeah it does have a lot of problems, but i'm only a beginner at programming and this is for a school assignment, so ill improve it slowly as i learn more and more. Also for the libraries part, ive edited the question to include what i did to add the jar file in the library – Achint Nov 23 '21 at 17:40
  • @M.Deinum yeah, that didn't fix it for me, it still says class not found for me. – Achint Nov 23 '21 at 17:44
  • @rzwitserloot also yeah my mistake, i was frustrated with this code not working and i really wanted a solution so i quickly posted this, i've removed the pass now – Achint Nov 23 '21 at 17:46

0 Answers0