gpt4 book ai didi

java - 无需用户名和密码即可连接 derby 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:17 25 4
gpt4 key购买 nike

我在 Netbeans 中创建了一个 derby 数据库,并决定避免使用用户名和密码,这样我就不会冒忘记它的风险(这是我的错误)。

在花费了大量时间在 net beans 中创建项目、将数据库链接到各种表等之后,我无法获取下面的代码来连接到数据库。有没有一种方法可以使用代码进行连接而无需密码或用户名?或者我可以如何添加用户名和密码吗?

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;
}

}

我收到的错误是这样的:

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)

我不太熟练,这是一个学校项目,我在网上寻找答案,但找不到类似的问题。

最佳答案

创建数据库时使用空白用户名显然存在错误(我发现的一页说这是 MySQL 错误,而不仅仅是 Apache/JavaDB)。我发现的几个选项:

  1. 修复数据库配置,该配置是 META-INF 文件夹中的一个 persistence.xml 文件,您应该在 Netbeans 左侧“项目”选项卡的 Source Packages 下的项目下找到该文件。更多信息请参见this answer ,您需要将空白用户名和密码设置为正在使用的架构的名称(默认情况下可能是“APP”)。
  2. 您可以从头开始,但这意味着一切,而不仅仅是数据库。我尝试重建数据库,但这对我来说不起作用 - 它仍然有来自先前数据库的信息,我相信,在项目中,这些信息没有被清除。我可以这么说,因为我对新数据库的命名略有不同。

关于java - 无需用户名和密码即可连接 derby 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38920723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com