- Good afternoon\n\n- We&#x2019;re going to take a quick look at the basic elements that make up JDBC\n
- Talk about exactly where JDBC fits into the Java language\n\n- Then look at some examples of the 4 basic CRUD operations for databases - Create, Read, Update and Delete\n\n
- Enterprise developer and architect\n\n- I work with these kinds of technologies on an almost daily basis\n\n\n- How about you? Anyone with JDBC experience? How about experience connecting to databases from other languages like .NET or PHP?\n\n\n
- This is a quick and shallow overview of JDBC so the pre-requisites are pretty light\n\n- Hopefully you know how to write and compile simple Java programs\n\n- Also, some knowledge of basic relationaly database concepts like tables and the SQL language\n
- In a nutshell, JDBC is simply an API that allows you to make a connection from a Java application to a relational database and execute SQL statements against that database\n\n- It is part of the core Java APIs and has been around since very early on in Java&#x2019;s life\n\n- All of the API is contained in 2 packages, java.sql contains the basic API, the javax.sql contains some more advanced API elements\n
- JDBC is the piece that sits between your code and the database\n\n- There is the standard API code, combined with a vendor specific Driver that knows how to talk to a particular database\n\n- In theory you can swap in a different driver and your program can work with a different database\n
- To initialize a Driver you simply use the standard Java classloader to load the Driver class\n\n- In this example we are loading a driver to talk to an Apache Derby database\n\n- In JavaSE 6, you don&#x2019;t even need to do this, it happens in the background as part of the new Service API\n\n
- url is a database specific string containing information on how to connect to the database, including credentials and other details\n\n- use the DriverManager to actually create the connection\n\n- notice the error handling\n
- pretty basic sql table create statement\n\n- notice that Derby doesn&#x2019;t support a boolean column type, so we use an integer, 0 for false, 1 for true\n\n- we have now introduced a little vendor dependency\n\n- use the connection to get a Statement object, and then execute the SQL\n
- once again, pretty standard SQL for inserting a row into a database\n\n- id column, name column, integer boolean column\n\n- once again we simply get a Statement object from our connection, then use it to run SQL against the database\n
- basic sql update statement\n\n- changing the value of the hired column to the value 1, and we use the where clause to specify which row we want to update\n\n- use the executeUpdate method call instead\n
- standard sql select statement, we want all columns from all rows\n\n- use the executeQuery method, which returns a ResultSet object\n\n- in this example we pass the result set to another method to print out the results\n
- so here is the printResults method, notice the ResultSet being passed into this method as a parameter\n\n- notice the while loop, this allows us to process each row in the result set in turn\n\n- notice the resultSet.get methods, which include a 1-based column index\n\n- if we run this code, can anyone tell me what actually gets printed to the screen?\n
- notice the true value for the first row, even though the value in the database was the number 1, the JDBC API was able to convert that to a boolean for us\n
- Of course once you have been inserting and updating data for a while, you inevitably need to delete some data\n\n- Notice the standard SQL delete statement, where we are saying to delete all rows in the candidates table whose id column has the value 888 in it\n\n- Get the Statement object from the connection and use it to run the SQL\n
JDBC Basics (In 20 Minutes Flat) Craig S. Dickson!
Agenda • What is JDBC? • Connecting to a database • Creating & Updating data • Querying data • Deleting data!
About Me • Software Architect, currently consulting with Adobe and BET • 15 years software development experience • Sun Certified JavaEE Architect!
Assumptions • Basic understanding of JavaSE 6 • Basic understanding of relational database concepts and SQL!
What is JDBC? • JDBC = Java Database Connectivity • Provides basic API for connecting to relational databases • Part of JavaSE since version 1.1 (c. 1997) • java.sql and javax.sql!
In conclusion ... • JDBC can be used to connect to relational databases from Java programs • Allows all basic CRUD operations and can handle sophisticated multi-tier, multi-vendor environments • Copious amounts of error handling required • Can unintentionally create a dependency to a specific database vendor!