1

Hello i am creating a small application in which i would like to register users to database. It seems fine but it tells me that i have a problemm with the driver and i cannot spot it.

My Code:

private void createEventListenerDBProperties() {
        dbSubmitBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                    if (dbDriverChooser.getSelectedItem().equals("com.mysql.jdbc.Driver")) {
                        driver = (String) dbDriverChooser.getSelectedItem();
                        port = dbPortField.getText();
                        host = "jdbc:mysql://" + hostField.getText() + ":" + port + "/";
                    } else if (dbDriverChooser.getSelectedItem().equals("oracle.jdbc.driver.OracleDriver")) {
                        driver = (String) dbDriverChooser.getSelectedItem();
                        port = dbPortField.getText();
                        host = "jdbc:oracle:thin:@" + hostField.getText() + ":" + port + ":";
                    } else if (dbDriverChooser.getSelectedItem().equals("org.postgresql.Driver")) {
                        driver = (String) dbDriverChooser.getSelectedItem();
                        port = dbPortField.getText();
                        host = "jdbc:postgresql://" + hostField.getText() + ":" + port + "/";
                    } else {
                        driver = (String) dbDriverChooser.getSelectedItem();
                        host = "jdbc:sqlite:";
                    }
                    db = dbnameField.getText();
                    dbuser = dbUsernameField.getText();
                    dbpassword = new String(dbPasswordField.getPassword());
                    main.remove(dbProperties);
                    main.add(register);
                    main.revalidate();
                }
        });
    }

My connection method:

private Connection instanciateDB() {
        Connection con = null;
        try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(host + db, dbuser, dbpassword);
            System.out.println("Connection Established");
        } catch (ClassNotFoundException | SQLException | InstantiationException |IllegalAccessException e) {
            System.out.println("Connection not Established");
            JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return con;
    }

My Registration Method:

private void setupRegisterEventListener() {
        registerBtn.addActionListener((ActionEvent e) -> {

                try {
                    conn = instanciateDB();
                    pst = conn.prepareStatement("insert into usersinfo(username, password) values(?,?)");
                    pst.setString(1, leftFirstText);
                    pst.setString(2, new String(leftSecondText));
                    int x = pst.executeUpdate();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(MainFrame.this, "Registration Failed");
                    System.out.println(ex.getMessage());
                }
                JOptionPane.showMessageDialog(MainFrame.this, "Registration Successful", "Success", JOptionPane.PLAIN_MESSAGE);
        });
    }

And it gives me error com.mysql.jdbc.Driver and i dont know why in the instanciateDB method.

Connection not Established
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ijae4.xdrako00.MainFrame.lambda$setupRegisterEventListener$0(MainFrame.java:126)
    at ijae4.xdrako00.MainFrame$$Lambda$13/1389133897.actionPerformed(Unknown Source)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    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:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    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)

Any ideas?

Thank you in advance!

Kostas Drak
  • 2,859
  • 6
  • 25
  • 58
  • `== "..."` [don't compare strings with `==`. Use `equals`.](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Dec 19 '14 at 20:42
  • the same problemm...i have the driver installed but i dont know why it gives me that error – Kostas Drak Dec 19 '14 at 20:46
  • do you think that it fails because i remove the dbProperties panel and i add the registration panel? does that make Strings loose their values?? – Kostas Drak Dec 19 '14 at 20:48
  • I didn't say it was solution to your problem, you are using interned strings so your code would probably work also with `==` but it is generally better to avoid `==` when comparing objects. Anyway you need to say something more about problem you are facing: *gives me error com.mysql.jdbc.Driver* is very vague. Do you have any stacktrace? – Pshemo Dec 19 '14 at 20:49
  • 1
    BTW consider posting complete (but simplified) code which will reproduce your problem ([SSCCE](http://sscce.org/)), not everyone likes or has time to start everything from scratch just to test few methods. – Pshemo Dec 19 '14 at 20:52
  • yes check my question – Kostas Drak Dec 19 '14 at 20:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67351/discussion-between-kostasmatrix-and-pshemo). – Kostas Drak Dec 19 '14 at 20:57
  • About NPE in your stacktrace: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Pshemo Dec 19 '14 at 21:04
  • What you have posted is not a completed, self contained, compliable example which demonstrates your problem. Take a look at [MCVE](https://stackoverflow.com/help/mcve) of the details we are looking for in order to help you further – MadProgrammer Dec 19 '14 at 21:07
  • 1
    Use `Exception#printStackTrace` instead of just printing the error message within your `try-catch` blocks, it mat present more useful information – MadProgrammer Dec 19 '14 at 21:09
  • thanks for your help guys!!! – Kostas Drak Dec 19 '14 at 21:15

1 Answers1

2

Right click on Libraries -> Add Library -> MySql JDBC Driver - these are the steps for Netbeans IDE

SummerCode
  • 1,333
  • 1
  • 13
  • 26