RES: [JDBC] Java access pgsql sample - Mailing list pgsql-general

From Elielson Fontanezi
Subject RES: [JDBC] Java access pgsql sample
Date
Msg-id A799F7647794D311924A005004ACEA97080DDEB5@cprodamibs249.prodam
Whole thread Raw
List pgsql-general
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@myrealbox.com]
E.F.:> Enviada em: quinta-feira, 25 de julho de 2002 16:11
E.F.:> Para: pgsql-jdbc@postgresql.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@postgresql.org so that your
E.F.:> message can get through to the mailing list cleanly
E.F.:>

pgsql-general by date:

Previous
From: Elielson Fontanezi
Date:
Subject: :-( Free Books
Next
From: "Scott Gammans"
Date:
Subject: Is there any such thing as PostgreSQL security on a hosted website?