Web Design & Development 1 Lec - 16. Web Design & Development 2 More on JDBC.

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

1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 PostgreSQL JDBC interface documentation
Object-Oriented Enterprise Application Development Advanced JDBC.
1 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 12: JDBC.
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
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.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
1 JDBC – Java Database Connectivity Modified slides from Dr. Yehoshua Sagiv.
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.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
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.
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.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
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.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.
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.
DAT602 Database Application Development Lecture 9 Advanced JDBC 2.
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.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
1 Session 2 Module 3: Scrollable Resultset and Rowsets.
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.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC (Java Database Connectivity)
Adv Java Chapter 1.JDBC Chapter 2.Servlets Chapter 3.JSP.
JDBC Java DataBase Connectivity. Loading the driver import java.sql.*... Class.forName("org.postgresql.Driver")‏
JDBC - Resultset The java.sql.ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
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.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
CS3220 Web and Internet Programming Database Access with JDBC
Lec - 14.
JDBC – Java Database Connectivity
JDBC III IS
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Database Management Systems
Client Access, Queries, Stored Procedures, JDBC
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – Java Database Connectivity
Database Programming Using JDBC and MySQL C API
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Web Design & Development 1 Lec - 16

Web Design & Development 2 More on JDBC

Web Design & Development 3 Result Set

Umair Javed©2005 JDBC4 ResultSet Overview –A ResultSet contains the results of the SQL query Represented by a table with rows and columns Maintains a cursor pointing to its current row of data. Initially the cursor positioned before the row (0). First row has index 1

Umair Javed©2005 JDBC5 ResultSet (cont.) ResultSet id Address model town gulberg defence Name ali usman raza phoneNum Row numbers

Umair Javed©2005 JDBC6 ResultSet (cont.) A default ResultSet object is not updateable and has a cursor that moves forward only –You can iterate through it only once and only from the first row to last row. String sql = “SELECT * FROM Person”; PreparedStatement pStmt = con.prepareStatement(sql ); ResultSet rs = pStmt.executeQuery( );

Umair Javed©2005 JDBC7 ResultSet (cont.) Useful Methods –next( ) Attempts to move to the next row in the ResultSet If successful true is returned; otherwise, false The first call to next, moves the cursor to the first row –close( ) Releases the JDBC and database resources The result set is automatically closed when the associated Statement object executes a new query or closed by method call

Umair Javed©2005 JDBC8 ResultSet (cont.) Useful Methods –getters Returns the value from the column specified by the column name or index –String name = rs.getString(“name”); –String add = rs.getString(3); –double sal = rs.getDouble(“Salary”) Returns the value in a specified format double byte int Date String float short long Time Object

Umair Javed©2005 JDBC9 ResultSet (cont.) It is possible to produce ResultSet objects that are scrollable and/or updatable (since JDK 1.2). String sql = “SELECT * FROM Person”; PreparedStatement pStmt = con.prepareStatement( sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); ResultSet rs = pStmt.executeQuery( );

Umair Javed©2005 JDBC10 ResultSet (cont.) Useful Methods –previous( ) Moves the cursor to the previous row in the ResultSet object. Returns true if cursor is on a valid row, false it is off the result set. Throws exception if result type is TYPE _ FORWARD _ ONLY.

