Database applications with JDBC Jiafan Zhou. DBMS Database management systems (DBMS) is an organised collection of data. Usually database stores data.

Slides:



Advertisements
Similar presentations
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
Advertisements

Distributed Application Development B. Ramamurthy.
Fundamentals, Design, and Implementation, 9/e Chapter 14 JDBC, Java Server Pages, and MySQL.
1 JDBC Java Database Connectivity. 2 c.pdf
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
1 Session 2 Module 3: Scrollable Resultset and Rowsets.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC (Java Database Connectivity)
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 JDBC – Java Database Connectivity CS , Spring 2010.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC.
1. Writing a Java program to connect to SQL Server 2008 and Create a table Populate the table (insert data) Perform queries to retrieve information from.
JSP and DB.
Chapter 38 Advanced Java Database Programming
Web Technologies IT230 Dr Mohamed Habib.
Chapter 41 Advanced Java Database Programming
Advanced Web Automation Using Selenium
HW#4 Making Simple BBS Using JDBC
Objectives In this lesson, you will learn about:
Interacting with Database
Database applications with Java
JDBC Example.
Presentation transcript:

Database applications with JDBC Jiafan Zhou

DBMS Database management systems (DBMS) is an organised collection of data. Usually database stores data on hard drives, so data will not get lost after a system reboot. SQL is the language program interacts with the database to perform the CRUD operations (Create, Read, Update and Delete) There are many database products, open source or commercial, available in the market place. Just to name a few:  Oracle  SQL Server  DB2  Sybase  MySQL  PostgreSQL  Apache Derby (Java DB)

Java DB In this course, we will use the Java DB, which comes with the latest version of the Java SDK. (Installed in the db directory of the JDK installation) Java DB is Oracle’s supported distribution of the Apache Derby open source database. Java DB supports standard ANSI/ISO SQL through the JDBC. To start Java DB: In Windows: c:\Java\jdk1.7.0_25\db\bin> startNetworkServer.bat In Unix/Linux: Tue Sep 17 14:35:28 BST 2013 : Security manager installed using the Basic server security policy. Tue Sep 17 14:35:31 BST 2013 : Apache Derby Network Server ( ) started and ready to accept connections on port 1527 By default, the database server starts on the listening port To stop Java DB: (Enter ctrl c) Terminate batch job (Y/N)? y

Create table in Java DB Let us first connect to the Java DB using ij (the Java DB tool), create a table and add some data into the table. Open another cmd or terminal: c:\Java\jdk1.7.0_25\db\bin> ij ij version 10.8 ij> connect 'jdbc:derby://localhost:1527/c:\temp\db;create=true'; ij> create table students(id integer, name varchar(20), age integer, city varchar(50)); ij> insert into students values(1, 'david', 22, 'Dublin'); 1 row inserted/updated/deleted ij> insert into students values(2, 'peter', 25, 'Galway'); 1 row inserted/updated/deleted ij> insert into students values(3, 'frank', 28, 'Limerick'); 1 row inserted/updated/deleted ij> select * from students; ID |NAME |AGE |CITY |david |22 |Dublin 2 |peter |25 |Galway 3 |frank |28 |Limerick 3 rows selected

Connect to Java DB using ij Similar to create the database, but without the ‘create=true’ clause All the data is saved in the hard drive in c:\temp\db Open another cmd or terminal: c:\Java\jdk1.7.0_25\db\bin> ij ij version 10.8 ij> connect 'jdbc:derby://localhost:1527/c:\temp\db'; ij> select * from students; ID |NAME |AGE |CITY |david |22 |Dublin 2 |peter |25 |Galway 3 |frank |28 |Limerick 3 rows selected

JDBC driver Java DB already comes with a JDBC driver. If using other database, then a JDBC driver must be downloaded/installed first in order for Java program to connect to the database, and the driver must be registered in the code e.g. For MySQL database, the JDBC driver must be registered first: Class.fornName(“com.mysql.jbdc.Driver”); Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”, “username”, “pasword”); Check Internet to obtain a proper JDBC driver for your DBMS. In Java DB, there is no need to register JDBC driver, it is already included.

JDBC driver

JDBC API The Java Database Connectivity API (JDBC) provides a universal data access from the Java programming language. Using the JDBC API, we can access any data source, from relational database to excel spreadsheet or even flat files. The JDBC API is comprised of the following two packages: java.sql javax.sql A JDBC driver is needed to mediate between JDBC technology and the database.

