RES: [JDBC] Java access pgsql sample

From: Elielson Fontanezi <ElielsonF(at)prodam(dot)sp(dot)gov(dot)br>
To: 'Ribamar FS' <ribafs(at)myrealbox(dot)com>
Cc: pgsql-jdbc <pgsql-jdbc(at)postgresql(dot)org>, pgsql-general <pgsql-general(at)postgresql(dot)org>, Alexandre Seiji Matsushita <amatsushita(at)prodam(dot)sp(dot)gov(dot)br>, Claudia Lacerda Alves de Almeida <claalmeida(at)prodam(dot)sp(dot)gov(dot)br>, Francisco Luis dos Santos <fsantos(at)prodam(dot)sp(dot)gov(dot)br>, Marcio Pacheco de Jesus <mjesus(at)prodam(dot)sp(dot)gov(dot)br>, Marcio Ros Moreira <mrmoreira(at)prodam(dot)sp(dot)gov(dot)br>, Marco Antonio da Silva <mantonio(at)prodam(dot)sp(dot)gov(dot)br>, Maria Aparecida Nobuko Kina de Lima <Marial(at)prodam(dot)sp(dot)gov(dot)br>, Patrícia dos Santos Bagattini Tupynambá <ptupynamba(at)prodam(dot)sp(dot)gov(dot)br>, Tania Nunes Baptista Arruda <tnunes(at)prodam(dot)sp(dot)gov(dot)br>
Subject: RES: [JDBC] Java access pgsql sample
Date: 2002-07-26 12:24:24
Message-ID: A799F7647794D311924A005004ACEA97080DDEB5@cprodamibs249.prodam
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-jdbc

Hi!

First of all, you need to put the pgjdbc2.jar in accurate directory.
For example, put the pgjdbc2.jar in $JAVA_HOME/lib and
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/lib/pgjdbc2.jar.

Segond, be sure that the postgres is accepting conections from
tcpip.

Third, compile the little java program bellow to be sure that you
can
ping the database and then try your program.
Remember, the most common url is: jdbc:postgresql://host:port/dbname

/*
** pgdbping.java - programa para testar uma conexo jdbc com PostGRES
**
** Elielson - 24.07.2002
*/

import java.sql.*;

class pgdbping {
public static void main(String args[]) {
try {
Class.forName("org.postgresql.Driver");
Connection con;

if ( args.length != 3 )
{
String
url =
"jdbc:postgresql://10.10.67.142:5432/formacao_funcionario_des",
user = "postgres",
password = "6lenao";

System.out.println("Uso: java dbping URL user password");
System.out.println("URL jdbc:postgresql://host:port/dbname");
System.out.println("Vou usar conex o interna: " + url
+ ", " + user + ", " + "*******");

con = DriverManager.getConnection(url, user, password);
}
else
{
con = DriverManager.getConnection(args[0], args[1], args[2]);
}
System.out.println("Sucesso na conexo!!");
con.close();
}
catch(Exception e) {
System.out.println("Falha na conex od");
e.printStackTrace();
}
}
}

E.F.:> -----Mensagem original-----
E.F.:> De: Ribamar FS [mailto:ribafs(at)myrealbox(dot)com]
E.F.:> Enviada em: quinta-feira, 25 de julho de 2002 16:11
E.F.:> Para: pgsql-jdbc(at)postgresql(dot)org
E.F.:> Assunto: [JDBC] Java access pgsql sample
E.F.:>
E.F.:>
E.F.:> Hi!
E.F.:>
E.F.:> Help me with this sample. It don't work:
E.F.:>
E.F.:> Linux Conectiva
E.F.:> Postgresql 7.2.1
E.F.:> pgjdbc2.jar
E.F.:>
E.F.:> /etc/profile
E.F.:> JAVA_HOME="/usr/java/jsdk"
E.F.:> J2EE_HOME="/usr/java/j2ee"
E.F.:> CLASSPATH=".:$JAVA_HOME/lib:$J2EE_HOME/lib:/usr/java/jsdk
E.F.:> /corejava/corejava.zip"
E.F.:> J2EE_CLASSPATH=$CLASSPATH
E.F.:> PATH="$PATH:$JAVA_HOME/bin:$J2EE_HOME/bin"
E.F.:> export JAVA_HOME J2EE_HOME CLASSPATH J2EE_CLASSPATH
E.F.:>
E.F.:>
E.F.:> import java.sql.*;
E.F.:> public class SQLStatement {
E.F.:> public static void main(String args[]) {
E.F.:> String url = "jdbc:postgresql:javatest";
E.F.:> Connection con;
E.F.:> String query = "select col1 from test";
E.F.:> Statement stmt;
E.F.:> try {
E.F.:> Class.forName("org.postgresql.Driver");
E.F.:> } catch(java.lang.ClassNotFoundException e) {
E.F.:> System.err.print("ClassNotFoundException: ");
E.F.:> System.err.println(e.getMessage());
E.F.:> }
E.F.:> try {
E.F.:> con =
E.F.:> DriverManager.getConnection(url,"postgres", "postgres");
E.F.:>
E.F.:> stmt = con.createStatement();
E.F.:>
E.F.:>
E.F.:> ResultSet rs = stmt.executeQuery(query);
E.F.:> ResultSetMetaData rsmd = rs.getMetaData();
E.F.:> int numberOfColumns = rsmd.getColumnCount();
E.F.:> int rowCount = 1;
E.F.:> while (rs.next()) {
E.F.:> System.out.println("Linha " +
E.F.:> rowCount + ": ");
E.F.:> for (int i = 1; i <=
E.F.:> numberOfColumns; i++) {
E.F.:> System.out.print("
E.F.:> Coluna " + i + ": ");
E.F.:>
E.F.:> System.out.println(rs.getString(i));
E.F.:> }
E.F.:> System.out.println("");
E.F.:> rowCount++;
E.F.:> }
E.F.:> stmt.close();
E.F.:> con.close();
E.F.:>
E.F.:> } catch(SQLException ex) {
E.F.:> System.err.print("SQLException: ");
E.F.:> System.err.println(ex.getMessage());
E.F.:> }
E.F.:> }
E.F.:> }
E.F.:>
E.F.:>
E.F.:> Errors:
E.F.:> ClassNotFoundException: org/postgresql/Driver
E.F.:> SQLException: driver not found: jdbc:postgresql:javatest
E.F.:>
E.F.:>
E.F.:> Thank you for attention. Sorry ny english (brasilian).
E.F.:>
E.F.:>
E.F.:> ---------------------------(end of
E.F.:> broadcast)---------------------------
E.F.:> TIP 3: if posting/reading through Usenet, please send an
E.F.:> appropriate
E.F.:> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
E.F.:> message can get through to the mailing list cleanly
E.F.:>

Browse pgsql-general by date

  From Date Subject
Next Message Ribamar FS 2002-07-26 12:39:52 Re: RES: [JDBC] Java access pgsql sample
Previous Message Ahmad Munif 2002-07-26 11:28:52 how to get primary / unique key on table ?

Browse pgsql-jdbc by date

  From Date Subject
Next Message Ribamar FS 2002-07-26 12:39:52 Re: RES: [JDBC] Java access pgsql sample
Previous Message Chantal Ackermann 2002-07-26 11:25:55 PreparedStatement: setting column names dynamically