Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.

Slides:



Advertisements
Similar presentations
JDBC – Java DataBase Connectivity CSE432 Object Oriented Software Engineering.
Advertisements

Database programming in Java An introduction to Java Database Connectivity (JDBC)
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 PostgreSQL JDBC interface documentation
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.
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
Programming for Geographical Information Analysis: Advanced Skills Lecture 5: Database connectivity Dr Andy Evans.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
Chapter 121 Adding Power to Database Access with JSP JavaServer Pages By Xue Bai.
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
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.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
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 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.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
DAT602 Database Application Development Lecture 9 Advanced JDBC 2.
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.
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.
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.
Connecting to MySQL using Java By:. – Required to use Java.sql so that we can use Connection and Queries using strings. – Javax.swing.* needed for components.
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)
Adv Java Chapter 1.JDBC Chapter 2.Servlets Chapter 3.JSP.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Web Programming Assistant Professor Xiaozhong Liu
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.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
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.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
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.
JDBC IV IS Outline  Batch processing  Transactions  Homework #2  Examples.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
JDBC – Java DataBase Connectivity
Course Outcomes of Advanced Java Programming AJP (17625, C603)
JDBC – Java Database Connectivity
JDBC III IS
Web Technologies IT230 Dr Mohamed Habib.
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Design and Implementation of Software for the Web
JDBC – Java DataBase Connectivity
Database Management Systems
JDBC – Java DataBase Connectivity
Introduction to Server-Side Web Development using JSP and Databases
JDBC – Java Database Connectivity
JDBC – Java DataBase Connectivity
Presentation transcript:

Databases: Queries with Java Dr Andy Evans

JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that are altered to new data through input parameters. Useful, for example, in looped structures. CallableStatements: for SQL procedures stored in the database. Produce ResultSet objects.

Example: Select Statement st = con.createStatement(); ResultSet rs = st.executeQuery ("SELECT a,b,c FROM Table1"); Three different execution methods: executeQuery : simple SQL queries. executeUpdate : anything that changes or creates a Table, e.g. UPDATE. Returns number of rows effected. execute : Complex, multi-return queries. st.close(); polite!

Example: Creating tables String createTable = "CREATE TABLE Results (" + "Address varchar(255)," + "Burglaries int" + ")"; Statement st = null; try { st = conn.createStatement(); st.execute (createTable); } catch (SQLException ex) { ex.printStackTrace(); }

Auto-commit You can run multiple queries on the same statement. By default Connection objects are set to auto-commit – all changes are solidified after single statements are run. conn.setAutoCommit(booleanAutoCommit); If set to false (off) the changes will only be solidified when commit called: conn.commit(); Until then you can rollback all changes since the last commit, by calling: conn.rollback(); Also options to setup and rollback to savepoints.

Escape characters Remember that some characters, like “ _ ”, are wildcards. If we want to use these literally, they need a backslash infront of them “ \_ ”. However, backslash is a String escape character in Java, so you can define what is a special escape character to ignore in statement execution: stmt.executeQuery( "SELECT name FROM Table1 WHERE Id LIKE '\_%' {escape '\'}"); In some cases the escape occurs in a table name, in which case treat literally by enclosing in [] e.g. [Sheet1$]

ResultSets Links to a cursor on the database and converts data to Java types. ResultSet rs = st.executeQuery ("SELECT a, b, c FROM Table1"); while (rs.next()) { int i = rs.getInt("a"); or String s = rs.getString("b"); or Object o = rs.getObject("c"); } Use object if unsure, and cast. A new SQL query will close the current ResultsSet. Can also use column index number (starting 1).

ResultSets Standard results sets only let you read from beginning to end a line at a time. If you want to write to the data (without using SQL UPDATE) or go back and forwards, you need a scrollable statement. Statement st = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.HOLD_CURSORS_OVER_COMMIT); Only need to worry about this if you’re not auto- committing.

Scrollable ResultSet rs.beforeFirst(); rs.afterLast(); rs.next() rs.previous() Looping: while (!rs.isAfterLast()) { Finding number of rows: rs.last(); int numberOfRows = rs.getRow();

Scrollable ResultSet st.setFetchSize(25); Fetch 25 rows at a time. rs.absolute(2); Move to row 2 rs.updateInt(3, 10); Update col 3 of current row. rs.updateInt("Name",10); Update by col name. also updateObject, updateString, updateFloat and others. rs.updateRow(); Must call this after updating data in all the columns you want to change for that row. You also need to call rs.close() to commit all changes.

Inserting a row rs.moveToInsertRow(); rs.updateString(1, "Bob"); rs.updateObject(2, someObject); rs.insertRow(); rs.first(); (In general the cursor points at the row used prior to insert.) st.commit(); If needed. rs.close();

Database metadata Especially useful in debugging is getting metadata about the database: DatabaseMetaData md = conn.getMetaData(); ResultSet rs = md.getTables( null, null, "%", null); while (rs.next()) { System.out.println(rs.getString(3)); }

Further info Online guide: art/GettingStartedTOC.fm.html