pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Constantin Teodorescu <teo@flex.ro>
To: Robson Martins <robson@netalfa.com.br>
Cc: pgsql-interfaces <pgsql-interfaces@postgresql.org>
Subject: Re: [INTERFACES] How to access postgresql database using jdbc driver?
Date: Wed, 07 Jul 1999 15:29:41 +0000
Message-ID: <37837265.1E270F2B@flex.ro> (raw)
References: <004c01bec882$dfc536a0$1ed4d2c8@Designer2.netalfa.com.br>

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(); } }

view thread (3+ messages)

Message-ID: <37837265.1E270F2B@flex.ro>
Permalink:  ../37837265.1E270F2B@flex.ro/
Also on:    postgresql.org/message-id/37837265.1E270F2B@flex.ro

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: teo@flex.ro, robson@netalfa.com.br
  Subject: Re: [INTERFACES] How to access postgresql database using jdbc driver?
  In-Reply-To: <37837265.1E270F2B@flex.ro>

* 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