java - for - Error de la base de datos no encontrada en Apache Derby
java derby driver download (2)
Primero, esta es mi primera vez con Apache Derby. Estoy usando netbeans, estoy dispuesto a utilizar Apache derby incorporado, y seguí el siguiente tutorial para configurar e instalar la base de datos
http://netbeans.org/kb/docs/ide/java-db.html#starting
La imagen adjunta mostrará el estado de mi base de datos en netbeans
Mi nombre de base de datos es "contacto". El nombre de la tabla es "AMIGOS".
El siguiente es mi código de prueba
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();
}
}
Pero, cuando intento insertar datos en la base de datos, me está dando el siguiente 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)
Por favor, ayúdame. Gracias
La URL de conexión "jdbc: derby: contact" especifica que la base de datos de "contacto" se debe encontrar en el directorio de trabajo actual de su proceso, pero probablemente no esté donde se encuentra. Cuando se ejecuta su programa Java, el directorio de trabajo actual probablemente se haya configurado en otro lugar. Puede confirmar esto imprimiendo el directorio de trabajo actual al comienzo de su programa.
Para evitar esto por el momento, puede especificar la ruta completa a su base de datos: "jdbc: derby: / ruta / a / mi / db / contacto".
Si eso te lleva más lejos, puedes continuar, pero en algún momento tendrás que pensar más sobre dónde quieres que esté tu base de datos permanentemente y cómo deseas especificar eso en tu programa.
Mire amablemente su conexión derby / jdbc, esta línea aquí:
con = DriverManager.getConnection("jdbc:derby:contact","yohan","xyz");
Verifica si se ve similar a:
"jdbc:derby://localhost:1527/contact"
O, como he visto, el nombre de su base de datos es ContactDB ... Por favor, compruebe ...
Gracias...