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
Please tell if any further references are needed to solve the problem.
Thanks for you patience
You gave derby location of a database that doesn't exist, so it tries to create the database itself.
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.