1. Developing PGTop for Android
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 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
4. 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
10. 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
11. 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
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
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
15. 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
16. 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
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 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
19. 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
20. 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
21. 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
22. 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
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 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
31. 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
32. 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
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
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