Developing PGTop for Android
Mark Wong
markwkm@postgresql.org
PostgreSQL Conference West 2010
November 3, 2010
__ __
/ ~~~/  . o O ( Hello! )
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 2 / 36
Overview
Slides available at http://www.sideshare.net/markwkm
• What is PGTop for Android?
• Development environment
• Using the PostgreSQL JDBC driver
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 3 / 36
PGTop for Android
A PostgresSQL stats monitoring app
for Android (BSD License):
http://github.com/markwkm/PGTop
Note: Must allow non-Market app to
install because PGTop for Android is
not in the Market.
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 4 / 36
Main Screen
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 5 / 36
Add Database Connection
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 6 / 36
Database Statistics
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 7 / 36
Background Writer Statistics
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 8 / 36
Activity Statistics
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 9 / 36
The Development Environment
A few more details on the following slides about:
• Java (JDK, etc.)
• Android SDK
• Eclipse [optional]
Full system requirement details:
http://developer.android.com/sdk/requirements.html
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 10 / 36
Gentoo Linux Plug
Keep in mind I use a 64-bit Gentoo Linux.
1 The ”g” logo is a trademark of Gentoo Foundation, Inc., and that any Gentoo artwork is
copyright Gentoo Foundation, Inc.
2 The ”g” logo and Gentoo artwork are used in content that pertain to ”the Gentoo project”, as
directed by the Gentoo Foundation, Inc., and not any effort outside or beyond the authority of the
Gentoo Foundation, Inc.
3 The content, project, site, product or any other type of item with which the ”g” logo or Gentoo
artwork is associated is not part of the Gentoo project and is not directed or managed by Gentoo
Foundation, Inc.
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 11 / 36
Java Related Details
• Requires 32-bit Java 5 or 6 development kit (JDK)
http://www.oracle.com/technetwork/java/javase/downloads/
• PostgreSQL JDBC3/4 Driver http://jdbc.postgresql.org version
9.0-801 or later
• Apache Ant http://ant.apache.org/
Additional commentary mostly pertaining to Linux:
• This doesn’t mean it has to be a 32-bit OS
• But if you have a 64-bit OS, both 32- and 64-bit Java is needed, for
building and packaging respectively
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 12 / 36
JDBC3 vs JDBC4
The PostgreSQL JDBC driver is thread-safe. Some additional comments from
http://jdbc.postgresql.org/download.html:
• If you are using the 1.6 JVM, then you should use the JDBC4 version.
• JDK 1.4, 1.5 - JDBC 3. This contains support for SSL and javax.sql, but
does not require J2EE as it has been added to the J2SE release.
• JDK 1.6 - JDBC4. Support for JDBC4 methods is limited. The driver
builds, but the several of the new methods are stubbed out.
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 13 / 36
Android SDK Details
http://developer.android.com/sdk/
• Android emulator
• Android jars
• Android packaging tools
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 14 / 36
Eclipse Details
Optional to have Eclipse Classic 3.4 or 3.5, but must be 32-bit version
http://www.eclipse.org/downloads/
Advantages for using Eclipse:
• GUI designer for building the user interface
• Hides XML
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 15 / 36
Things I learned about
. . . but won’t cover here.
• Using SQLite database methods
• User interface widgets
• Spinner
• TextView
• EditView
• CheckBox
• Toast
• Button
• How the threading model works, or doesn’t work with the user interface
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 16 / 36
Android Programming Examples
• Hello, World (includes example without using Eclipse)
http://developer.android.com/resources/tutorials/hello-world.html
• More examples without using Eclipse
http://developer.android.com/guide/developing/other-ide.html
• More sample applications
http://developer.android.com/resources/samples/
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 17 / 36
PostgreSQL JDBC
Now for some simple examples for connecting to a PostgreSQL database and
querying some data. Warning, code formatted to fit better on these slides. . .
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 18 / 36
Open a Database Connection
Load the PostgreSQL JDBC driver and open a database connection using SSL:
Class.forName("org.postgresql.Driver");
String url;
url = "jdbc:postgresql://pghost:5432/pgdatabase" +
"?sslfactory=org.postgresql.ssl.NonValidatingFactory" +
"&ssl=true";
Connection conn = DriverManager.getConnection(url,
"pguser",
"pgpass");
 Don’t forget to close the connection when you’re done.
 conn.close();
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 19 / 36
Execute a Query
Building on the previous slide, select the name of all relations from pg class
and iterate through every row returned:
String sql;
sql = "SELECT relname FROM pg_class WHERE relkind = ’r’;"
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
 Columns are enumerated starting with 1.
