简体   繁体   中英

derby database connect without username and password

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.doIntersectionPri vilege(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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM