//Modify YOUR_USERNAME and YOUR_PASSWORD. //Note that dbname is YOUR_USERNAME in PostgreSQL system. //drop table Hello if it exists in your database. // //Compile it by // javac -classpath .:PATH_TO/pg73jdbc3.jar psql2.java // //Download pg73jdbc3.jar, modify PATH_TO and run it by // java -cp .:PATH_TO/pg73jdbc3.jar HelloAgain // //See 8.5 in "A First Course in Database System", // Jeffrey D. Ullman and Jennifer Widom, Prentice Hall, 1997. import java.sql.*; import javax.sql.*; //new JDBC package, supports DataSource import org.postgresql.jdbc3.*; //PostgreSQL driver class HelloAgain { public static void main(String args[]) throws Exception // Bad programming style, please don't follow it. // Or you may lose marks for programming style. { //setup data source Jdbc3PoolingDataSource source = new Jdbc3PoolingDataSource(); source.setDataSourceName("PostgreSQL"); source.setServerName("sql.csic.umd.edu"); source.setDatabaseName("YOUR_USERNAME"); source.setUser("YOUR_USERNAME"); source.setPassword("YOUR_PASSWORD"); source.setMaxConnections(10); //connect to db for PostgreSQL Connection conn = source.getConnection(); //set auto-commit conn.setAutoCommit(true); //As expected, the following codes are as same as before Statement statement = conn.createStatement(); statement.executeUpdate("CREATE TABLE hello (value varchar(32))"); statement.executeUpdate("INSERT INTO hello VALUES('Hello world again!')"); ResultSet res = statement.executeQuery("SELECT * FROM hello "); if (res != null) for(; res.next();) System.out.println(res.getString(1)); res.close(); statement.close(); conn.close(); } }