How to add the Postgre driver to the classpath [message #775302] |
Thu, 05 January 2012 18:39 |
Jonathan Camilleri Messages: 91 Registered: July 2009 |
Member |
|
|
My program attempts to establish a connection with the local database (test), however, I am getting an error that seems to indicate that the JAR file is not added to the CLASSPATH.
I have included the driver within the project, and, attempted to add the JAR file within the classpath through the GUI, however, when running the project I am getting an error.
The archive: /EmployeeSystem/postgresql-9.1-901.jdbc4.jar which is referenced by the classpath, does not exist (see error.GIF).
package firstApp;
import firstApp.Employee.PerformanceRank;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class EmployeeTest {
/**
* @param args
*/
public static void main(String[] args)
{
final String Currency = "€";
Employee first = new Employee("Jon C.", 200000.0d, 2011, 1, 1);
first.setPerformance(PerformanceRank.LOW);
System.out.println(first.getName());
System.out.println();
System.out.println("Performance: " + first.getPerformance());
first.increaseSalary(10);
System.out.print(Currency);
System.out.printf("%,.2f", first.getSalary()); //display number with commas to separate three digits of decimal figure, . as the decimal point and up to 2 precision numbers after the decimal point.
System.out.print(" yearly.");
System.out.println();
System.out.println(first.hashCode());
Employee second = new Employee ("Jon D", 20000.0d, 2011, 1,1);
System.out.println(second.hashCode());
if (first.equals(first)) {System.out.println(" Reflexive: first employee is equal to itself");}
if (first.equals(second)) {System.out.println(" Symmetric: first employee is equal to the second object");}
if (second.equals(first)) {System.out.println(" Transitive: second employee is equal to the first object");}
if (first == second) {System.out.println("it is not valid to compare objects using ==");}
System.out.println("PostGre SQL JDBC Connection Testing");
try
{
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
return;
}
System.out.println("PostgreSQL Driver registered");
Connection connection = null;
try
{
String url = "jdbc:postgresql://127.0.0.1:5432/EmployeeSystem";
String user = "jon";
String pwd = "j1misdead";
connection = DriverManager.getConnection(url, user, pwd);
}
catch (SQLException e)
{
System.out.println("Connection failed. Check output console");
e.printStackTrace();
return;
}
if (connection != null)
{
System.out.println("Connection successful");
}
else
{
System.out.println("Connection failed");
}
/* Sourced from http://www.mkyong.com/jdbc/how-do-connect-to-postgresql-with-jdbc-driver-java/ */
}
}
References
1. Core Java Volume 2 (7th Edition)
[Updated on: Thu, 05 January 2012 18:39] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.09203 seconds