Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.

Similar presentations


Presentation on theme: "Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D."— Presentation transcript:

1 Beginning Databases with JDBC Mike Bradley Adapted from http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html and notes by Kevin Parker, Ph.D. http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html

2 Overview Getting Started Setting Up a Database Establishing a Connection Creating Statements Executing Statements Retrieving Values from Result Sets Updating Tables Intermediate JDBC

3 Getting Started Things you need installed Java and JDBC  Get the latest JDK at http://java.sun.com/products/JDK/CurrentRelease http://java.sun.com/products/JDK/CurrentRelease Database drivers  Can be supplied by the vendor  JDBC-ODBC bridge is installed automatically with JDK for Windows and Solaris Database Management System  If needed, not necessary for some databases, such as Microsoft Access

4 Setting Up a Database JDBC allows for Connections to a database Sending SQL commands to the database Receiving responses from the database Things to remember Must have access to proper library  Import java.sql.* Objects must be in methods that can throw exceptions, or try..catch blocks to catch ClassNotFoundException and SQLException

5 Establishing a Connection Establishing a connection consists of two steps Loading drivers  Class.forName(“jdbc.DriverName”);  Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver” ); Making the connection  Connection con = DriverManager.getConnection(url, “myLogin”, “myPassword”)

6 Establishing a Connection: The URL What to put in the URL Documentation for the driver should indicate what to put after the jdbc: For the JDBC-ODBC bridge it will be jdbc:odbc:something  Something can be a DSN, or connection string information jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/comic.mdb Jdbc:odbc:Driver={SQL Server};Server=SERVERNAME;UID=user;PWD=pass;datab ase=DATABASENAME

7 Creating Statements Statement objects send SQL commands to the database Statements require an active Connection object to be created Statement stmt = con.createStatement();

8 Executing Statements Three types of statements to execute Queries Data updates Data definition language (DDL) statements Called with stmt.executeXXX executeQuery(“SQL statement”) for returning ResultSet executeUpdate(“SQL statement”) for data updates and DDL statements

9 Retrieving Values from Result Sets Creating a result set ResultSet rs = stmt.executeQuery(someQuery) Using next method Initially the cursor for the RecordSet is prior to the first record rs.next() moves the cursor to the record in the record set

10 Retrieving (cont.) Using the rs.getXXX() methods getXXX methods exist for most primitive data types and some common objects (String, Date, Object) Can take either String or int parameter  String parameter is the field (column) name String s = rs.getString(“IssueName”);  Int parameter is the 1-based ordinal number of the field String s = rs.getString(3);

11 Updating Tables Uses executeQuery(someQuery) Used for data updates and DDL statements INSERT, UPDATE, and DELETE CREATE TABLE Can either get return value or ignore it int n = stmt.executeQuery(“UPDATE foo SET bar = 1 WHERE snafu = ‘tarfu’”) n will equal the number of rows affected  DDL statements always return 0

12 Intermediate JDBC Prepared Statements Similar to normal statements, but query is supplied during creation, and can contain parameter values Transactions For processing all or nothing batch updates Stored Procedures For calling procedures which exist in the DBMS rather than supplied in code

13 Summary JDBC is very similar to working with databases in.NET or other OO and OB languages Syntax is most difficult thing Remember to catch or throw errors


Download ppt "Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D."

Similar presentations


Ads by Google