Umair Javed©2005 JDBC11 Example Code: ResultSetEx previous, next & getters methods import java.sql.*; public class ResultSetEx { public static void main ( String args[ ]) { try { // load driver & make connection String sql = “ SELECT * FROM Person”; PreparedStatement pStmt = con.prepareStatement( sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); ResultSet rs = pStmt.executeQuery( );

Umair Javed©2005 JDBC12 Example Code: ResultSetEx previous, next & getters methods rs.next(); System.out.println(“moving cursor forward”); String name = rs.getString(“name”); System.out.println(name); rs.next(); rs.previous(); System.out.println(“moving cursor backward”); name = rs.getString(“name”); System.out.println(name);

Umair Javed©2005 JDBC13 Example Code: ResultSetEx previous, next & getters methods con.close(); } catch (Exception ex) { System.out.println(ex); } }// end main }//end class

Umair Javed©2005 JDBC14 Compile & Execute

Umair Javed©2005 JDBC15 ResultSet (cont.) Useful Methods –absolute(int) Move the cursor to the given row number in the ResultSet object If the row number is positive, moves the cursor forward with respect to beginning of the result set. If the given row number is negative, the cursor moves to the absolute row position with respect to the end of the result set. –For example, calling absolute(-1) positions the cursor on the last row; calling absolute(-2) moves the cursor to next-to-last row, and so on. Throws exception if result type is TYPE _ FORWARD _ ONLY.

Umair Javed©2005 JDBC16 ResultSet (cont.) Useful Methods –updaters (for primitives, Object & String) Used to update column values in the current row or the insert row. Do not update the underlying database Each update method is overloaded. For example of String updateString(String columnName, String value) updateString(int columnIndex, String value)

Umair Javed©2005 JDBC17 ResultSet (cont.) Useful Methods –updateRow( ) Updates the underlying database with new contents of the current row of this ResultSet object.

Umair Javed©2005 JDBC18 Modify Example : ResultSetEx updating existing rows Import java.sql.*; public class ResultSetEx { ….. // main method ….. Load driver, make connection ….. Make updatable resultset //move cursor to 2 nd row of rs rs.absolute(2); //update address column of 2 nd row in rs rs.updateString(“address”, “model town’); //update the row in database rs.updateRow(); ………. // close connection etc …. //end main }// end class

Umair Javed©2005 JDBC19 Compile & Execute Before execution After execution

Umair Javed©2005 JDBC20 ResultSet (cont.) Useful Methods –moveToInsertRow( ) An Updatable ResultSet object has a special row associated with it i.e. insert row. Insert row – a buffer, where a new row may be construted by calling the updater methods Doesn’t insert row into a result set or into a database

Umair Javed©2005 JDBC21 ResultSet (cont.) Useful Methods –insertRow( ) Inserts the contents of the insert row into this ResultSet object and into the database. The cursor must be on the insert row when this method is called

Umair Javed©2005 JDBC22 ResultSet (cont.) Updatable ResultSet id Address model town gulberg defence Name ali usman raza phoneNum Row numbers Insert Row imitiaz cantt moveToInsertRow( )

Umair Javed©2005 JDBC23 ResultSet (cont.) Updatable ResultSet id Address model town gulberg defence Name ali usman raza phoneNum Row numbers Insert Row 4 imitiaz cantt insertRow( )

Umair Javed©2005 JDBC24 Modify Example : ResultSetEx Inserting new row Import java.sql.*; public class ResultSetEx { ….. // main method ….. Load driver, make connection ….. Make updatable resultset //move cursor to insert row rs.moveToInsertRow(); // updating values into insert row rs.updateString(“name”, “imitaz’); rs.updateString(“address”, “cantt’); rs.updateString(“phoneNum”, “ ’); //insert row into rs & db rs.insertRow(); ………. …. //end main }// end class

Umair Javed©2005 JDBC25 Compile & Execute Before execution After execution

Umair Javed©2005 JDBC26 ResultSet (cont.) Useful Methods –last( ) & first( ) Moves the cursor to the last & first row of the ResultSet object respectively. Throws exception if the result set is TYPE_FORWARD_ONLY –getRow( ) Returns the current row numner The first row number is 1, second row number is 2 and so on

Umair Javed©2005 JDBC27 ResultSet (cont.) Useful Methods –deleteRow( ) Deletes the current row from this ResultSet object and from the underlying database. Throws exception when the cursor is on the insert row

Umair Javed©2005 JDBC28 Modify Example : ResultSetEx deleting existing row Import java.sql.*; public class ResultSetEx { ….. // main method ….. Load driver, make connection ….. Make updatable resultset //moves to last row rs.last( ); int rNo = rs.getRow(); System.out.println(“curr row no: ”+ rNo ); //delete current row (4) from rs & db rs.deleteRow(); ………. …. //end main }// end class

Umair Javed©2005 JDBC29 Compile & Execute Before execution After execution