210 likes | 317 Views
This overview provides a comprehensive guide to JDBC (Java Database Connectivity), an API that enables Java applications to interact with relational databases. It includes details on setting up JDBC, including installation of Java, drivers, and databases like MySQL and Oracle. Learn how to establish connections, execute SQL statements, and retrieve data using Java classes and interfaces. Example queries and ResultSet handling techniques are also covered to help you manage database operations efficiently.
E N D
JDBC Overview Autumn 2001 Lecturer: C. DeJong
Relational Databases • widespread use • used via SQL (Structured Query Language) • freely available • powerful • text-based
What is JDBC? • An API for Java database connectivity • A collection of Java classes from Sun • A set of interfaces for database programming • java.sql.*
Steps for setting up JDBC • install Java and JDBC (available from java.sun.com) • install a driver • install a relational database • MySQL, PostgreSQL (free!) • Oracle, Sybase, IBM’s DB2 (commercial)
Establishing a connection • Load the database driver • Make the connection
Loading the driver • driver should be provided with database • one line of code: Class.forName(<driver class name>); • ex: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Make the connection • Returns a Connection object • Syntax: Connection con = DriverManager.getConnection( url, "myLogin", "myPassword");
URL for connecting • should be in documentation with JDBC driver • starts with “jdbc:” • can connect across a network
Statements • used to send SQL to the database • syntax: Statement stmt = con.createStatement();
Using statements • creating a table stmt.executeUpdate( "CREATE TABLE COFFEES " + "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " + "SALES INTEGER, TOTAL INTEGER)");
Using statements • inserting data stmt.executeUpdate( "INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)");
Sample query • in SQL: SELECT COF_NAME, PRICE FROM COFFEES
Query result COF_NAME PRICE ------------------ ----- Colombian 7.99 French_Roast 8.99 Espresso 9.99 Colombian_Decaf 8.99 French_Roast_Decaf 9.99
Retrieving data with ResultSet • Statements can run a query on a database • This returns an object. • This object is a ResultSet.
Running the query in Java Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery( "SELECT COF_NAME, PRICE FROM COFFEES");
Inspecting the ResultSet while (rs.next()) { String s = rs.getString("COF_NAME"); float n = rs.getFloat("PRICE"); System.out.println(s + " " + n); }
Output • The output will look something like this: Colombian 7.99 French_Roast 8.99 Espresso 9.99 Colombian_Decaf 8.99 French_Roast_Decaf 9.99
alternative syntax while (rs.next()) { String s = rs.getString(1); float n = rs.getFloat(2); System.out.println(s + " " + n); } • note: starts at 1, not 0!
some ResultSet retrieval methods • getString() • getInt() • getFloat() • getObject() • … many others
Closing • When finished, call close() on • ResultSet objects • Statement objects • Connection objects • … or you may have a memory leak!
Exceptions • Most methods on classes in the java.sql package throw java.sql.SQLException • SQLException.getMessage() shows database error • SQLException is a checked exception • so: try/catch/finally or throws