Process SQL with JDBC In general, to process SQL statement with JDBC, the steps as below are needed: 1. Establish a db connection To establish a connection with the data source, which can be a DBMS with a corresponding JDBC driver. 2. Create an SQL statement A statement is an interface that represents an SQL statement. 3. Execute the statement To execute an SQL statement and it generates results. 4. Process the ResultSet Object if required A table of data representing a database result set. 5. Close the connection Release the resources it is using.

1. Establish a db connection First is to establish a connection with the data source. In order to include the derby JDBC driver, it is necessary to include the $JAVA_HOME/db/lib/derbyclient.jar in the project classpath. Typically, a JDBC application connects to a target data source using one of the following two classes:  DriverManager Connects an application to a data source, which is specified by a database URL. A database URL is a string that the JDBC driver uses to connect to a database. When this class first attempts to establish a connection, it automatically loads any JDBC drivers found within the class path. Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/c:/temp/db");  DataSource // alternatively, it is possible to use DataSource to get db connection // notice ClientDataSource is a subtype of DataSource ClientDataSource ds = new ClientDataSource(); ds.setPortNumber(1527); ds.setServerName("localhost"); ds.setDatabaseName("c:/temp/db"); Connection conn = ds.getConnection();

2. Create an SQL statement A statement is an interface that represents an SQL statement. The program executes the Statement object, generating a ResultSet objects, which is a table of data representing a database result set. Need a Connection object to create a Statement object Statement stmt = conn.createStatement(); There are three different kinds of statements:  Statement Used for simple SQL statements without parameters.  PreparedStatement Used for statements that might contain input parameters.  CallableStatement Used to execute stored procedures that may contain input/output parameters.

3. Execute the statement To execute a query, simply call an execute method from the Statement String sql = "select * from students"; ResultSet rs = stmt.executeQuery(sql); There are three available execute methods can be used:  execute() Returns true if the first object that the query returns is a ResultSet. Use this method if the query could return one or more ResultSet objects.  executeQuery() Returns the ResultSet object.  executeUpdate() Returns an integer representing the number of rows affected by the SQL statement. (Use this method if using with INSERT, DELETE or UPDATE SQL)

4. Process the ResultSet Object Access the data in a ResultSet object through a cursor. The cursor is a pointer that points to one row of data in the ResultSet object Initially, the cursor is positioned before the first row Call the next() to move the cursor forward by one row. while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String city = rs.getString("city"); System.out.println(id + "\t" + name + "\t" + age + "\t" + city); } When reaching the line row of the ResultSet object, the next() will return a false.

5. Close the connection When finished using a Statement, call the method Statement.close() to immediately release the resources it is using. When this method is called, the ResultSet objects are closed. Alternatively, it is also possible to close the database connection. finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } Note: In Java 7, it is possible to use the try-with resources statement to automatically close the Statement or database connection.

StudentDaoDemo.java 1.public class StudentDaoDemo { 2. public static void viewStudents(Connection conn) { 3. String sql = "select * from students"; 4. try { 5. Statement stmt = conn.createStatement(); 6. ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { 9. int id = rs.getInt("id"); 10. String name = rs.getString("name"); 11. int age = rs.getInt("age"); 12. String city = rs.getString("city"); 13. System.out.println(id + "\t" + name + "\t" + age + "\t" + city); 14. } 15. } 16. catch (SQLException ex) { 17. ex.printStackTrace(); 18. } 19. finally { 20. try { 21. conn.close(); 22. } 23. catch (SQLException e) { 24. e.printStackTrace(); 25. } 26. } 27. } 28. public static void main(String[] args) throws Exception { 29. Connection conn = DriverManager 30..getConnection("jdbc:derby://localhost:1527/c:/temp/db"); 31. viewStudents(conn); 32. } 33.}

RowSet Compared with ResultSet, JDBC RowSet object holds tabular data in a way that makes it more flexible and easier to use. For some more popular uses of a RowSet, RowSet has sub interfaces. Programmers are free to write their own versions of the RowSet interface and the implementations. But many programmers will probably find the existing RowSet interfaces already fit their need.

RowSet Features RowSet is derived from the ResultSet and therefore share its capability. RowSet extended the ResultSet in the following two ways:  Function as Javabeans For all RowSet objects, three events trigger notifications. Components that have implemented the RowSetListener interface will be noticed when any of the three events occurs: 1. A cursor movement 2. The update, insertion or deletion of a row 3. A change to the entire RowSet contents  Scrollability or Updatability A RowSet object is scrollable and updatable by default.

