• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can Connect to PostgreSQL Database Using NetBeans but Not Using Eclipse

 
David Ellis Rogers
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to both Eclipse and the Mac, have a MacBook Pro with OS X 10.10 Yosemite. Had been developing a project using Java and PostgreSQL on a Dell Win 7 with NetBeans. Due to several reasons I decided to bite the bullet and move to a Mac 1) to make some changes and additions to the present project and 2) to develop a related project under Eclipse. (Other folks who might further develop these project are working with Eclipse; thus, I felt I should work in only one SDK - Eclipse to make things simpler for myself and others in the future.)

I installed NetBeans 8.0.2 SDK on the Mac. The identical program developed under NetBeans works both on the Win 7 and on the Mac.

However, when I moved the code for the present project into Eclipse (Luna 4.4.1) there are several errors, indicated in my bracketed comments below. The problem I am requesting help on is connection to the PostgreSQL database via Eclipse, which is properly installed on the Mac. Connectivity is successful under NetBeans on the Mac but not under Eclipse on the Mac.


The following sections of code are in BOTH NetBeans and Eclipse versions.

public static void sign_on() {
Class driverClass = loadDriver("org.postgresql.Driver");
if (driverClass == null) {
return;
}
System.out.println("Connection successful”);
con = connectURL(“jdbc:postgresql://localhost/pg1", "postgres", "ginkat8a");
if (Globals.con != null) {
try {
DatabaseMetaData dmd = Globals.con.getMetaData();
System.out.print("Connected to ");
System.out.print(dmd.getDatabaseProductName());
String dbversion = dmd.getDatabaseProductVersion();
System.out.println(", version " + dbversion);
System.out.println("driver: " + dmd.getDriverName()
+ ", version " + dmd.getDriverVersion());
} catch (SQLException e) {
System.err.println("Error 3: "
+ "DatbaseMetaData failed(SQLExcception: "
+ e.getMessage());
JOptionPane.showMessageDialog(null,
"DatbaseMetaData failed(SQLExcception: "
+ e.getMessage(), //BODY OF MESSAGE
"Error 3: cannot obtain metadata about database",
// ABOVE IS BANNER
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
} // END OF if (Globals.con != null)

public static Class loadDriver(String driverName) {
try {
return (Class.forName(driverName));
} catch (ClassNotFoundException e) {
System.err.println("Error 1: Can't load driver 1: "
+ e.getMessage());
JOptionPane.showMessageDialog(null,
"ClassNotFoundException " + e.getMessage(),
"Error 1: Can't load driver",//BANNER
JOptionPane.ERROR_MESSAGE);
return (null);
} catch (Exception e)
{
System.err.println("Error 2: Can't load driver 2: "
+ e.toString());
JOptionPane.showMessageDialog(null,
"Error 2: Can't load driver", //BODY OF MESSAGE
"ClassNotFoundException " + e.getMessage(),//BANNER
JOptionPane.ERROR_MESSAGE);
return (null);
// System.exit(1);
}

[Output of Eclipse version:]
Point 1 at beginning in Main of MainProgram.java
Point 3A after initComponents AND BEFORE addActionListeners
Point 3 after initComponents
]Error 1: Can't load driver 1: org.postgresql.Driver
Point 4 Am in MainProgram after return from Globals.sign_on()

[Output of NetBeans version:]
Point 1 at beginning in Main of MainProgram.java
Point 3A after initComponents AND BEFORE addActionListeners
Point 3 after initComponents
Connection successful
have just set up con in globals.java
Connected to PostgreSQL, version 9.4.1
driver: PostgreSQL Native Driver, version PostgreSQL 9.2 JDBC4.1 (build 1004)
Point 4 Am in MainProgram after return from Globals.sign_on()

[ and then it goes on to successfully retrieve rows from the PostgreSQL database.]
I think it is a JDBC problem, as I can't figure out the PostgreSQL instructions.
I do not know how to set up the CLASSPATH on the Mac. (Am having trouble converting myself over to the Mac - too many years of Windows.) Do not know where to put the CLASSPATH. I downloaded the following:
postgresql-jdbc-9.4-1200.src.tar
postgresql-9.4-1200.jdbc41.jar
postgresql-9.4-1200.jdbc4.jar
postgresql-9.2-1004.jdbc41.jar
postgresql-9.2-1004.jdbc41.jar

Help would be greatly appreciated.
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

If you're going to post code, can you please UseCodeTags ? It makes it so much easier to read, and also doesn't lose your formatting.

>Error 1: Can't load driver 1: org.postgresql.Driver
>I do not know how to set up the CLASSPATH on the Mac.

Looks like you have successfully diagnosed the issue yourself.
The database driver is not available for the java class at runtime, so it throws that error.

Seeing as you are in Eclipse, the easiest solution would be to add that database driver jar file to the project's build path.
You can find it in project properties "Java Build Path" - the "Libraries" tab.
Alternatively, right click the project and select: Build Path --> Add External Libraries.





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic