当前位置: 首页 > 知识库问答 >
问题:

Apache Derby中找不到数据库错误

薄兴昌
2023-03-14

首先,这是我第一次使用Apache Derby。我正在使用netbeans,愿意使用嵌入式apache derby,并且我遵循了以下配置和安装数据库的教程

http://netbeans.org/kb/docs/ide/java-db.html#开始

我的数据库名称是“联系人”。表名为“Friends”。

下面是我的测试代码

DatabaseConnector.java

import java.sql.*;

public class DataBaseConnector
{
    private Connection con;

    public DataBaseConnector()
    {

    }

    private void createConnection()
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            con = DriverManager.getConnection("jdbc:derby:contact","yohan","xyz");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private void closeConnection()
    {
        try
        {
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


    public void insertData(int id, String firstName, String lastName)
    {
        createConnection();
        try
        {
            PreparedStatement ps = con.prepareStatement("insert into FRIENDS values(?,?,?)");
            ps.setInt(1, id);
            ps.setString(1, firstName);
            ps.setString(2, lastName);

            int result = ps.executeUpdate();

            if(result>0)
            {
                System.out.println("Data Inserted");
            }
            else
            {
                System.out.println("Something happened");
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            closeConnection();
        }
    }


}

DatabaseUI.java

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class DatabaseUI extends JFrame
{
    private JLabel firstName, id, lastName;
    private JTextField idTxt, firstNameTxt, lastNameTxt;
    private JButton ok;

    public DatabaseUI()
    {
     firstName = new JLabel("First Name: ");
     lastName = new JLabel("Last Name: ");
     id = new JLabel("ID: ");

     firstNameTxt = new JTextField(10);
     lastNameTxt = new JTextField(10);
     idTxt = new JTextField(10);

     ok = new JButton("OK");
     ok.addActionListener(new OKAction());

     JPanel centerPanel = new JPanel();
     centerPanel.setLayout(new GridLayout(4,2));
     centerPanel.add(id);
     centerPanel.add(idTxt);
     centerPanel.add(firstName);
     centerPanel.add(firstNameTxt);
     centerPanel.add(lastName);
     centerPanel.add(lastNameTxt);
     centerPanel.add(new JPanel());
     centerPanel.add(ok);

     getContentPane().add(centerPanel,"Center");


     this.pack();
     this.setVisible(true);


    }

    private class OKAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            DataBaseConnector db = new DataBaseConnector();

            int id = Integer.parseInt(idTxt.getText());

            db.insertData(id, firstNameTxt.getText().trim(), lastNameTxt.getText().trim());
        }
    }

    public static void main(String[]args)
    {
        new DatabaseUI();
    }
}

但是,当我试图将数据插入数据库时,它会给我以下错误

run:
java.sql.SQLException: Database 'contact' not found.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleDBNotFound(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)
    at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
    at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at DataBaseConnector.createConnection(DataBaseConnector.java:17)
    at DataBaseConnector.insertData(DataBaseConnector.java:40)
    at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:52)
    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:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    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:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    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:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.sql.SQLException: Database 'contactDB' not found.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 53 more
java.lang.NullPointerException
    at DataBaseConnector.insertData(DataBaseConnector.java:43)
    at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:52)
    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:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    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:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    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:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
java.lang.NullPointerException
    at DataBaseConnector.closeConnection(DataBaseConnector.java:29)
    at DataBaseConnector.insertData(DataBaseConnector.java:65)
    at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:52)
    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:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    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:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    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:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

请帮帮我.谢谢

共有2个答案

金阳华
2023-03-14

请看一下您的Derby/JDBC连接,下面这行:

con = DriverManager.getConnection("jdbc:derby:contact","yohan","xyz");

检查其外观是否类似于:

"jdbc:derby://localhost:1527/contact"

或者,正如我所看到的,您的数据库的名称是ContactDB...请查一下...

谢谢...

盛承
2023-03-14

连接URL“jdbc:derby:contact”指定“contact”数据库应该在进程的当前工作目录中找到,但它可能不在当前工作目录中。当您的Java程序运行时,当前的工作目录可能被设置在其他地方。您可以通过在程序开始时打印出当前的工作目录来确认这一点。

现在要解决这个问题,您可以指定数据库的完整路径:“jdbc:derby:/path/To/my/db/contact”。

如果这让您走得更远,那么您可以继续前进,但在某个时候,您将不得不更多地考虑您希望数据库永久位于何处,以及您希望如何在程序中指定该位置。

 类似资料:
  • 我读到的几乎所有答案都说我的数据库名或pw是错的,但我100%确定不是。以下是http错误描述:javax . servlet . servlet异常:javax . servlet . JSP . JSP异常:无法获取连接,数据源无效:“java.sql.SQLException:拒绝用户' root'@'localhost '(使用密码:YES)的访问” 如果有人能帮助我,我会很棒。仍然是编程

  • 我对冬眠很陌生,所以我的问题对你来说很明显。我在H2控制台用3个表(data.mv.db)创建了数据库,插入一些值,然后将其复制到项目路径中的数据库文件夹中。现在我试图阅读其中一个测试,但总是有错误,即表不存在。我试着给连接url添加一些属性,比如连接延迟或者不把字母改成大写,但仍然是一样的。 错误: 我试过编辑配置。所以我补充: 并使数据库为空。奇怪的是,在H2控制台打开后,Java创建的数据库

  • 问题内容: 当我尝试编译它使用OpenSSL的代码“密码”的库函数与COMAND线与它给出了一个错误 这可能是什么原因? 找到命令结果 有人可以帮忙吗,或者指出我正在做的任何错误 @ Daniel Roethlisberger尝试使用-L标志,但是导致这些错误 非常感谢 问题答案: 在之前,将其添加到GCC命令行中。由于您是根据下的OpenSSL标头构建的,因此,您还需要链接到具有相同前缀的实际库

  • 其他人有这个奇怪的问题吗?错误消息: 致命错误:未捕获异常“exception”,消息为“DateTime::\uuu construct():无法分析位置17(A)处的时间字符串(2016年1月18日美国/纽约凌晨00:00):在数据库中找不到时区 异常:DateTime::_构造():未能分析位置17(A)处的时间字符串(2016年1月18日美国/纽约凌晨00:00):在数据库中找不到时区 原

  • 问题内容: 我收到这样的错误消息: Windows无法加载库 名称 库.dll。确保该库位于您的Path环境变量中。线程“主”中的异常java.lang.UnsatifiedLinkError:java.library.path中没有 库名 。 这是我尝试通过cmd在Windows XP上运行jar文件的错误。我想知道java.library.path到底在哪里?我已经将C:\ Program F

  • 我使用CAS和Spnego支持,KDC是192.168.1.244,我的领域是示例。COM我测试了我的本地Windows域环境,我可以从示例中获得票证。com我用“kinit”命令测试它,但在CAS spnego环境中,在Kerberos数据库中找不到异常客户端,我已经在C:\windows\krb5.ini中创建了它,内容如下 krb5.ini 异常报告如下: 但为什么在kerberos数据库中