Sie sind auf Seite 1von 21

JDBC

Content
Introduction of Java Introduction of JDBC JDBC API

Introduction of Java
Java evolved from C++. They have similar syntax and both support object-oriented programming. The output of Java compiler is not executable code but bytecode. It is run on Java virtual machine (JVM).

Introduction of Java
Java programs are more portable and secure. Java is the language of Internet There are some significant differences between Java and C++. For example Java does not support pointers.

Requirements in HW2
How to use Java to create a Graphical User Interface (GUI) for a spatial DBMS It can display spatial data and query results It can handle the events from the mouse or keyboard The related classes include JFrame, JComponent, JPanel, Jlabel, JCheckBox, JTextField, JRadioButton, etc. They are in javax.swing and java.awt packages.

What is JDBC
Java DataBase Connectivity It enables the integration of SQL with Java The application programmer access database capabilities through an application programming interface (API)

What is JDBC
In contrast to embedded SQL, it allows a single executable to access different DBMSs without recompilation An application can access several different DMBSs simultaneously

JDBC architecture
JDBC achieves the portability by using DBMS-specific drivers A driver is a software program that translates the JDBC calls into DBMSspecific calls

JDBC architecture
Available drivers are registered with a driver manager Drivers are loaded dynamically at run-time

How JDBC works


The application initiates and terminates the connection with a data source (DBMS), submits SQL statements and retrieves results through the JDBC API The driver manager loads appropriate JDBC drivers and pass JDBC functions calls to the correct drivers

How JDBC works


The driver establishes the connection with the DBMS and translate the function calls and date formats between the specific data source and the JDBC standard

Types of JDBC drivers


Type I---Bridges. This type translates JDBC function calls into function calls of another API that is not native to the DBMS An example is JDBC-ODBC bridge. An application can use JDBC to access an ODBC (Open DataBase Connectivity) compliant data source without installing JDBC drivers

Types of JDBC drivers


Type II---Direct translation to the Native API Type III---Network bridges. The driver talks over a network to a middleware server that translates the JDBC requests into DBMSspecific method invocations

JDBC API
The classes and interfaces are part of the java.sql package. So:
import java.sql.*;

Register the JDBC driver:


DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); Or Class.forName(oracle/jdbc.driver.OracleDriver);

JDBC API
Connection to the DBMS:
url = jdbc:oracle:thin:@shams.usc.edu:1521:CSCI585; userId = vivksin; Password = v578657 Connection con = DriverManager.getConnection(url, userId, password);

Close Connection:
con.close();

JDBC API
Set connnection properties, for example:
Connection.SetAutoCommit(true); //each SQLstatement is a transaction

Execute SQL statements:


String sql = INSERT INTO Books VALUES(isbn, title, author, price, year); PreparedStatement pstmt = con.prepareStatement(sql); Pstmt.executeUpdate();

JDBC API
Execute SQL statements:
String sql = SELECT title From Books WHERE year = 2005; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();

Or
String sql = SELECT title From Books WHERE year = 2005; Statement stmt = con.createStatment(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(sql);

JDBC API
ResultSets:
Statement stmt = con.CreateStatement(); ResultSet rs = stmt.executeQuery(sql); //rs is a cursor While(rs.next()){ title = rs.getString(TITLE); }

JDBC API
SQL Type BIT CHAR VARCHAR DOUBLE INTEGER FLOAT Java class Boolean String String Double Integer Double Get method getBoolean() getString() getString() getDouble() getInt() getDouble()

JDBC API
Most methods in java.sql can throw an exception of the type SQLException if an error occurs. It provides getMessage(), getSQLState() and getErrorCode() to retrieve the error information.

JDBC API
Connection, statement and ResultSet objects all have a getWarnings() method to retrive SQL warnings if they exist.

Das könnte Ihnen auch gefallen