1

I created a derby database in Netbeans and decided to avoid using a username and password so that I would not risk forgetting it (This was my mistake).

After spending a lot of time creating my project in net beans, linking my database to various tables etc. I cannot get the code below to connect to the database. Is there a way to connect using the code without a password or username? or Could I some how add a username and password?

public class Database {   

private String DBusername = "";
private String DBpassword = "";
private String host = "jdbc:derby://localhost:1527/physioADB";

public Database(){

}

public Connection con() {


    try {

        Connection c = DriverManager.getConnection(host, DBusername, DBpassword);

        return c;
    } catch (SQLException e) {
        System.out.println(e);
        System.out.println("Faild to connect to database");
        return null;

    }
}

public void update(String update) {

    try {
        con().createStatement().executeUpdate(update);
        con().close();


    } catch (SQLException ex) {
        ex.printStackTrace();
    }

}
static ResultSet rs;

public ResultSet Query(String SQL) {

    try {
        rs = con().createStatement().executeQuery(SQL);
        con().close();

    } catch (SQLException ex) {
        Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "Unfortunatly an error occured and the database could not be reached");
    }
    return rs;
}

}

The error I am getting is this:

java.sql.SQLNonTransientConnectionException: User id length (0) is outside the range of 1 to 255. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Faild to connect to database at physioasist.Database.update(Database.java:49) at physioasist.ClientsScreen.btnUpdateActionPerformed(ClientsScreen.java:787) at physioasist.ClientsScreen.access$200(ClientsScreen.java:15) at physioasist.ClientsScreen$3.actionPerformed(ClientsScreen.java:272) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 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:6516) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6281) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4872) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747) at java.awt.EventQueue.access$300(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:706) at java.awt.EventQueue$3.run(EventQueue.java:704) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:720) at java.awt.EventQueue$4.run(EventQueue.java:718) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:717) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I'm not very skilled and this is a school project, I have looked online for answers but I cannot find similar problems.

4
  • Did you try the alternate call DriverManager.getConnection(url) ? Aug 12, 2016 at 16:43
  • If I just do the url like this --> Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/physioADB"); Then the error is this java.sql.SQLNonTransientConnectionException: Connection authentication failure occurred. Reason: userid or password invalid. Aug 12, 2016 at 17:06
  • 1
    So apparently you really do need a username and password. Although you didn't think you required one, you must have configured your database such that one is required: db.apache.org/derby/docs/10.12/security/cseccsecure42374.html Aug 13, 2016 at 1:10
  • I checked again, in netbeans I went to services and ran the database server, I then said create database, it asks for a name, username and password... I named it, but left username and password blank. I then click on the database and said connect, when prompted for username and password I left that blank, it connected fine.... But the code I am using still doesn't connect when no username or password is inputted... Aug 15, 2016 at 15:55

1 Answer 1

1

There is apparently a bug with using a blank username when creating the DB (one page I found said this is a MySQL bug, not just Apache/JavaDB). A couple options from what I've found:

  1. Fix the DB configuration, which is a persistence.xml file in the META-INF folder that you should find under your Project, under Source Packages in the "Projects" tab on the left in Netbeans. More information in this answer, where you need to set the blank username and password to the name of the schema in use (likely "APP" by default).
  2. You can start over from scratch, but this means everything, not just the database. I tried just rebuilding the DB and that didn't work for me - it still had info from the prior DB, I believe, in the project, that didn't get cleared out. I could tell because I named the new DB slightly differently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.