JDBC – Java Database Connectivity

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

Database programming in Java An introduction to Java Database Connectivity (JDBC)
JDBC CS-328. JDBC Java API for accessing RDBMS Allows use of SQL for RDBMS programming Can be used for: –embedded SQL –execution of stored queries.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
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.
מסדי נתונים תשס " ג 1 JDBC Java Database Connectivity קורס מסדי נתונים.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 Sub-queries and Views. 2 A Complex Query We would like to create a table containing 3 columns: –Sailor id –Sailor age –Age of the oldest Sailor How.
1 JDBC – Java Database Connectivity Representation and Management of Data on the Internet.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
1 Triggers. 2 PL/SQL reminder We presented PL/SQL- a Procedural extension to the SQL language. We reviewed the structure of an anonymous PL/SQL block:
1 JDBC "Java Database Connectivity". 2 Getting Started Guide: etstart/GettingStartedTOC.fm.html java.sql.
Web Application Development Muhammad Ali Versonic Pte Asher Imtiaz Forman Christian College.
Training - Day 3 OJB. What is OR Mapping? OR Mapping is the mapping of relational database tables to objects (Java Objects in our case) Many OR Mapping.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
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.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
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.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Chapter 8 Databases.
JDBC – Java Database Concentricity
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
JDBC (Java Database Connectivity)
CS122B: Projects in Databases and Web Applications Winter 2016
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.
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC 2 Getting Started Guide: etstart/GettingStartedTOC.fm.html java.sql Package API:
1 JDBC – Java Database Connectivity THETOPPERSWAY.COM.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Java Database Connectivity: JDBC
CompSci 280 S Introduction to Software Development
DB Apps Introduction SoftUni Team Technical Trainers
CS3220 Web and Internet Programming Database Access with JDBC
JDBC 15-Apr-18.
JDBC
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
How to connect natively?
Database JDBC Overview CS Programming Languages for Web Applications
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Part 4 FaaDoOEngineers.com IBM.
Design and Implementation of Software for the Web
Database Management Systems
JDBC 15-Nov-18.
Interacting with Database
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
JDBC II IS
Presentation transcript:

JDBC – Java Database Connectivity

Querying with PreparedStatement String queryStr = "SELECT * FROM employee " + "WHERE superssn= ? and salary > ?"; PreparedStatement pstmt = con.prepareStatement(queryStr); pstmt.setString(1, "333445555"); pstmt.setInt(2, 26000); ResultSet rs = pstmt.executeQuery();

Statements vs. PreparedStatements: Be Careful! Are these the same? What do they do? String val = "abc"; PreparedStatement pstmt = con.prepareStatement("select * from R where A=?"); pstmt.setString(1, val); ResultSet rs = pstmt.executeQuery(); String val = "abc"; Statement stmt = con.createStatement( ); ResultSet rs = stmt.executeQuery("select * from R where A=" + val);

ResultSet ResultSet objects provide access to the tables generated as results of executing a Statement queries Only one ResultSet per Statement can be open at the same time! The table rows are retrieved in sequence A ResultSet maintains a cursor pointing to its current row The next() method moves the cursor to the next row

executeQuery() no assumption is made on the validity of the query ResultSet executeQuery(String sql) Executes the given SQL statement, which returns a single ResultSet object no assumption is made on the validity of the query if the SQL execute successfully it returns a ResultSet object containing rows from the database if the SQL fails it will raise a SQLException

Executing a Statement - Example ResultSet rs = stmt.executeQuery(“select name from pets”); ResultSet: Initial cursor position Fluffy Claws Buffy Fang Chirpy Whistler Slim Puffball next() next()

ResultSet Object A table of data representing a database result set maintains a cursor pointing to its current row of data Initially the cursor is positioned before the first row The next() method moves the cursor to the next row next() returns false when there are no more rows in the ResultSet object A default ResultSet object is not updatable and has a cursor that moves forward only table of data representing a database result set, which is usually generated by executing a statement that queries the database. A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable.

Moving Through the ResultSet -Example while (rs.next()) { System.out.println(rs.getString((1))); } Fluffy Claws Buffy Fang Chirpy Whistler Slim Puffball

Basic Getter Methods int getInt(int columnIndex) int getInt(String columnName) String getString(int columnIndex) String getString(String columnName) Date getDate(int columnIndex) Date getDate(String columnName)

ResultSet Methods boolean next() void close() activates the next row the first call to next() activates the first row returns false if there are no more rows void close() disposes of the ResultSet allows you to re-use the Statement that created it automatically called by most Statement methods

ResultSet Methods Type getType(int columnIndex) returns the given field as the given type indices start at 1 and not 0! Type getType(String columnName) same, but uses name of field less efficient For example: getString(columnIndex), getInt(columnName), getTime, getBoolean, getType,... int findColumn(String columnName) looks up column index given column name

ResultSet Methods JDBC 2.0 includes scrollable result sets. Additional methods included are : ‘first’, ‘last’, ‘previous’, and other methods.

ResultSet Example Statement stmt = con.createStatement(); ResultSet rs = stmt. executeQuery("select lname,salary from Employees");     // Print the result while(rs.next()) {  System.out.print(rs.getString(1) + ":");  System.out.println(rs.getDouble(“salary")); }

Mapping Java Types to SQL Types SQL type Java Type CHAR, VARCHAR, LONGVARCHAR String NUMERIC, DECIMAL java.math.BigDecimal BIT boolean TINYINT byte SMALLINT short INTEGER int BIGINT long REAL float FLOAT, DOUBLE double BINARY, VARBINARY, LONGVARBINARY byte[] DATE java.sql.Date TIME java.sql.Time TIMESTAMP java.sql.Timestamp

Cleaning Up After Yourself Remember to close the Connections, Statements, Prepared Statements and Result Sets con.close(); stmt.close(); pstmt.close(); rs.close()

DEMO