RowSet Types A RowSet object is considered either connected or disconnected. A connected RowSet object uses a JDBC driver to make a connection to a relational database and maintains the connection.  JdbcRowSet A disconnected RowSet object makes a connection to a data source only when read/write data to the data source is needed.  CachedRowSet  WebRowSet  JoinRowSet  FilteredRowSet In this course, we will only discuss the connected RowSet, i.e. the JdbcRowSet. Check the documentations for other types of disconnected RowSet.

JdbcRowSet Basics A JdbcRowSet is an enhanced ResultSet object, and maintains the connection to the data source. It has a set of properties and a listener notification mechanism. To make a ResultSet object scrollable and updatable. There are many ways to create a JdbcRowSet object:  Invoke the JdbcRowSetImpl constructor that takes a ResultSet object Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); JdbcRowSet rowSet = new JdbcRowSetImpl(rs);  Invoke the JdbcRowSetImpl constructor that takes a Connection object Connection conn = …; JdbcRowSet rowSet = new JdbcRowSetImpl(conn);  Invoke the JdbcRowSetImpl default constructor JdbcRowSet rowSet = new JdbcRowSetImpl();  Using an instance of RowSetFactory, which is created from the class RowSetProvier RowSetFactory myRowSetFactory = RowSetProvider.newFactory(); JdbcRowSet rowSet = myRowSetFactory.createJdbcRowSet();

