Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.

Similar presentations


Presentation on theme: "Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson."— Presentation transcript:

1 Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson

2 Last Time URLs A reminder of Sockets. Threads: running processes at the same time on the same computer. They can be tricky to use.

3 This lecture Relational databases Accessing relational databases from Java using JDBC and SQL Manipulating database structure from Java

4 References Java JDBC Documentation http://java.sun.com/javase/6/docs/technotes/guides/jdbc/ http://java.sun.com/javase/6/docs/technotes/guides/jdbc/ JDBC Basics Tutorial http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html JDBC Advanced Tutorial http://java.sun.com/developer/Books/JDBCTutorial/index.html http://java.sun.com/developer/Books/JDBCTutorial/index.html mySQL Connector http://dev.mysql.com/downloads/connector/j/3.1.html http://dev.mysql.com/downloads/connector/j/3.1.html Cay Horstmann (2008) Big Java, 3 rd Edition. Wiley.

5 What is a Relational Database?

6 Tables and Relationships Riders Teams Events A rider is a member of a team. A team comprises many riders. A rider competes in an event. A event has many riders competing. A table contains many records. Each record has a unique primary key. Tables are linked using the keys.

7 Accessing a book If I want to read a book, what do I need to do? What do you need to do to access a file? Do these same ideas apply to a database?

8 To access a relational database Need to know where the database is Need to know the structure of the database – What tables – What columns and keys Use SQL to form queries and to manipulate data

9 How do you open (connect) to a database? JDBC Database Access

10 Accessing a database What do we need to be able to do to access a database from our program code? Obtain access (i.e. Open the database) Read data from the database Write data to the database Relinquish access (i.e. Close the database) Have transaction control Group a set of updates Rollback changes if errors occur

11 Linking to database driver Set the class path to find the driver Database Driver Where do you find driver names? Should be in database vendor documentation For driver in assignment: “com.mysql.jdbc.Driver” or"org.gjt.mm.mysql.Driver" Database URL How is a database URL constructed? “jdbc: : For mySQL: “jdbc:mysql:// / ” There are variations

12 Java code Loading the driver class Class.forName(" com.mysql.jdbc.Driver "); Get a connection object (open the database connection Connection con = DriverManager.getConnection(url, username, password); Use try {…} catch {…} around these Methods available on connection object con.isClosed() returns true if connection is closed con.close() closes the connection

13 How do you obtain data from a database? JDBC Database Access

14 Building and executing a query SQL Select statement – Simple form Select from where Create a statement Statement =.createStatement(); PreparedStatement =.prepareStatement( );.set (, ); Execute query ResultSet =.executeQuery( ); Remember to use try {…} catch {…}

15 Retrieving data from result set Stepping through data records.next(); Retrieving individual fields =.get ( ); dataType can be int, double, String, Date, etc. Close the result set.close();

16 How can you write data to a database? JDBC Database Access

17 SQL commands To add data Insert Into ( ) Values ( ) To modify data Update Set Where

18 JDBC statement execution Prepare as for SQL Select Execute int =.executeUpdate( ); Returns number of records changed.execute( ) Returns true if a result set has been created by SQL statement.getResultSet() Returns result set.getUpdateCount() Returns the number of records updated

19 How do I handle sets of data from a database in my program? Scrollable and updatable result sets

20 Changes to the createStatement Additional options on the createStatement and prepareStatement Type TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE Concurrency CONCUR_READ_ONLY CONCUR_UPDATEABLE

21 Scrollable result set Step forward and backwards rs.next() rs.previous() rs.relative(n) Skip to a specific row rs.absolute() rs.getRow() Other methods first, last, beforeFirst, afterLast isFirst, isLast, isBeforeFirst, isBeforeLast

22 Updateable result set Must create or prepare statement with CONCUR_UPDATABLE Able to update field values updateXxx(column, value) updateRow() Able to add rows moveToInsertRow() updateXxx(column, value) insertRow() moveToCurrentRow() Able to delete rows deleteRow()

23 How do I ensure that no other program is updating the same data as my program? Transaction control

24 What is the issue? Should we treat a group of database updates as though they were one update? Purchase transaction updates Product inventory Sales history Customer transaction history What constitutes a complete transaction?

25 Implementation of transaction control Switch off automatic commit conn.setAutoCommit(false); Update the result set Commit the changes conn.commit(); Oops! Something has gone wrong. conn.rollback();

26 Why is transaction control associated with the connection and not the statement?

27 Batch updates Improves the performance of update operations stat.addBatch(command); stat.executeBatch(); Might also treat the batch as a single transaction

28 Can I access information about the structure of the database? Metadata

29 What is Metadata Data about the structure of the database What tables are in the database? How many columns are in a table? What columns are in the tables? What are the data types of the columns? Metadata is held in a result set like conventional data

30 SQL data management statements All that are supported by the database software in use. Includes Create Database Use Create Table Create Relationships Modify Table Drop table Drop Database

31 Conclusion Reviewed the nature of a relational database Connection to a database using JDBC Retrieving data using SQL and JDBC Using SQL to update the database Using a result set to update the database Database metadata using SQL Using SQL commands to modify the database structure

32 Next Time Servlets


Download ppt "Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson."

Similar presentations


Ads by Google