Re: set up jdbc - Mailing list pgsql-jdbc

From Tim Lucia
Subject Re: set up jdbc
Date
Msg-id 7BFCE5F1EF28D64198522688F5449D5A3D0214@xchangeserver2.storigen.com
Whole thread Raw
In response to set up jdbc  ("Pingdong Ai" <pingdong@cs.pdx.edu>)
List pgsql-jdbc
Here is a simple program which takes 4 arguments:

javac DBTest.java      # don't forget to put the jdbc jar in the classpath
java DBTest jdbc:postgresql:your-database-name username password table-name

It connects to the jdbc URL (1st arg) using the username and password (2nd/3rd) and dumps the data in table-name (4th
arg)

Enjoy,
Tim Lucia

import java.sql.*;
public class DBTest
{
    public static void main(String[] args)
    {
        try {
            Class.forName("org.postgresql.Driver");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Connection conn =
                DriverManager.getConnection(args[0], args[1], args[2]);
            Statement s = conn.createStatement();
            dumpSQLTable(s, args[3]);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void dumpSQLTable(Statement dump, String tableName)
    {
        try {
            int rowCount = 0;
            String query = "SELECT * FROM " + tableName;
            java.sql.ResultSet rs = dump.executeQuery(query);
            ResultSetMetaData rsm = rs.getMetaData();
            final int columns = rsm.getColumnCount();
            System.out.println("<table border='1' cellspacing='1' cellpadding='1'>");
            System.out.println("<caption>" + tableName + "</caption>");
            System.out.println("<tr>");
            for (int i = 1; i <= columns; i++) {
                System.out.println("<td>" + rsm.getColumnLabel(i) + "</td>");
            }
            System.out.println("</tr>");
            while (rs.next()) {
                System.out.println("<tr>");
                for (int i = 1 ; i <= columns; i++) {
                    System.out.println("<td>" + rs.getObject(i) + "</td>");
                }
                rowCount++;
                System.out.println("</tr>");
            }
            System.out.println("<tr><td colspan='" + columns + "'>Rows = " + rowCount +
                       "</td></tr>");
            System.out.println("</table>");
        }
        catch (SQLException sqle) {
            System.out.println("Exception: " + sqle.getMessage());
        }
    }
}

-----Original Message-----
From: Pingdong Ai [mailto:pingdong@cs.pdx.edu]
Sent: Monday, May 06, 2002 7:21 PM
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] set up jdbc


hi, everyone

I am a beginner. I want to set up jdbc driver for Postgres 7.2.   Can
someone give me
a step-by-step suggestion.  Btw, I have the Postgres DBA account, but I
don't have the
system administrator account. (I am not sure whether this will make
difference).

Thanks a lot in advance!

Pingdong Ai
Portland, OR


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

pgsql-jdbc by date:

Previous
From: "Thomas De Vos"
Date:
Subject: Encoding
Next
From: "Nick Fankhauser"
Date:
Subject: Re: set up jdbc