简体   繁体   English

Apache Derby中找不到数据库错误

[英]Database not found error in Apache Derby

First, this is my first time with Apache Derby. 首先,这是我第一次使用Apache Derby。 I am using netbeans, willing to use embedded apache derby, and I followed the following tutorial for configuring and installing the database 我正在使用netbeans,愿意使用嵌入式apache derby,我按照以下教程配置和安装数据库

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

The attached image will show my database status in netbeans 附加的图像将在netbeans中显示我的数据库状态 在此输入图像描述

My database name is "contact". 我的数据库名称是“contact”。 Table name is "FRIENDS". 表名是“FRIENDS”。

Following is my test code 以下是我的测试代码

DatabaseConnector.java 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 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();
    }
}

But, when I am trying to insert data into the database, it is giving me the following error 但是,当我试图将数据插入数据库时​​,它给了我以下错误

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)

Please help me. 请帮我。 Thanks 谢谢

The connection URL "jdbc:derby:contact" specifies that the 'contact' database should be found in the current working directory of your process, but that's probably not where it's actually located. 连接URL“jdbc:derby:contact”指定应在进程的当前工作目录中找到“联系人”数据库​​,但这可能不是它实际所在的位置。 When your Java program is run, the current working directory is probably been set somewhere else. 运行Java程序时,当前工作目录可能已设置在其他位置。 You can confirm this by printing out the current working directory at the start of your program. 您可以通过在程序开头打印出当前工作目录来确认这一点。

To get around this for now, you can specify the full path to your database: "jdbc:derby:/path/to/my/db/contact". 要暂时解决这个问题,您可以指定数据库的完整路径:“jdbc:derby:/ path / to / my / db / contact”。

If that gets you farther, then you can keep going, but at some point you'll have to think more about where you want your database to be permanently located and how you want to specify that in your program. 如果这让你走得更远,那么你可以继续前进,但在某些时候你必须更多地考虑你希望你的数据库永久定位在哪里以及你想如何在你的程序中指定它。

Kindly look at your derby/jdbc connection, This line here: 请看看你的derby / jdbc连接,这一行在这里:

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

Check if it looks similar to: 检查它是否类似于:

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

Or, as what I've seen, the name of your database is ContactDB... Kindly check... 或者,正如我所看到的,您的数据库的名称是ContactDB ...请检查...

Thanks... 谢谢...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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