pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <heikki@enterprisedb.com>
To: Andreas <maps.on@gmx.net>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Hello World
Date: Tue, 21 Nov 2006 09:45:53 +0000
Message-ID: <4562CAD1.5090603@enterprisedb.com> (raw)
In-Reply-To: <456295E2.3010209@gmx.net>
References: <456295E2.3010209@gmx.net>

Andreas wrote:
> Hi,
> 
> first of all I'll have to confess I don't know next to nothing at all 
> about JAVA, yet.
> On the other hand I'm not exactly new to programming nor PostgreSQL either.
> 
> Is there a "Hello World" level sample for jdbc with PG?

I'd suggest to read the Java Tutorial by Sun:

http://java.sun.com/docs/books/tutorial/

and after you got the basics, move on to the JDBC trail of the tutorial:

http://java.sun.com/docs/books/tutorial/jdbc/index.html

> Lets suppose I allready have a running PG with a db db_test that has a 
> few tables among which there is tbl_test in schema s.
> The db-server might be 192.168.0.100.
> User could be "user1" with password "pw1".
> 
> What would be minimally needed to connect to db_test and read the 
> content of tbl_test to the console and logg off from PG.
> 
> I'd prefer a complete sample code that could be dumped through javac.

A sample attached. Save it to a file called JdbcTest.java, modify the 
server address, database name, table name etc. to suite your needs, and 
compile with "javac JdbcTest.java". To run, add postgresql.jar to your 
classpath. For example:

java -cp path/to/postgresql.jar JdbcTest

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com

Attachments:

JdbcTest.java (text/x-java, 395B) download import java.sql.*; public class JdbcTest { public static void main(String[] args) throws Exception { Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost/testdb", "username", "pw"); ResultSet rs = conn.prepareStatement("SELECT * FROM test").executeQuery(); while(rs.next()) System.out.println(rs.getString(1)); } }

view thread (6+ messages)  latest in thread

Message-ID: <4562CAD1.5090603@enterprisedb.com>
Permalink:  ../4562CAD1.5090603@enterprisedb.com/
Also on:    postgresql.org/message-id/4562CAD1.5090603@enterprisedb.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: heikki@enterprisedb.com, maps.on@gmx.net
  Subject: Re: Hello World
  In-Reply-To: <4562CAD1.5090603@enterprisedb.com>
  References: <456295E2.3010209@gmx.net> <4562CAD1.5090603@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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