22JDBC - CS108 Stanford Winter 2012 Handout#22 Young JDBC...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS108, Stanford Handout #22 Winter 2012 Young JDBC JDBC allows us to connect our Java programs to a database. The MySQL JDBC Driver JDBC can work with a variety of different types of databases. A JDBC Driver connects Java to a particular type of database. In our case, we ’ll need a JDBC driver for MySQL. We will use the Connector/J driver from MySQL.com available at: http://www.mysql.com/products/connector/ Download the driver. The download will be a zip file including Java source code and a JAR file with the source pre-compiled. Ignore the source code, we are only interested in the JAR file. Go ahead and store the mysql-connector-java-5.1.18-bin.jar file somewhere handy. When creating a project which uses JDBC you’ll need to tell Eclipse where your JDBC driver is. You can do this by right-clicking on the project folder in Eclipse and select “Properties” . This will bring up the project’s properties window, switch to the “Java Build Path” sect ion and select the “Libraries” tab :
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Click on the “Add External JARs” button on the right side of the window. Find and add the mysql-connector-java-5.1.18-bin.jar file. A Simple JDBC Program When working with JDBC we’ll need the same pieces of information we needed when working with MySQL directly. Make sure you have the following information which you should have received via e-mail: Your MySQL Username Your MySQL Initial Password The name of the Stanford MySQL Database Server Your MySQL Database Name In the previous handout on MySQL we created a table of metropolises and stored it in our MySQL database. We will now take a look at a program which accesses that table. Just as a reminder, here is what the table looked like: +---------------+---------------+------------+ | metropolis | continent | population | +---------------+---------------+------------+ | Mumbai | Asia | 20400000 | | New York | North America | 21295000 | | San Francisco | North America | 5780000 | | London | Europe | 8580000 | | Rome | Europe | 2715000 | | Melbourne | Australia | 3900000 | | San Jose | North America | 7354555 | | Rostov-on-Don | Europe | 1052000 | +---------------+---------------+------------+ Here is a program which prints the metropolis and population data from the table:
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern