Re: set up jdbc

From: "Tim Lucia" <Tim(dot)Lucia(at)storigen(dot)com>
To: <pingdong(at)cs(dot)pdx(dot)edu>, <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: set up jdbc
Date: 2002-05-07 13:04:44
Message-ID: 7BFCE5F1EF28D64198522688F5449D5A3D0214@xchangeserver2.storigen.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)cs(dot)pdx(dot)edu]
Sent: Monday, May 06, 2002 7:21 PM
To: pgsql-jdbc(at)postgresql(dot)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(at)postgresql(dot)org)

Browse pgsql-jdbc by date

  From Date Subject
Next Message Nick Fankhauser 2002-05-07 13:06:51 Re: set up jdbc
Previous Message Thomas O'Dowd 2002-05-06 23:31:19 Re: set up jdbc