0


Recently I have been trying a lot and scratching everywhere to connect to a derby database using NetBeans 8.0. After a lot of research in SO (link1, link2, link3, link4) and everywhere else I am unable to understand that how to make it go away.I realize this is the reason :-(the db.lck) that I am not getting a connection to the embedded apache derby DB.

I notice that db.lck appears while I compile the Java program. Is that a reason that I am denied to a embedded apache derby connection?

N.B :-
1. I connected to the JavaDB Server.
2. Disconnected the connection to the DB while compiling the program.
3. Added the relevant jar file derby.jar.
4. Created the database with default schema APP used username and password both as app.
5. Removed the create=true option from the connection string.
6. I can view and manipulate the data through the SQL console in NetBeans 8.0

Providing the code below for further reference :

test.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class test
{
public static void main(String args [])
{
    try { 
            Connection con = DriverManager.getConnection("jdbc:derby:C:/Users/Dhruvh/Documents/NetBeansProjects/ApacheDerby/derbytest","app","app");
            PreparedStatement stmt=con.prepareStatement("select * from \"APP\".TABLE1");
            ResultSet rs=stmt.executeQuery();
            if(rs.next())
            {
               System.out.println("Id : "+rs.getInt(1) +" "+" name :"+rs.getString(2));
            }
            else
            {
              System.out.println("No word matching in database");
            }
        } catch (SQLException err) {
           System.out.println(err.getMessage());
        }
    }
}

I have also tried using:-

PreparedStatement stmt=con.prepareStatement("select * from APP.TABLE1");
PreparedStatement stmt=con.prepareStatement("select * from TABLE1");
PreparedStatement stmt=con.prepareStatement("select * from app.table1"); //table was created in lower case

But everything I mention above gave the same error:-
Table/View 'APP.TABLE1' does not exist.

StackTrace:

java.sql.SQLSyntaxErrorException: Table/View 'APP.TABLE1' does not exist.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement42.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver42.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at test.main(test.java:12)

Caused by: ERROR 42X05: Table/View 'APP.TABLE1' does not exist. at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.impl.sql.compile.FromBaseTable.bindTableDescriptor(Unknown Source) at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source) at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source) at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source) at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source) at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source) at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source) at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source) at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source) at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)

Also proving a snapshot of the created DB
enter image description here

Please tell if any further references are needed to solve the problem.

Thanks for you patience

Community
  • 1
  • 1
mustangDC
  • 945
  • 1
  • 12
  • 33

1 Answers1

0

You gave derby location of a database that doesn't exist, so it tries to create the database itself.

jdbc:derby:C:/Users/Dhruvh/Documents/NetBeansProjects/ApacheDerby/src/derbytest;**create=true**

The bold part is a default argument which you leave out in your driver creation.

You can connect to the database but there doesn't exist a table you want to access. You have to fix the location of the database. Also if you are connected to the db through your program(looks like mplab x ?), you need to terminate that connection first.

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Marcin D
  • 918
  • 7
  • 14
  • no I mistakenly posted wit that location. The original location is `Connection con = DriverManager.getConnection("jdbc:derby:C:/Users/Dhruvh/Documents/NetBeansProjects/ApacheDerby/derbytest;create=true","app","app");` **Corrected in the question**, and I cross checked it now once agin the database folder is still there – mustangDC Jun 08 '15 at 16:45
  • Still the error exists and I am not using any aplication like mplab x – mustangDC Jun 08 '15 at 16:45
  • you are still connecting to two different databases. run this before accessing the table. CREATE TABLE APP.TABLE1 (ID int PRIMARY KEY NOT NULL); ALTER TABLE APP.TABLE1 ADD COLUMN NAME VARCHAR (30); – Marcin D Jun 08 '15 at 17:01
  • `CREATE TABLE APP.TABLE1 (ID int PRIMARY KEY NOT NULL);` gave `Error code 30000, SQL state X0Y32: Table/View 'TABLE1' already exists in Schema 'APP'.` – mustangDC Jun 08 '15 at 17:03
  • ` ALTER TABLE APP.TABLE1 ADD COLUMN NAME VARCHAR (30);` gave `Error code 30000, SQL state X0Y32: Column 'NAME' already exists in Table/View '"APP"."TABLE1"'.` – mustangDC Jun 08 '15 at 17:05
  • and now what do u get when you try select * from APP.TABLE1 – Marcin D Jun 08 '15 at 17:05
  • Thats what I am saying that the schema and table is there, but somewhere something is missing. I also mentioned the `db.lck` factor which appears while compiling the program – mustangDC Jun 08 '15 at 17:06
  • I wish I could attach the screenshot here but `select * from APP.TABLE1` showed all the rows with data as I mentioned on the screenshot above – mustangDC Jun 08 '15 at 17:08
  • i need you to run these statements in your java code where you get the error. as in stm =con.prepareStatement("CREATE TABLE APP.TABLE1 (ID int PRIMARY KEY NOT NULL)"); – Marcin D Jun 08 '15 at 17:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79978/discussion-between-mustangdc-and-marcin-deszczynski). – mustangDC Jun 08 '15 at 17:18