Welcome to the second article in a series of four designed to introduce InformIT readers to the ins and outs of mobile database development. As we discussed in my first article in this series, "Mobile Database Development Tutorial: Part 1," the Sybase SQL Anywhere Studio 8.0 database product will be the DBMS tool of choice for our mobile clients due to its rich feature set and current popularity among developers who build mobile sales and field force automation applications. In the first article, we set up a basic Adaptive Server Anywhere (ASA) database (named mobile_sales.db) consisting of four tables:
In this article, I'll present the techniques required to build the actual client application using Java (either J2SE or a PersonalJava runtime environment are required). Note that we won't delve into J2ME MIDP hereneither ASA nor the UltraLite deployment option are supported on MIDP devices. J2SE or PersonalJava runtimes are currently available for Win32, Linux, Symbian, Windows CE, and Palm OS platformsmaking Java a popular development language for developers who want to write data access code once and deploy it to a variety of platforms. I say "data access code" because the user interface will generally need to be customized for each individual platform to accommodate changes in form factor and screen real estate from device to device.
Connecting to ASA
Connections to ASA databases are made via the supplied jConnect JDBC Driver, which is contained in the jconn2.jar file stored in the Sybase\Shared\jConnect-5_5 directory after installation. To access this JAR file from your application, simply include jconn2.jar in your classpath, as you would any other Java archive. This archive includes Sybase's 100% pure JDBC driver. A separate driver is included for applications that want to connect to an RDBMS using a JDBC-ODBC bridge. As with any JDBC application, the first step is to obtain a java.sql.Connection object that can be used to query the database. The following code snippet illustrates how this would be done:
Properties props = new Properties(); props.put( "user", "admin" ); props.put( "password", "123456" ); Class.forName( "com.sybase.jdbc.SybDriver" ).newInstance(); String URL = "jdbc:sybase:Tds:localhost"; Connection conn = DriverManager.getConnection( URL , props );
The connection above was obtained using a "ServiceName" parameter, which must be of the following syntax:
Typecasting the com.sybase.jdbc.SybDriver class allows us to also use the SybDriver class' setRemotePassword() method to pass additional parameters.