CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.

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.
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: Part I Attribution These slides are based on three primary sources: –Sun JDBC Tutorial URL: jdbc/TOC.htmlhttp://java.sun.com/docs/books/tutorial/
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Think Possibility Integrating Web Applications With Databases.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
Using Java with PSQL and Oracle Thanks to Drs. Raj and Liu for sharing some of these slides TRUDY: ALSO NEED SAMPLE PROGRAM.
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.
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.
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.
CSCD34 - Data Management Systems – A. Vaisman1 Database Application Development.
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.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
1 Database Application Development Chapter 6 Sections 6.1 –
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.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections 6.1 –
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.
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 Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
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
Basics of JDBC.
JDBC (Java Database Connectivity)
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.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
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.
1 Database Application Development Programming With Databases.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
1 Database Application Development Chapter 6. 2 Overview  SQL in application code  Embedded SQL  Cursors  Dynamic SQL  JDBC.
COP Introduction to Database Structures
CS3220 Web and Internet Programming Database Access with JDBC
Lec - 14.
JDBC – Java Database Connectivity
Web Technologies IT230 Dr Mohamed Habib.
JAVA Connection The following uses a ‘bridge’ between Java Database Connectivity (JDBC) and ODBC, namely sun.jdbc.odbc.JdbcOdbcDriver Supplied with the.
CS320 Web and Internet Programming Database Access with JDBC
Database Application Development
HW#4 Making Simple BBS Using JDBC
Interacting with Database
Database Application Development
Using a Database with JDBC
JDBC Example.
Database Application Development
Database Application Development
Presentation transcript:

CS178 Database Management “JDBC”

What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java application and a SQL database Allows a Java program to issue SQL statements and process the results.

JDBC Classes and Interfaces Steps to using a database query: Load a JDBC “driver” Connect to the data source Send/execute SQL statements Process the results

JDBC Driver Acts as the gateway to a database Not actually a “driver”, just a.jar file Java applicationDatabase Server JDBC Driver

JDBC Driver Installation Must download the driver, copy it to cobweb then add the.jar file to your $CLASSPATH To set up your classpath, ssh into cobweb and execute the following command: –export CLASSPATH=$CLASSPATH: :.

JDBC Driver Management All drivers are managed by the DriverManager class Example - loading an Oracle JDBC driver: –In the Java code: Class.forName(“oracle.jdbc.driver.OracleDriver”) Driver class names: Oracle: oracle.jdbc.driver.OracleDriver MySQL:com.mysql.jdbc.Driver MS SQL Server: com.microsoft.jdbc.sqlserver.SQLServerDriver

Establishing a Connection Create a Connection object Use the DriverManager to grab a connection with the getConnection method Necessary to follow exact connection syntax Problem 1: the parameter syntax for getConnection varies between JDBC drivers Problem 2: one driver can have several different legal syntaxes

Establishing a Connection (cont.) Oracle Example Connection con = DriverManager.getConnection(string, “username", “password"); what to supply for string ? “ ” DriverDatabase URLPort #SIDType

Establishing a Connection (cont.) MySQL Example Connection con = DriverManager.getConnection(string); what to supply for string ? “ jdbc:mysql:// :3306/ ?user= &password= ” DriverPortUsernamePasswordURL DB Name

Executing Statements Obtain a statement object from the connection: –Statement stmt = con.createStatement (); Execute the SQL statements: –stmt.executeUpdate(“update table set field=‘value’”); –stmt.executeUpdate(“INSERT INTO mytable VALUES (1, ‘name’)”); –stmt.executeQuery(“SELECT * FROM mytable”);

Retrieving Data ResultSet rs = stmt.executeQuery(“ SELECT id,name FROM employees where id = 1000 ”) Some methods used in ResultSet: –next() –getString() –getInt()

Using the Results while (rs.next()) { float s = rs.getInt("id"); String n = rs.getString("name"); System.out.println(s + " " + n); }

Connection Class Interface public boolean getReadOnly() and void setReadOnly(boolean b) Specifies whether transactions in this connection are read-only public boolean isClosed() Checks whether connection is still open. public boolean getAutoCommit() and void setAutoCommit(boolean b) If autocommit is set, then each SQL statement is considered its own transaction. Otherwise, a transaction is committed using commit(), or aborted using rollback().

Executing SQL Statements Three different ways of executing SQL statements: –Statement (both static and dynamic SQL statements) –PreparedStatement (semi-static SQL statements) –CallableStatment (stored procedures) PreparedStatement class:Precompiled, parametrized SQL statements: –Structure is fixed –Values of parameters are determined at run-time

Executing SQL Statements (cont.) String sql=“INSERT INTO Sailors VALUES(?,?,?,?)”; PreparedStatment pstmt=con.prepareStatement(sql); pstmt.clearParameters(); pstmt.setInt(1,sid); pstmt.setString(2,sname); pstmt.setInt(3, rating); pstmt.setFloat(4,age); // we know that no rows are returned, thus we use executeUpdate() int numRows = pstmt.executeUpdate();

ResultSets PreparedStatement.executeUpdate only returns the number of affected records PreparedStatement.executeQuery returns data, encapsulated in a ResultSet object (a cursor) ResultSet rs=pstmt.executeQuery(sql); // rs is now a cursor While (rs.next()) { // process the data }

ResultSets (cont.) A ResultSet is a very powerful cursor: previous() : moves one row back absolute(int num) : moves to the row with the specified number relative (int num) : moves forward or backward first() and last()

Matching Java-SQL Data Types getTimestamp()java.sql.TimeStampTIMESTAMP getTime()java.sql.TimeTIME getDate()java.sql.DateDATE getFloat()DoubleREAL getInt()IntegerINTEGER getDouble()DoubleFLOAT getDouble()DoubleDOUBLE getString()StringVARCHAR getString()StringCHAR getBoolean()BooleanBIT ResultSet get methodJava classSQL Type

JDBC: Exceptions and Warnings Most of java.sql can throw and SQLException if an error occurs (use try/catch blocks to find connection problems) SQLWarning is a subclass of EQLException; not as severe (they are not thrown and their existence has to be explicitly tested)

JDBC Cobweb example: import java.sql.*; public class JDBCexample { static String url ="jdbc:mysql://cobweb.seas.gwu.edu:3306/ ?user= &password= "; public static void main(String[] args) throws Exception { Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url); System.out.println("Got Connection."); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); }

In class assignment Download the MySQL JDBC “driver” (Connector/J) from and copy the.jar file to your cobweb accountwww.mysql.org Write a java application to run a query on one of your tables and return the results to the command line.