Sample: JDBC-Test für PostgreSQL



import java.sql.*;

public class Sample {

  public static void main (String[] args) throws SQLException, 
                                                 ClassNotFoundException {

    Class.forName("org.postgresql.Driver");		// load JDBC driver

    // establish a connection with the desired database
    Connection db = 
      DriverManager.getConnection(
        "jdbc:postgresql://localhost.html",
        "username", 
        "passwort");

    // set up a query to run
    PreparedStatement st = db.prepareStatement("select * from adressen");

    // run the query to produce a ResultSet
    ResultSet rs = st.executeQuery();
    int numCols  = rs.getMetaData().getColumnCount();

    // print out the contents of the ResultSet
    while (rs.next()) {
      for (int j=1; j <= numCols; j++) 
	System.out.print(rs.getString(j) + "  ");
      System.out.println();
    }

    // close the resources 
    rs.close();
    st.close();
    db.close();
  }
}