Hi all,
I'm having trouble opening a database connection to a test database on a
fresh PostGreSQL 7.1.3 installation on RedHat 7.1 from the RPMs. I have a
simple test class TestPostGreSQL that opens a connection, queries for all the
rows in a table and prints the results to standard output. The driver file
is in the same directory as the java class, which I execute using
java -classpath .:jdbc7.1-1.2.jar TestPostGreSQL
I get the following error message:
Something unusual has occured to cause the driver to fail. Please report this
exception: Exception: java.lang.NullPointerException
Stack Trace:
java.lang.NullPointerException
at org.postgresql.Connection.openConnection(Connection.java:148)
at org.postgresql.Driver.connect(Driver.java:122)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:177)
at TestPostGreSQL.main(TestPostGreSQL.java:17)
End of Stack Trace
I created the database using
createdb testdb
and from psql created the table as
create table Person (
FirstName char(30) not null,
LastName char(30) not null);
then created the user and password and granted all permissions on the Person
table.
I start the server with
postmaster -i
to get TCP/IP support. The code for the test class is at the end of this
message. Note that everything is commented out except for loading the driver
and opening a connection. I have used JDBC successfully for months with
Microsoft SQL Server, but I'm anxious to switch over to PostGreSQL!
Thanks in advance for any help!
Larry Rogers
*****************************
import java.sql.*;
import java.util.Date;
public class TestPostGreSQL {
public static void main(String[] args) {
final String driver = "org.postgresql.Driver";
final String url = "jdbc:postgresql://localhost:5432?database=testdb";
final String login = "TestUser";
final String password = "TestPass";
String sql = "SELECT * FROM Person";
try {
Class.forName(driver);
Connection connection
= DriverManager.getConnection(url, login, password);
/*
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
System.out.println(result.getString("FirstName") + " " +
result.getString("LastName"));
}
result.close();
statement.close();
*/
connection.close();
} catch(SQLException e) {
System.out.println(e);
} catch(ClassNotFoundException e) {
System.out.println(e);
}
}
}