String relname = rs.getString(1);
}
rs.close();
st.close();
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 20 / 36
Execute a Query with a Bind Value
Building on the previous slide, select the number of all relations from pg class:
String sql = "SELECT COUNT(*) FROM pg_class WHERE relkind = ?;"
PreparedStatement ps = conn.createStatement();
// Bind variables are enumerated starting with 1;
ps.setString(1, "r");
ResultSet rs = ps.executeQuery(sql);
rs.next();
long count = rs.getLong(1);
rs.close();
ps.close();
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 21 / 36
More JDBC Code Examples
The PostgreSQL JDBC documentation has more examples at
http://jdbc.postgresql.org/documentation/head/
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 22 / 36
Using cursors
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 23 / 36
Executing INSERT, UPDATE, DELETE SQL statements
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 24 / 36
Creating and modifying database objects
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 25 / 36
Calling stored functions
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 26 / 36
Handling binary data
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 27 / 36
JDBC escapes e.g. strings, outer joins, date-time, scalar functions
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 28 / 36
PostgreSQL Extensions to the JDBC API
• Geometric data types
• Large objects
• Listen/Notify
• Server prepared statements
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 29 / 36
Connection pools
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 30 / 36
Further JDBC Reading
JDBC API Documentation and JDBC Specification
http://jdbc.postgresql.org/documentation/head/reading.html
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 31 / 36
Ready to debug your new app?
After the Android emulator has started the stdout/stderr messages can be
viewed (I didn’t try the debugger).
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 32 / 36
__ __
/ ~~~/  . o O / 
,----( oo ) | Thanks to Google for providing |
/ __ __/ | phones! |
/| ( |(  /
^  /___ / |
|__| |__|-"
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 33 / 36
__ __
/ ~~~/  . o O ( Thank you! )
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 34 / 36
Acknowledgements
Hayley Jane Wakenshaw
__ __
/ ~~~/ 
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 35 / 36
License
This work is licensed under a Creative Commons Attribution 3.0 Unported
License. To view a copy of this license, (a) visit
http://creativecommons.org/licenses/by/3.0/us/; or, (b) send a
letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco,
California, 94105, USA.
markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 36 / 36

Developing PGTop for Android

  • 1.
    Developing PGTop forAndroid Mark Wong markwkm@postgresql.org PostgreSQL Conference West 2010 November 3, 2010
  • 2.
    __ __ / ~~~/ . o O ( Hello! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 2 / 36
  • 3.
    Overview Slides available athttp://www.sideshare.net/markwkm • What is PGTop for Android? • Development environment • Using the PostgreSQL JDBC driver markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 3 / 36
  • 4.
    PGTop for Android APostgresSQL stats monitoring app for Android (BSD License): http://github.com/markwkm/PGTop Note: Must allow non-Market app to install because PGTop for Android is not in the Market. markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 4 / 36
  • 5.
    Main Screen markwkm (PGWest2010) Developing PGTop for Android November 3, 2010 5 / 36
  • 6.
    Add Database Connection markwkm(PGWest 2010) Developing PGTop for Android November 3, 2010 6 / 36
  • 7.
    Database Statistics markwkm (PGWest2010) Developing PGTop for Android November 3, 2010 7 / 36
  • 8.
    Background Writer Statistics markwkm(PGWest 2010) Developing PGTop for Android November 3, 2010 8 / 36
  • 9.
    Activity Statistics markwkm (PGWest2010) Developing PGTop for Android November 3, 2010 9 / 36
  • 10.
    The Development Environment Afew more details on the following slides about: • Java (JDK, etc.) • Android SDK • Eclipse [optional] Full system requirement details: http://developer.android.com/sdk/requirements.html markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 10 / 36
  • 11.
    Gentoo Linux Plug Keepin mind I use a 64-bit Gentoo Linux. 1 The ”g” logo is a trademark of Gentoo Foundation, Inc., and that any Gentoo artwork is copyright Gentoo Foundation, Inc. 2 The ”g” logo and Gentoo artwork are used in content that pertain to ”the Gentoo project”, as directed by the Gentoo Foundation, Inc., and not any effort outside or beyond the authority of the Gentoo Foundation, Inc. 3 The content, project, site, product or any other type of item with which the ”g” logo or Gentoo artwork is associated is not part of the Gentoo project and is not directed or managed by Gentoo Foundation, Inc. markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 11 / 36
  • 12.
    Java Related Details •Requires 32-bit Java 5 or 6 development kit (JDK) http://www.oracle.com/technetwork/java/javase/downloads/ • PostgreSQL JDBC3/4 Driver http://jdbc.postgresql.org version 9.0-801 or later • Apache Ant http://ant.apache.org/ Additional commentary mostly pertaining to Linux: • This doesn’t mean it has to be a 32-bit OS • But if you have a 64-bit OS, both 32- and 64-bit Java is needed, for building and packaging respectively markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 12 / 36
  • 13.
    JDBC3 vs JDBC4 ThePostgreSQL JDBC driver is thread-safe. Some additional comments from http://jdbc.postgresql.org/download.html: • If you are using the 1.6 JVM, then you should use the JDBC4 version. • JDK 1.4, 1.5 - JDBC 3. This contains support for SSL and javax.sql, but does not require J2EE as it has been added to the J2SE release. • JDK 1.6 - JDBC4. Support for JDBC4 methods is limited. The driver builds, but the several of the new methods are stubbed out. markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 13 / 36
  • 14.
    Android SDK Details http://developer.android.com/sdk/ •Android emulator • Android jars • Android packaging tools markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 14 / 36
  • 15.
    Eclipse Details Optional tohave Eclipse Classic 3.4 or 3.5, but must be 32-bit version http://www.eclipse.org/downloads/ Advantages for using Eclipse: • GUI designer for building the user interface • Hides XML markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 15 / 36
  • 16.
    Things I learnedabout . . . but won’t cover here. • Using SQLite database methods • User interface widgets • Spinner • TextView • EditView • CheckBox • Toast • Button • How the threading model works, or doesn’t work with the user interface markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 16 / 36
  • 17.
    Android Programming Examples •Hello, World (includes example without using Eclipse) http://developer.android.com/resources/tutorials/hello-world.html • More examples without using Eclipse http://developer.android.com/guide/developing/other-ide.html • More sample applications http://developer.android.com/resources/samples/ markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 17 / 36
  • 18.
    PostgreSQL JDBC Now forsome simple examples for connecting to a PostgreSQL database and querying some data. Warning, code formatted to fit better on these slides. . . markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 18 / 36
  • 19.
    Open a DatabaseConnection Load the PostgreSQL JDBC driver and open a database connection using SSL: Class.forName("org.postgresql.Driver"); String url; url = "jdbc:postgresql://pghost:5432/pgdatabase" + "?sslfactory=org.postgresql.ssl.NonValidatingFactory" + "&ssl=true"; Connection conn = DriverManager.getConnection(url, "pguser", "pgpass"); Don’t forget to close the connection when you’re done. conn.close(); markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 19 / 36
  • 20.
    Execute a Query Buildingon the previous slide, select the name of all relations from pg class and iterate through every row returned: String sql; sql = "SELECT relname FROM pg_class WHERE relkind = ’r’;" Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while (rs.next()) { Columns are enumerated starting with 1. String relname = rs.getString(1); } rs.close(); st.close(); markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 20 / 36
  • 21.
    Execute a Querywith a Bind Value Building on the previous slide, select the number of all relations from pg class: String sql = "SELECT COUNT(*) FROM pg_class WHERE relkind = ?;" PreparedStatement ps = conn.createStatement(); // Bind variables are enumerated starting with 1; ps.setString(1, "r"); ResultSet rs = ps.executeQuery(sql); rs.next(); long count = rs.getLong(1); rs.close(); ps.close(); markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 21 / 36
  • 22.
    More JDBC CodeExamples The PostgreSQL JDBC documentation has more examples at http://jdbc.postgresql.org/documentation/head/ markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 22 / 36
  • 23.
    Using cursors markwkm (PGWest2010) Developing PGTop for Android November 3, 2010 23 / 36
  • 24.
    Executing INSERT, UPDATE,DELETE SQL statements markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 24 / 36
  • 25.
    Creating and modifyingdatabase objects markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 25 / 36
  • 26.
    Calling stored functions markwkm(PGWest 2010) Developing PGTop for Android November 3, 2010 26 / 36
  • 27.
    Handling binary data markwkm(PGWest 2010) Developing PGTop for Android November 3, 2010 27 / 36
  • 28.
    JDBC escapes e.g.strings, outer joins, date-time, scalar functions markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 28 / 36
  • 29.
    PostgreSQL Extensions tothe JDBC API • Geometric data types • Large objects • Listen/Notify • Server prepared statements markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 29 / 36
  • 30.
    Connection pools markwkm (PGWest2010) Developing PGTop for Android November 3, 2010 30 / 36
  • 31.
    Further JDBC Reading JDBCAPI Documentation and JDBC Specification http://jdbc.postgresql.org/documentation/head/reading.html markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 31 / 36
  • 32.
    Ready to debugyour new app? After the Android emulator has started the stdout/stderr messages can be viewed (I didn’t try the debugger). markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 32 / 36
  • 33.
    __ __ / ~~~/ . o O / ,----( oo ) | Thanks to Google for providing | / __ __/ | phones! | /| ( |( / ^ /___ / | |__| |__|-" markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 33 / 36
  • 34.
    __ __ / ~~~/ . o O ( Thank you! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 34 / 36
  • 35.
    Acknowledgements Hayley Jane Wakenshaw ____ / ~~~/ ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 35 / 36
  • 36.
    License This work islicensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, (a) visit http://creativecommons.org/licenses/by/3.0/us/; or, (b) send a letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco, California, 94105, USA. markwkm (PGWest 2010) Developing PGTop for Android November 3, 2010 36 / 36