Você está na página 1de 3

JDBC:Java Database Connectivity What does JDBC do?

z What is JDBC™? z Establish a connection with a database


z Java™ API for executing SQL statements z Send SQL statements
z Consists of a set of classes and interfaces written in z Process the results
the Java programming language
z Provides a standard API for tool/database
developers and makes it possible to write database
applications using a pure Java API package: java.sql
Has classes and interfaces for using relational databases

Establishing a Connection Example:


Connection con ;
try {
z Connection Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
z Manages connection between database and program String url = "jdbc:odbc:nwind" ;
con = DriverManager.getConnection(url,"admin","");
z Create connection }
z 1. Checking for driver catch (ClassNotFoundException cnfe){
System.out.println(“No specific driver..”);
z Using static method forName (class Class) }
ƒ Load class definition for database driver (complete package name)
ƒ Throws ClassNotFoundException z url:
Protocol for communication (jdbc)
z 2. Open a connection Subprotocol (odbc)
z Using Class DriverManager - indicates Microsoft ODBC data source
- Driver allowing Java to access any ODBC source:
ƒ The basic service for managing a set of JDBC drivers
sun.jdbc.odbc.JdbcOdbcDriver
ƒ getConnection(url, user-name, password) Database name (nwind)
The Statement Objects
Sending SQL Statements
z The object used for executing a static SQL statement
z JDBC provides three classes for sending SQL and returning the results it produces
statements to the database. z A Statement object is created with the Connection
z Statement z Executing Statement Objects
z Created by the method createStatement z The Statement interface provides three different methods for
z Used for sending simple SQL statements executing SQL statements
z executeQuery
z PreparedStatement
z executeUpdate
z Created by the method prepareStatement
Execute INSERT, UPDATE, or DELETE statements and also
z Used for SQL statements that take one or more parameters as SQL DDL (Data Definition Language) statements like
input arguments (IN parameters) CREATE TABLE and DROP TABLE
z CallableStatement z execute
The execute method should be used only when it is possible
z Created by the method prepareCall
that a statement may return more than one ResultSet object,
z Used to execute SQL stored procedures more than one update count, or a combination of ResultSet
objects and update counts

Example: The PreparedStatement Objects


z Creating PreparedStatement
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = "jdbc:odbc:nwind"; String url = "jdbc:odbc:nwind" ;
Connection con; Connection con ;
con = DriverManager.getConnection(url, "admin", "");
con = DriverManager.getConnection(url, "admin", "");
PreparedStatement pstmt =
con.prepareStatement("UPDATE Customer SET
Statement stmt = con.createStatement(); companyname=? WHERE customerid=?");
String sql = “SELECT * FROM Customers”
z Passing IN parameters
ResultSet rs = stmt.executeQuery(sql); z Use method setXXX(order, value)

pstmt.setString(1,“IT-SOFT”) ;
pstmt.setLong(2, 41254) ;
pstmt.executeUpdate();
ResultSet Example:
z Contains all of the rows which satisfied the conditions ResultSet rs =
stmt.executeQuery("SELECT * FROM Customers");
in an SQL statement
z Provides access to the data in those rows through a while ( r.next() ) {
set of get methods
int i = r.getInt("customerid") ;
z The ResultSet.next method is used to move to the
String s = r.getString("companyname");
next row of the ResultSet, making the next row
float f = r.getFloat("discount_rate");
become the current row
System.out.println(“ROW = " + i + " " + s + " " +
z The general form of a result set is a table with column f);
headings and the corresponding values returned by a }
query

ResultSetMetaData
z Use to get information about the types and
properties of the columns in a ResultSet
object
z Created by the method getMetaData()
z Example:
ResultSet rs =
stmt.executeQuery("SELECT * FROM Customers");
ResultSetMetaData rsmd ;
rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();

Você também pode gostar