Using JdbcRowSet The command property must be set which is the query that determines what data the JdbcRowSet object will hold. rowSet.setCommand("select * from students"); After setting the command property, invoke the execute() method: rowSet.execute(); To add listeners: rowSet.addRowSetListener(new public void rowSetChanged(RowSetEvent event){ System.err.println("RowSet changed..."); public void rowChanged(RowSetEvent event){ System.err.println("Row changed... "); public void cursorMoved(RowSetEvent event){ System.err.println("Cursor moved..."); } });

Navigating JdbcRowSet A ResultSet can only use the next() method to move the cursor forward. A JdbcRowSet can use all of the cursor movement methods defined in the ResultSet interface. The previous() method move the cursor to the previous row The absolute(int n) method moves the cursor directly to the n row // get the third row rowSet.absolute(3); displayRow(rowSet); // then get the previous row, i.e. the second row rowSet.previous(); displayRow(rowSet); JdbcRowSetNavigate.java example.

Insert row JdbcRowSet JdbcRowSet is updatable, inserting a row involves moving the cursor to the insert row, use the appropriate updater method to set value for each column and call the insertRow() method. rowSet.moveToInsertRow(); rowSet.updateInt("id", 4); rowSet.updateString("name", "enda"); rowSet.updateInt("age", 35); rowSet.updateString("city", "moat"); rowSet.insertRow(); JdbcRowSetInsert.java example

Update/Delete row JdbcRowSet JdbcRowSet is updatable, deleting a row involves moving the cursor to the last row, and call the deleteRow() method. rowSet.last(); rowSet.deleteRow(); JdbcRowSet is updatable, updating a row involves moving the cursor to the target row, use the appropriate updater method to update value for each column and call the updateRow() method. rowSet.first(); rowSet.updateString("name", "jason"); rowSet.updateRow(); JdbcRowSetDelete.java example JdbcRowSetUpdate.java example

Prepared Statements PreparedStatement objects can be used most often for SQL statements that take parameters. The advantage of using parameters is that you can use the same statement and supply it with different values each time it is executed it. Creating a PreparedStatement Object: String sql = "update students set name = ? where id = ?"; PreparedStatement stmt = conn.prepareStatement(sql); Supplying values for PreparedStatement parameters: stmt.setString(1, name); stmt.setInt(2, id); Executing PreparedStatement Objects int rowsAffected = stmt.executeUpdate(); PreparedStatementDemo.java

Transactions There are many cases that several database statements need to take effect at the same time. One statement needs to take effect until another one completes. For instance, a number of students need to be registered in the database students table. These students come as a group from another country. The registration require adding all students into the database in one go. The purpose is to ensure data integrity. The way to ensure that either multiple actions occur or none action occurs is to be said a database transaction. A database transaction is a set of one or more statements that is executed as a unit, so either all of the statements are executed, or none of the statements is executed.

Transactions – Auto Commit By default, when a connection is created, the auto-commit mode is enabled. Auto commit means that each individual SQL statement is treated separately as a transaction, and is automatically committed after it is executed. The way to allow two or more statements to be grouped into a transaction is to disable the default auto-commit mode. conn.setAutoCommit(false); After disabling the auto commit mode, no SQL statements are committed until you explicitly call the method commit and committed together as a unit. conn.commit(); Don’t forget to get the auto commit mode back to true when the transaction is completed. It is advisable to disable the auto-commit mode only during the transaction mode. AutoCommitModeDemo.java TransactionFailedDemo.java

Transactions – locks Transactions can also help to preserve the integrity of the data in a table. Transaction provides some level of protection against conflicts that arise when two users access data at the same time. e.g. What if one user changes a student’s name while another one view the student’s name. To avoid conflicts during a transaction, a DBMS uses locks, mechanisms for blocking access by others to the data that is being accessed by the transaction. How locks are set is determined by what is called a transaction isolation level, which can range from not supporting transactions at all to supporting the strict access rules transactions. In order to understand this concept, we need to understand different types of reads in the database transaction.

Transactions – Reads There are basically three types of reads (hazards) in transactions that can occur. Notice if there is no transaction, there are no reads. Dirty Reads Occurs when transaction A retrieves a row, transaction B subsequently updates the row. The data retrieved by transaction A is different from the data in database. Non-repeatable Reads Occurs when transaction A retrieves a row. Transaction B subsequently updates the row, and transaction A later retrieves the same row again. Transaction A retrieves the same row twice but sees different data. Phantom Reads Occurs when transaction A retrieves a set of rows satisfying a given condition, transaction B subsequently inserts or updates a row such that the row now meets the condition in transaction A, and transaction A later repeats the conditional retrieval. Transaction A now sees an additional row. This row is referred to as a phantom.

Transactions – Isolation Level The JDBC has five different values that represent the transaction isolation levels Isolation LevelTransactionsDirty Reads Non- repeatable Reads Phantom Reads TRANSACTION_NONENot supported TRANSACTION_READ_ COMMITTED SupportedPreventedAllowed TRANSACTION_READ_ UNCOMMITTED SupportedAllowed TRANSACTION_REPEA TABLE_READ SupportedPrevented Allowed TRANSACTION_SERIA LIZABLE SupportedPrevented

Transactions – Isolation Level Usually, you don’t need to worry about the transaction isolation level, you can just use the default one for your DBMS. Different DBMS has different default transaction isolation level. JDBC driver might not support all transaction isolation levels. If a driver does not support the isolation level specified in an invocation of setTransactionIsolation, the driver can substitute a higher, more restrictive transaction isolation level. The default transaction level for Java DB is TRANSACTION_READ_COMMITED. TransactionIsolationLevelDemo.java

Advanced Data Advanced data types give a relational database more flexibility in what can be used as a value for a table column. For example, a column can be used to store BLOB (binary large object) values, which can store very large amounts of data as raw bytes. Or CLOB (character large objects), which is capable of storing very large amounts of data in character format. You retrieve, store and update advanced data types the same way you handle other data types. You can use either ResultSet.getDataType or CallableStatement.getDataType methods to retrieve them. The following table shows which methods to use

Advanced Data Advanced Data Type getDataType Method setDataType method updateDataType Method BLOBgetBlobsetBlobupdateBlob CLOBgetClobsetClobupdateClob NCLOBgetNClobsetNClobupdateNClob ARRAYgetArraysetArrayupdateArray XMLgetSQLXMLsetSQLXMLupdateSQLXML Structured typegetObjectsetObjectupdateObject REF(structured type) getRefsetRefupdateRef ROWIDgetRowIdsetRowIdupdateRowId DISTINCTgetBigDecimalsetBigDecimalupdateBigDecimal DATALINKgetURLsetURLupdateURL

Using Stored Procedures Stored procedures are supported by most database management systems (DBMS), but differs large in their syntax and capability. A stored procedure is a group of SQL statements that form a logical unit and perform a particular task. For example, operations related to employee database (hire, fire, promote, lookup) could be coded as stored procedures executed. CallableStatement is used to execute stored procedures that may contain input/output parameters.

Homework

Update your banking system to persist data into the database. All the user data should be persisted. Implement some user interface to retrieve the persisted data. Peek at some ORM solutions like Hibernate.

Questions & Answers

Mock Exam Questions