Developing PGTop for Android

Post on 27-Dec-2014

911 views 1 download

description

A brief introduction on developing Android PostgreSQL apps using JDBC.

Transcript of Developing PGTop for Android

Developing PGTop for Android

Mark Wongmarkwkm@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 appfor Android (BSD License):http://github.com/markwkm/PGTop

Note: Must allow non-Market app toinstall because PGTop for Android isnot 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 version9.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, forbuilding 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 fromhttp://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, butdoes not require J2EE as it has been added to the J2SE release.

• JDK 1.6 - JDBC4. Support for JDBC4 methods is limited. The driverbuilds, 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 versionhttp://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 Eclipsehttp://developer.android.com/guide/developing/other-ide.html

• More sample applicationshttp://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 andquerying 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 athttp://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 Specificationhttp://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 beviewed (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 UnportedLicense. To view a copy of this license, (a) visithttp://creativecommons.org/licenses/by/3.0/us/; or, (b) send aletter 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