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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.3 Making C<strong>on</strong>necti<strong>on</strong>s329These three operati<strong>on</strong>s corresp<strong>on</strong>d to JDBC objects for doing these verythings, namely the classes C<strong>on</strong>necti<strong>on</strong>, PreparedStatement, and ResultSet.Let’s jump right in and look at some code. Example 15.1 will make ac<strong>on</strong>necti<strong>on</strong> to a MySQL database, prepare a query statement, execute the query,then read up the results.Let’s also look at a similar example, but this time for an Oracle database(Example 15.2). Notice how much is the same between the two examples, andwhich parts are different.The <strong>on</strong>ly real difference between the two programs has to do with thec<strong>on</strong>necti<strong>on</strong>s. Once the c<strong>on</strong>necti<strong>on</strong> to the database is established, the rest of thecode is exactly the same—which is what you’d hope for in an abstracti<strong>on</strong>. Thisis a good news for developers: “Learn <strong>on</strong>ce, use anywhere.”15.3MAKING CONNECTIONSThe most complicated part of JDBC is establishing the c<strong>on</strong>necti<strong>on</strong>. There areseveral ways to make a c<strong>on</strong>necti<strong>on</strong>, depending <strong>on</strong> how much informati<strong>on</strong> aboutthe c<strong>on</strong>necti<strong>on</strong> driver you want hard-coded into your applicati<strong>on</strong>. We are goingto keep it simple and describe <strong>on</strong>e straightforward way to c<strong>on</strong>nect.The DriverManager class is where our applicati<strong>on</strong> goes to get itsc<strong>on</strong>necti<strong>on</strong> to the database, as shown in our example. Many different JDBCdrivers can register with the DriverManager, and it can make thec<strong>on</strong>necti<strong>on</strong> to the kind of driver that you want based <strong>on</strong> the URL that youprovide in the call to getC<strong>on</strong>necti<strong>on</strong>(). So where did our example registeranything with the DriverManager? Well, it happened indirectly, via theClass.forName(...).newInstance(); call. That loaded the class and createdan instance of it. The JDBC specificati<strong>on</strong> says that when a Driver classinitializes it must register with the DriverManager. So it happened “under thecovers,” in loading the driver class.Another difference between the two examples deals with how the usernameand password are supplied to the database. Both are supplied in the URL,though in different syntax. That syntax is at the discreti<strong>on</strong> of those who implementedthe JDBC driver for that particular flavor of database. If we were toc<strong>on</strong>struct the URL at runtime, so that the user could supply a username andpassword dynamically, we’d want to remove the difference in how the URL isc<strong>on</strong>structed. To do that we could use a call to getC<strong>on</strong>necti<strong>on</strong>() with a signaturethat includes the username and password as separate String parameters:

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

Saved successfully!

Ooh no, something went wrong!