pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to access postgresql database using jdbc driver?
3+ messages / 3 participants
[nested] [flat]
* How to access postgresql database using jdbc driver?
@ 1999-07-07 14:13 Robson Martins <robson@netalfa.com.br>
1999-07-07 14:39 ` Re: [INTERFACES] How to access postgresql database using jdbc driver? Benoit Foucher <benoit@ooc.com>
1999-07-07 15:29 ` Re: [INTERFACES] How to access postgresql database using jdbc driver? Constantin Teodorescu <teo@flex.ro>
0 siblings, 2 replies; 3+ messages in thread
From: Robson Martins @ 1999-07-07 14:13 UTC (permalink / raw)
To: pgsql-interfaces
Hello all.
My name is Robson.
I'm starting on java language yet, and I'm having problems to access postgre sql database with jdbc.
I have PostgreSQL 6.5, JDK1.1.8, and a jdbc driver to JDK1.1.x
I'm using the comands:
Class.forName("postgresql.Driver"); -> to select the type of database
Connection db = DriverManager.getConnection("jdbc:postgresql://200.210.212.4:5432/imf", "username", "password"); -> to connect to the database
But i dont know how I check if worked, and I get a lot of errors.
Anybody can send me a example .java that show me how to load the driver, connect to the database, see if worked, create tables, and others, using postgres 6.5?
I didnt find on the web.
Please anybody help me with this!
I'm waiting any answer...
Robson ( robson@netalfa.com.br )
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: [INTERFACES] How to access postgresql database using jdbc driver?
1999-07-07 14:13 How to access postgresql database using jdbc driver? Robson Martins <robson@netalfa.com.br>
@ 1999-07-07 14:39 ` Benoit Foucher <benoit@ooc.com>
1 sibling, 0 replies; 3+ messages in thread
From: Benoit Foucher @ 1999-07-07 14:39 UTC (permalink / raw)
To: Robson Martins <robson@netalfa.com.br>; +Cc: pgsql-interfaces
Hi Robson,
What are the errors? I suggest you to read the JDBC section in
the java tutorial (on java.sun.com web site), there's also
code sample on:
http://developer.java.sun.com/developer/codesamples/database/
For postresql specific issue concerning the JDBC driver you can
have a look at the "Postgresql Programmer's Guide"
(doc/programmer.ps) and the example programs which are in the
pgsql/src/interfaces/jdbc/example directory...
Regards,
Benoit
> Robson Martins wrote:
>
> Hello all.
> My name is Robson.
> I'm starting on java language yet, and I'm having problems to access
> postgre sql database with jdbc.
> I have PostgreSQL 6.5, JDK1.1.8, and a jdbc driver to JDK1.1.x
> I'm using the comands:
> Class.forName("postgresql.Driver"); -> to select the type of database
> Connection db =
> DriverManager.getConnection("jdbc:postgresql://200.210.212.4:5432/imf",
> "username", "password"); -> to connect to the database
> But i dont know how I check if worked, and I get a lot of errors.
> Anybody can send me a example .java that show me how to load the
> driver, connect to the database, see if worked, create tables, and
> others, using postgres 6.5?
> I didnt find on the web.
> Please anybody help me with this!
> I'm waiting any answer...
> Robson ( robson@netalfa.com.br )
>
>
>
--
Benoit Foucher mailto:benoit@ooc.com
Object-Oriented Concepts, Inc. (trainee)
44 Manning Road
Billerica, MA 01821
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: [INTERFACES] How to access postgresql database using jdbc driver?
1999-07-07 14:13 How to access postgresql database using jdbc driver? Robson Martins <robson@netalfa.com.br>
@ 1999-07-07 15:29 ` Constantin Teodorescu <teo@flex.ro>
1 sibling, 0 replies; 3+ messages in thread
From: Constantin Teodorescu @ 1999-07-07 15:29 UTC (permalink / raw)
To: Robson Martins <robson@netalfa.com.br>; +Cc: pgsql-interfaces
I'm attaching here a simple Java program that will open, create a table
and query the table printing the results!
yourdb database must be previously created!
Best regards,
--
Constantin Teodorescu
FLEX Consulting Braila, ROMANIA
Attachments:
tutorial.java (text/plain, 1.5K) download
import java.io.*;
import java.sql.*;
public class tutorial {
private Connection dc;
private Statement st;
public tutorial() {
try {
Class.forName("postgresql.Driver");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Cannot load PostgreSQL driver\n"+e.getMessage());
System.exit(-1);
}
}
public boolean openDatabase() {
boolean result;
try {
dc = DriverManager.getConnection("jdbc:postgresql://localhost/yourdb","yourname","yourpassword");
st = dc.createStatement();
result = true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Cannot connect to database\nError:"+e.getMessage());
result = false;
}
return result;
}
public void run() {
System.out.println("Starting work!");
try {
st.executeUpdate("create table people (id int4, name varchar(32))");
st.executeUpdate("insert into people values(1,'teo')");
st.executeUpdate("insert into people values(2,'peter')");
ResultSet rs = st.executeQuery("select id,name from people");
if ( rs != null) {
while ( rs.next() ) {
System.out.println(rs.getString("name")+" has id= "+rs.getString("id"));
}
rs.close();
}
st.executeUpdate("drop table people");
} catch (SQLException sqle) {
System.out.println("A SQL exception occured : "+sqle.getMessage());
}
System.out.println("Finished!");
}
public static void main(String argv[]) {
tutorial task = new tutorial();
if ( task.openDatabase() ) task.run();
}
}
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~1999-07-07 15:29 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-07-07 14:13 How to access postgresql database using jdbc driver? Robson Martins <robson@netalfa.com.br>
1999-07-07 14:39 ` Benoit Foucher <benoit@ooc.com>
1999-07-07 15:29 ` Constantin Teodorescu <teo@flex.ro>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox