Обсуждение: JDBC Drivers for Java
I just installed the Windows version of PostgreSQL 7.3.1.
A jar file was included however the path was org/postgresql/Driver which causes and exception.
I expected somethig like org.postgresql.Driver.
If anyone can help me and send the exact code for making the connection it woud be appreciated.
Thanks!
Bob Lugering
Here is working example for you to try:
--In my databse
CREATE TABLE people (
id SERIAL,
name TEXT
);
INSERT INTO people(name) VALUES ('sue');
INSERT INTO people(name) VALUES ('peter');
INSERT INTO people(name) VALUES ('tony');
INSERT INTO people(name) VALUES ('mary');
--ListPeople.java
import java.sql.*;
public class ListPeople {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
//the url is
jdbc:postgresql://<host_name>:<port_number>/<database_name>?user=<user_name>&password=<password>
//in my case, the database is in localhost, and I need no passwords,
so using
//jdbc:postgresql:<database_name>?user=<user_name> will be just fine
Connection
con=DriverManager.getConnection("jdbc:postgresql:franco?user=admin");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM people");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
}
It works for me, so if doesn't work for you I'm sure the problem is in your
CLASSPATH.
On Friday 11 April 2003 20:47, Greg Lugering wrote:
> I just installed the Windows version of PostgreSQL 7.3.1.
> A jar file was included however the path was org/postgresql/Driver which
> causes and exception. I expected somethig like org.postgresql.Driver.
>
> If anyone can help me and send the exact code for making the connection it
> woud be appreciated.
>
> Thanks!
>
> Bob Lugering
> blugering@cfl.rr.com