import java.sql.*;

public class Bug {
	
	public static void main(String[] args) throws Exception {
		System.out.println("Driver version: " + org.postgresql.Driver.getVersion());
		Connection con = DriverManager.getConnection("jdbc:postgresql:bug", "bruno", null);
		
		checkVersion(con);
		triggerBug(con);
		
		con.close();
	}
	
	public static void checkVersion(Connection con) throws SQLException {
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery("select version()");
		if(!rs.next())
			throw new RuntimeException();
		System.out.println("Database version: " + rs.getString(1));
		rs.close();
		st.close();
	}
	
	public static void triggerBug(Connection con) throws SQLException {
		PreparedStatement ps = con.prepareStatement("insert into foo(value,date) values (?,?) returning id");
		ps.getMetaData();
		ps.clearParameters();
		ps.setString(1, "Hello world");
		ps.setDate(2, new Date(new java.util.Date().getTime()));
		ResultSet rs = ps.executeQuery();
		if(!rs.next())
			throw new RuntimeException();
		System.out.println(rs.getInt(1));
		rs.close();
		ps.close();
	}

}
