Apache Derby - java.sql.SQLException:无法启动数据库 [英] Apache Derby - java.sql.SQLException: Failed to start database

查看:1589
本文介绍了Apache Derby - java.sql.SQLException:无法启动数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是我第一次使用Apache Derby。我正在使用netbeans,愿意使用嵌入式apache derby,我按照以下教程配置和安装数据库。然后,我使用项目属性将derby.jar文件附加到我的项目。

First, this is my first time with Apache Derby. I am using netbeans, willing to use embedded apache derby, and I followed the following tutorial for configuring and installing the database. Then, I attached the derby.jar file to my project, using project properties.

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

附加图像将在netbeans中显示我的数据库状态

The attached image will show my database status in netbeans

我的数据库名称是contact。表名是FRIENDS。

My database name is "contact". Table name is "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:C:/Users/yohan/.netbeans-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();
    }
}

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

But, when I am trying to insert data into the database, it is giving me the following error

run:
java.sql.SQLException: Failed to start database 'C:/Users/yohan/.netbeans-derby/contact' with class loader sun.misc.Launcher$AppClassLoader@1050169, see the next exception for details.
    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.seeNextException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(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:53)
    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: Failed to start database 'C:/Users/yohan/.netbeans-derby/contact' with class loader sun.misc.Launcher$AppClassLoader@1050169, see the next exception for details.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 51 more
Caused by: java.sql.SQLException: Another instance of Derby may have already booted the database C:\Users\yohan\.netbeans-derby\contact.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    ... 48 more
Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database C:\Users\yohan\.netbeans-derby\contact.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.privGetJBMSLockOnDB(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.getJBMSLockOnDB(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.store.raw.RawStore.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.store.access.RAMAccessManager.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.db.BasicDatabase.bootStore(Unknown Source)
    at org.apache.derby.impl.db.BasicDatabase.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.bootService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startProviderService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.findProviderAndStartService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startPersistentService(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.startPersistentService(Unknown Source)
    ... 48 more
java.lang.NullPointerException
    at DataBaseConnector.insertData(DataBaseConnector.java:43)
    at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:53)
    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:53)
    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)
BUILD SUCCESSFUL (total time: 12 seconds)

这是为什么?请帮帮忙!

Why is this? Please help!

除了第一个主要问题外,我还想问另外两个问题。

Apart from that first main question, I would like to ask another 2 questions.


  1. 我必须在此处提供数据库的显式位置作为连接。但是,当我把它交给客户时,我该怎么做?

  2. 我必须使用netbeans手动启动数据库连接。反正有没有自动启动数据库连接?使用代码?

请帮助我。谢谢

推荐答案

您的程序包含一些误解和错误的作业!


  • 从你的图片中你可以看到你的DATABASE = contactDB没有联系。

    jdbc:derby:C:/ Users / yohan /。 netbeans-derby / contact是错误


    jdbc:derby:C:/Users/yohan/.netbeans-derby/contactDB现在只有数据库是正确的。

    错误:另一个Derby实例可能已经启动了数据库C:\ Users \yohan.netbeans-derby \ contact。

  • From your image you can see your DATABASE = contactDB not contact.
    "jdbc:derby:C:/Users/yohan/.netbeans-derby/contact" is wrong
    "jdbc:derby:C:/Users/yohan/.netbeans-derby/contactDB" now only the DB is correct.
    Error: Another instance of Derby may have already booted the database C:\Users\yohan.netbeans-derby\contact.

对于Con.String,最好不要使用PATH

将起作用jdbc:derby:// localhost:1527 / C:/用户/ yohan / .netbeans-derby / contactDB,yohan,xyz

更好jdbc:derby:// localhost:1527 / contactDB, yohan,xyz

For the Con.String it's better not to use the PATH
will work "jdbc:derby://localhost:1527/C:/Users/yohan/.netbeans-derby/contactDB","yohan","xyz"
better is "jdbc:derby://localhost:1527/contactDB","yohan","xyz"

最好指定表格的完整路径。否则它仅在Netbeans中工作,具有已打开的表和默认架构。

It is better to specify the full Path to the table. Otherwise it is working only in Netbeans, with an already open table and a default schema.


  • prepareStatement

    only 内部Netbeans con.prepareStatement(将插入FRIENDS 值(?,?,?));

    更好是con.prepareStatement(将插入APP.FRIENDS 值(?,?,?));

  • prepareStatement
    only inside Netbeans con.prepareStatement("insert into FRIENDS values(?,?,?)");
    better is con.prepareStatement("insert into APP.FRIENDS values(?,?,?)");

现在看看这个!

    ps.setInt(1, id);
    ps.setString(1, firstName);
    ps.setString(2, lastName);




  • 首先用setInt设置 id ()。

  • 秒现在您使用字符串 setString()(firstName !!!!!)
  • $设置 id b $ b
  • 最后用你的lastName覆盖firstName,用ps覆盖lastName。是空的。

    • First you set the id with setInt().
    • Second Now you set the id with a string setString() ( firstName !!!!!)
    • Third at the end you override firstName with lastName and the lastName in the ps. is empty.
    • 这将为您提供下一个错误

      This will give you the next Error

      它是更好的是,当发生错误时,不要进一步运行程序。错误列表会长大。这很难读。在你的情况下,第一个错误是在 createConnection 并运行!!

      It is better , when an error occurs, not to run the program further. The error list will grow up. It's hard to read. in your case the first Error is in createConnection and runs on !!


      • 错误createConnection

      • 错误con.prepareStatement(...

      • 错误ps.executeUpdate()

      • 错误closeConnection()

      • Error createConnection
      • Error con.prepareStatement("...
      • Error ps.executeUpdate()
      • Error closeConnection()

      测试它用布尔如果

      private boolean createConnection()
      {
          try
          {
              Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
              con = DriverManager.getConnection("jdbc:derby://localhost:1527/contactDB","yohan","xyz");
          }
          catch(Exception e)
          {   
              System.out.println("Error getConnection");
              return false;
          }
       return true;   
      }
      

      如果连接失败,则无需继续运行。

      public void insertData(int id, String firstName, String lastName)
          {
             if (createConnection()) {
              try
              {
                  PreparedStatement ps = con.prepareStatement("INSERT INTO APP.FRIENDS values(?,?,?)");
                  ps.setInt(1, id);
                  ps.setString(2, firstName);
                  ps.setString(3, lastName);
      [...]
      

      我希望这对你有所帮助。

      I hope this helps you a bit.

      这篇关于Apache Derby - java.sql.SQLException:无法启动数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