JDBC: Java Database Connectivity - A Comprehensive Guide, Slides of Database Management Systems (DBMS)

An overview of jdbc, a java api for database access. It covers the benefits of using jdbc, the different types of jdbc drivers, and the steps to create a simple jdbc application. The document also discusses the connection object, jdbc sql statement classes, and other important concepts such as preparedstatement, resultset, and errors and warnings.

Typology: Slides

2012/2013

Uploaded on 04/30/2013

archa
archa 🇮🇳

4.3

(15)

94 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JDBC
JDBC is an API that enables database access
from Java programs
JDBC for DB access provides
Portability across database servers
Portability across hardware architectures
JDBC drivers have been implemented for a
number of DB servers (Oracle, SQL Server,
DB2, Access, etc.)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download JDBC: Java Database Connectivity - A Comprehensive Guide and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

JDBC

  • JDBC is an API that enables database access

from Java programs

  • JDBC for DB access provides
    • Portability across database servers
    • Portability across hardware architectures
  • JDBC drivers have been implemented for a

number of DB servers (Oracle, SQL Server,

DB2, Access, etc.)

JDBC

  • Java Database Connectivity (JDBC) has been

part of the standard Java standard edition

since JDK 1.

  • Current version is JDBC 3.0 (as of J2SE 1.4)
  • JDBC classes are contained in the Java package java.sql
  • A JDBC-ODBC bridge is included so that any ODBC data source available in the JVM host environment can be accessed
  • Type
  • Type
  • Type
  • Type

JDBC

  • Steps in JDBC applications
    • Load JDBC classes (java.sql.*)
    • Load the JDBC drivers
      • Class.forName(“oracle.jdbc.driver.OracleDriver”)
    • Connect to the database
      • Connection conn = DriverManager.getConnection(url,userid,passwd)
      • jdbc:oracle:drivertype@database
    • Interact with DB using JDBC
    • Disconnect from DB

JDBC Example

Connection conn = DriverManager.getConnection( "jdbc:oracle:oci8:"+user+"/"+pass); Statement stmt = conn.createStatement (); ResultSet rset = stmt.executeQuery ("select distinct eno,ename,zip,hdate from employees"); while (rset.next ()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3) + " " + rset.getDate(4)); } stmt.close(); conn.close(); }

JDBC Example

//readEntry function -- to read input string static String readEntry(String prompt) { try { StringBuffer buffer = new StringBuffer(); System.out.print(prompt); System.out.flush(); int c = System.in.read(); while(c != '\n' && c != -1){ buffer.append((char)c); c = System.in.read(); } return buffer.toString().trim(); } catch (IOException e) { return ""; } } }

JDBC SQL Statement Classes

  • There are three classes for sending SQL to the

DB server

  • Statement. Used for SQL statements with no parameters.
  • PreparedStatement. Used when the same statement, possibly with different parameters is to be called multiple times.
  • CallableStatement. Used for calling stored procedures.

Other Connection Class Methods

  • public abstract void close() throws SQLException
    • Immediately release the Connection’s database and JDBC resources
  • public abstract void setAutoCommit (boolean autoCommit) throws SQLException - Pass true to enable autocommit, false to disable it
  • public abstract void rollback() throws SQLException
    • Useful when autocommit is false
  • public abstract DatabaseMetaData getMetaData() throws SQLException
    • Get metadata from the DB

ResultSet Class

  • The result of an executeQuery is a single ResultSet

object

  • This class provides access to the table resulting from the query
  • A cursor points to the current row (initially positioned before the first row.
  • Use the next method to fetch the next row
  • The current row’s column values are retrieved using the getXXX methods, where XXX is a Java type. - Pass either an index or the column name to specify which column

ResultSet Class

  • Use the wasNull method to check whether the

previously read column value was Null.

  • Use getMetaData to obtain info like number,

types and properties of the columns of a

ResultSet

Errors and Warnings

try { some JDBC statement to access the DB; } catch (SQLException e) { System.out.println(“SQL Exception caught!”); while (e!= null) { System.out.println(“Error Message = “ + e.getMessage()); System.out.println(“SQL State = “ + e.getSQLState()); System.out.println(“Error Code = “ + e.getErrorCode()); e = e.getNextException(); } }

Errors and Warnings

  • The SQLWarning class provides info about

warnings generated during DB access

  • Use getMessage to get the warning string
  • Use getSQLState to get warning according to X/Open SQL Spec
  • Use getErrorCode for vendor specific code
  • Use getNextWarning for next warning generated