13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

336Chapter 15Accessing the Data: An Introducti<strong>on</strong> to JDBCpositi<strong>on</strong> it afterLast(), then getRow() to get the size, then positi<strong>on</strong> it backto beforeFirst() to be ready to read.15.6UPDATES, INSERTS, DELETESNot every acti<strong>on</strong> <strong>on</strong> a database returns a ResultSet. Operati<strong>on</strong>s that createthe tables in a database, or those that modify, insert, or delete rows of data d<strong>on</strong>’treturn rows of values. For those sorts of SQL statements, we d<strong>on</strong>’t callexecuteQuery()—we call executeUpdate() instead. It returns an int givingthe number of rows affected by the executi<strong>on</strong>. In the case of a CREATE TABLEoperati<strong>on</strong>, it simply returns 0.15.7REVIEWC<strong>on</strong>necting a Java applicati<strong>on</strong> to a database is a key step in real applicati<strong>on</strong>s.The mechanisms for doing that are varied and can be complicated. We’vepicked a single approach for c<strong>on</strong>necting, to keep it simple and to highlight thesimilarities and differences between two different database implementati<strong>on</strong>s.The rest of the c<strong>on</strong>versati<strong>on</strong> with a database depends as much <strong>on</strong> yourSQL skills as <strong>on</strong> Java skills. Java will take strings of SQL, which can be parameterized,and, via JDBC calls, send them to be executed by the database. Theresults are like iterators; they can be retrieved, row after row, for further processing.Updates, inserts, and deletes are also easily d<strong>on</strong>e, with a few simple calls toprocess the SQL.15.8WHAT YOU STILL DON’T KNOWWe’ve skipped over lots of topics to keep this simple. There are a variety ofways to c<strong>on</strong>nect to a database that we haven’t covered; the most important <strong>on</strong>emay be the use of a DataSource instead of a DriverManager. As of Java 1.4.1,the DataSource is the preferred means of making c<strong>on</strong>necti<strong>on</strong>s to yourdatabase. While it makes the code more portable (e.g., if you’re planning <strong>on</strong>moving around, changing databases and/or database servers), it is more complicatedto set up—there are more “moving parts” to get right. If you already havea Java Naming and Directory Interface (JNDI) service running, thought, it’svery straightforward (see page 254 and the following pages of Gallardo).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!