O'Reilly logo

Database Programming with JDBC & Java, Second Edition by George Reese

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Basic Database Access

Now that you are connected to the database, you can begin making updates and queries. The most basic kind of database access involves writing JDBC code when you know ahead of time whether the statements you are sending are updates (INSERT, UPDATE, or DELETE) or queries (SELECT). In the next chapter, you will discuss more complex database access that allows you to execute statements of unknown types.

Basic database access starts with the Connection object you created in the previous section. When this object first gets created, it is simply a direct link to the database. You use a Connection object to generate implementations of java.sql.Statement tied to the same database transaction. After you have used one or more Statement objects generated by your Connection, you can use it to commit or rollback the Statement objects associated with that Connection.

A Statement is very much what its name implies—a SQL statement. Once you get a Statement object from a Connection, you have what amounts to a blank check that you can write against the transaction represented by that Connection. You do not actually assign SQL to the Statement until you are ready to send the SQL to the database.

This is when it becomes important to know what type of SQL you are sending to the database, because JDBC uses a different method for sending queries than for sending updates. The key difference is the fact that the method for queries returns an instance of java.sql.ResultSet, while ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required