• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to connect from Java to the Derby database  RSS feed

 
Igor Livshin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Eclipse 4.5.0 with Tomcat Server 8.03. I installed Derby database on the same machine where Eclipse and Tomcat reside. If in Eclipse, if I select the Database Development perspective I see in the Data Source Explorer that I am connected to the database (see Attachment 1).

However, a simple Java program is unable to connect., issues the exception below
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1,527 with message Connection refused: connect.
at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:579)


Here is the source
package com.derby.sample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSetMetaData;


public class School
{
private static String dbURL = "jdbc:derby://localhost:1527/C:\\DerbyDatabases;create=false;user=i262666;password=b122541b";

private static String tableName = "APP.SAMPLE";

// jdbc Connection
private static Connection conn = null;
private static Statement stmt = null;

public static void main(String[] args)
{
createConnection();
insertSchool(7, "Berkeley");
selectSchool();
shutdown();
}

private static void createConnection()
{
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
//Get a connection
conn = DriverManager.getConnection(dbURL);
}
catch (Exception except)
{
except.printStackTrace();
}
}

I even added Derby drivers to the Project's Build Path (see attachment 2)

Any help is appresiated.




 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Igor, welcome to the Ranch!

Connection refused: connect.


This error message almost always means that there is no server listening at the port you tried to connect to. In your case that would mean that there is no server running on your computer ("localhost") which is listening at port 1527. Perhaps your Derby server is listening at some other port? Or perhaps it isn't running at all? you should be able to find out by running netstat at the command line.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your connection string is incorrect.
Assuming your database is in that directory then it should be:
jdbc:derby:C:/DerbyDatabases/<your database name>
 
Igor Livshin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

You are right - my URL was coded incorrectly. I works now.
I was distracted, considering that there is a different reason for this problem. When you drop three derby plugin directories (making them the sub-directories of the Eclipse's "plugin" directory, the Java Nature should appear in the context menu for any project, and clicking on that Derby Nature adds it to the project. For some reason it does not work in my Eclipse, and I was considering as the root cause of this problem.

Thank you very much for your help.

By the way, googling Derby while researching this issue I found an interesting news. Oracle now made some changes to Derby and renamed it to Java DB. It became a small embedded database that is present in any JDK starting from JDK 7.x (no need to download and installed it). And with a small footprint it can be included in a jar file. It's size is not so small (could grow to 10 20 MB). Could be handy in some cases.

Thanks again.

Igor
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor Livshin wrote:. . . I works now.
. . . Derby . . . became a small embedded database that is present in any JDK starting from JDK 7.x . . .
Well done sorting it out and think you for that information, which some people here didn't know before.

And welcome to the ranch again
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!