我在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取值范围为1 ~ 255。线程"AWT-EventQueue-0"中的异常连接数据库失败physioasist.Database.update (Database.java: 49)physioasist.ClientsScreen.btnUpdateActionPerformed (ClientsScreen.java: 787)$200(ClientsScreen.java:15physioasist.ClientsScreen 3.美元actionperformed (ClientsScreen.java: 272)在javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java: 2018)在javax.swing.AbstractButton Handler.actionPerformed美元(AbstractButton.java: 2341)在javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java: 402)在javax.swing.DefaultButtonModel.setPressed (DefaultButtonModel.java: 259)在javax.swing.plaf.basic.BasicButtonListener.mouseReleased (BasicButtonListener.java: 252)在java.awt.Component.processMouseEvent(Component.java:6516javax.swing.JComponent.processMouseEvent (JComponent.java: 3321)java.awt.Component.processEvent (Component.java: 6281)java.awt.Container.processEvent (Container.java: 2229)java.awt.Component.dispatchEventImpl (Component.java: 4872)java.awt.Container.dispatchEventImpl (Container.java: 2287)java.awt.Component.dispatchEvent (Component.java: 4698)java.awt.LightweightDispatcher.retargetMouseEvent (Container.java: 4832)在java.awt.LightweightDispatcher.processMouseEvent (Container.java: 4492)java.awt.LightweightDispatcher.dispatchEvent (Container.java: 4422)在java.awt.Container.dispatchEventImpl(Container.java:2273java.awt.Window.dispatchEventImpl (Window.java: 2719)java.awt.Component.dispatchEvent (Component.java: 4698)java.awt.EventQueue.dispatchEventImpl (EventQueue.java: 747)java.awt.EventQueue.access (EventQueue.java: 103) 300美元java.awt.EventQueue 3.美元(EventQueue.java: 706)运行java.awt.EventQueue 3.美元(EventQueue.java: 704)运行java.security.AccessController。特权(本地方法)在java.security.ProtectionDomain dointersectionprivilege 1.美元(ProtectionDomain.java: 76)在java.security.ProtectionDomain dointersectionprivilege 1.美元(ProtectionDomain.java: 87)java.awt.EventQueue$4.run(EventQueue.java:720java.awt.EventQueue 4.美元(EventQueue.java: 718)运行java.security.AccessController。特权(本地方法)在java.security.ProtectionDomain dointersectionprivilege 1.美元(ProtectionDomain.java: 76)在java.awt.EventQueue.dispatchEvent(EventQueue.java:717java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java: 242)在java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java: 161)在java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java: 150)在java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java: 146)在java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java: 138)java.awt.EventDispatchThread.run (EventDispatchThread.java: 91)
我不是很熟练,这是一个学校的项目,我在网上寻找答案,但我没有找到类似的问题。
在创建DB时使用空白用户名显然有一个错误(我发现的一个页面说这是MySQL的错误,而不仅仅是Apache/JavaDB)。从我找到的几个选项:
- 修复DB配置,这是
META-INF
文件夹中的persistence.xml
文件,您应该在项目下找到,在Netbeans左侧的"Projects"选项卡中的Source Packages
下。在这个答案中有更多的信息,你需要将空白的用户名和密码设置为正在使用的模式的名称(默认情况下可能是"APP")。 - 你可以从头开始,但这意味着一切,而不仅仅是数据库。我试着重建数据库,但这对我不起作用-它仍然有以前的数据库的信息,我相信,在项目中,没有被清除。我可以看出,因为我对新DB的命名略有不同。