Received: from flex.flex.ro ([193.230.255.4]) by hub.org (8.9.3/8.9.3) with ESMTP id LAA45453 for ; Wed, 7 Jul 1999 11:23:50 -0400 (EDT) (envelope-from teo@flex.ro) Received: from flex.ro (IDENT:teo@teo.flex.ro [193.230.255.3]) by flex.flex.ro (8.9.1/8.9.1) with ESMTP id RAA25026; Wed, 7 Jul 1999 17:23:20 +0300 Sender: teo@flex.flex.ro Message-ID: <37837265.1E270F2B@flex.ro> Date: Wed, 07 Jul 1999 15:29:41 +0000 From: Constantin Teodorescu Organization: FLEX Consulting X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.9 i586) X-Accept-Language: en MIME-Version: 1.0 To: Robson Martins CC: pgsql-interfaces Subject: Re: [INTERFACES] How to access postgresql database using jdbc driver? References: <004c01bec882$dfc536a0$1ed4d2c8@Designer2.netalfa.com.br> Content-Type: multipart/mixed; boundary="------------F5AE6FF14FB5570D240AF39A" This is a multi-part message in MIME format. --------------F5AE6FF14FB5570D240AF39A Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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 --------------F5AE6FF14FB5570D240AF39A Content-Type: text/plain; charset=us-ascii; name="tutorial.java" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="tutorial.java" 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(); } } --------------F5AE6FF14FB5570D240AF39A--