You are on page 1of 72

i  


 i

|
 

|  
 | ¦  
  
  
G  
G    
 
G !"#$
G %& $

|  
 |
u

G Ñ' 
G Ñ' &|
G &| ()
G Ñ*+

|  
 |
Ñ


./) &| |Ñ.&

-

*|(,)(

0'  Ñ!
|  
 |
¦  
G    
G -

|  
 |
  Ñ

  

i

|  
 |



 

G   &


G !"#1 2  3

|  
 |
wi
G w 4'
G / 
G Ñ-5 
G  1 ,
G - 1 ,
G 0'  
Gw   )(   
G 
G  
G 6  
|  
 |
G 1 5

u 


G *' 5
G *5
G 05

|  
 |
ÿ ÿ
u 


ÿ
G | 
G 7**(4
G ( 8
G 
G 5  '    

G| 8
G 
G  |  
 |
ÿ

ÿ
u 


ÿ

G  !'


|  
 |
ÿ

ÿ

ÿ

G 8 ' 


 
G   ' 
 
G 1 8 , 
,
G +
G  '
|  
 |
ÿ

ÿ
 
ÿ

G  
G 
G  ''  
G    23

|  
 |
‰ÿ
u 


G   


9: 
G ;Ñ-5Ñ+
G (    '
  
G  '05* 4
'./)&|

|  
 |

ÿ
  

G 7
G  2  3
G 1 &

G . 2 !"#32.&/!3


G  '4 
G   

G Ñ-5 &2Ñ.&/!3
G 4  .&
G 0Ñ* -5 55 9 -:  

G Ñ-5 &2ÑÑ&3
G   -
G 5  ÑÑ 
|  
 |
[
  

G 6| 
G  
  '

G   2 3   23


G 9 :29 :3 ' 


 2  3

|  
 |
i ÿ

G  '  
G  ' 
G  ' 

G   -5 

|  
 |
i 


G ( 
G 
G 
G '  

G 
G 
G 
G 
G 1 |  
 |
u 
G !"#
G !1 9+:
23 
G ! 5
G 4  
 & &

|  
 |
u u
INSERT INTO à  (  
)
VALUES (    
)
G '
 
UPDATE à  SET (   

=  
) WHERE à
G 
  
DELETE FROM à  WHERE à
G     
SELECT  
FROM à  WHERE
à
G    
|  
 |
ÿ   
G * <  
'2 3 
G ) 4
  
G  =& 
'  
G |Ñ//)*<  
G .Ñ##& |>< 
|  
 |
  i Ñ



|  
 |
i  
G !"#5#
G
??@(
G >
G 75 
G #  
G ''$

G ; 4' 


G ;  
  |  
 |
i u
 

A,Ñ(60

Ñ&|
&|

|  
 |
i u 


  &| 

G  &|
G &|  

G   
G |  5B  

G )8' 
  
|  
 |
i  

G *)89&:
G *))890:
G *)))89/':
G *)%89(:

|  
 |
i  
 !"
*) Ñ&|
Ñ&|
9&: 

*))
&| |#)23
90:

*))) /'
9/': !

*)%
9(:
|  
 |
ÿ
 

G ;  
G .+ ,   

G 0   w
G Ñ&|&

|  
 |
ÿ
 

G 0 ()
G .+ ,   

G ; |#)
G ; 5
G /    '
G ) Ñ4w# 

|  
 |
ÿ
 

G |'4 
 
G %55  ' 
 
G Ñ  '  
G &=   

G !& '
|  
 |
ÿ
¦ 

G
??@(55 7  
G ;'    
 
G Ñ8  ' '
 
G Ñ4!"#

|  
 |
i  
G0   
G 0   

|  
 |
[

ÿ
  

G Ñ&|
G .+  2 3
G .Ñ4 Ñ
G .+wCD

G ÑÑ&
G Ñ-!  Ñ)
G &
G  - 2  
3 |  
 |
  i u 

|  
 |
D! #
G &|
-+ 

|  
 |
  
 

Driver d = new
foo.bar.MyDriver();
Connection c = d.connect(...);
G0  4/

G ;   ' '


|  
 |

$

G /
G ; '  
G w 4
'/
G * 4 4- E

|  
 |
[
 
 

G  
Class.forName(³foo.bar.MyDriver´);
Connection c =
DriverManager.getConnection(...);
G  Ddbc.drivers 
 

|  
 |
i ÑD


G /
G# 4 
G 
G  
G|  
G  !"#  &
G !
G !"#

G .!
G   ! |  
 |
i  % 

/



|  

!

.!

|  
 |
i %[
Ddbc:˜ à:˜ 
G 
'   
G     ' 
 
Ddbc:odbc:— à  
G Ddbc:odbc:Northwind
Ddbc:msql://›˜àà à  ˜
G 
Ddbc:msql://foo.nowhere.com:4333/accounting

|  
 |

$

Connection getConnection
(String url, String user,
String password)
G|  &|;.#'
' 
G * '-+!"#6 
G |   -

|  
 |
 
 
G |   '

G w  |  4!"#

G |   
G 0&8!  = 1  
G  4  2 '3
G   9:55    
44
G    ' 
|  
 |
Ñ 
 
u 
   

 
   ! "
   !#  "
$
  % &' ( "
( u  )* "$
 u+,&' ( "
( u  )* "$

|  
 |
 
 $
 
u     u   "
G '! -
ë( u   ((u   u 
-"
G '(! -
 u   ((u -"
G '|! -
G w  $
Ñ1 

|  
 |
u


G ! - 
!"# 
 

|  
 |
u

$
 
ResultSet executeQuery(String)
G 6!"#
.!
int executeUpdate(String)
G 6!"#)0!6.*4;( *6 6#6*6
.  '
boolean execute(String)
G 6!"#

G w  +$
Ñ1  |  
 |
[
u

G .!   
!
G Ñ .!! 

G * '+
G .!   
 ' 
G *FF    
 '
G ='
|  
 |
[
u
$
 
G 23
G  '
G  23 '
G    '

G  23
G   .!
G  '  5!

G   !
  |  
 |
[
u
$
 
G ÿ
ÿ
2 )3
G 
G 
2 ?3
G ÿ
ÿ
2! 03
G 4 
G 

G | 2! 03


G   

|  
 |
[
u
$
 
G !!2 )3
G  & 2 )3
G &2 )3
G  ! 2 )3
G )2 )3
G  # 2 )3
G   2 )3
G   2 )3
G 2 )3
G **2 )3
G **2 )3
|  
 |
[
u
$
 
G !!2! 03
G  & 2! 03
G &2! 03
G  ! 2! 03
G )2! 03
G  # 2! 03
G   2! 03
G   2! 03
G 2! 03
G **2! 03
G **2! 03
|  
 |
 ‰
G )!"#40;##
G0 ? 9:
G )&|4  
.!02 3

|  
 |
u
 

V 

  

  
    
     

! " 
# $% &  

|  
 |
uÿ

Connection con =
DriverManager.getConnection(url,
"alex", "8675309");
Statement st = con.createStatement();
ResultSet results =
st.executeQuery("SELECT EmployeeID,
LastName, FirstName FROM Employees");

|  
 |
uÿ
 !"
while (results.next()) {
int id = results.getInt(1);
String last = results.getString(2);
String first = results.getString(3);
System.out.println("" + id + ": " +
first + " " + last);
}
st.close();
con.close();

|  
 |
$iÿ
 u 
ÿ

u   

|7 .4% .|7 .4#Ñ0 % .|7 . !
0;/6.)|46|)/ # -&
&)*  
*)0G)0* 
!/ ##)0*  
)0*6 6. 
&) )0*  
.6 #  
#Ñ *4Ñ;&#6  
&)0 .G4% .&)0 .G4#Ñ0 % .&)0 .G HI
 *6 -+
*)/6 -+*
*)/6!* /( -+*

|  
 |

ÿ

G *!"#   
G  
G -+
G 4 4
G -+*
G 44 
G -+*
G 4 44 44 4  
G  
|  
 |
$ 
 

G ;!"# 
9)0!6.*: 9;( *6:
G w=  
!"#$Ñ1 
G ;  '
 
G ;  9|.6 *6
* &#6:2#3
|  
 |
‰u[ÿ


|  
 |
ÿ   $


G *    
 
G )4  
u    
G u   4
  
G 8

|  
 |

u  
|    | 2 3
G u   4
    
G      
 |   23
|    23

|  
 |
 
 $

G 78 4
|  / -
G )    
   
G w    4  
 |/= 
G w$&     
 
G w8|/ '  | 23
'  

|  
 |
Ñ&
u


G (!
G !"#  
G  '  12 3+
G '|  !23

G ! ( 
G '&5
G  
G '|  |23
|  
 |
i    

w E
|  
 |
$

G|  8
G //23

G .!8
G .!//23

|  
 |
[
u
$
 
G wF  .!$
G wF F$
G wF F!"#$
G wF F '$
G wF   $
G wF F $
G   F$
G ) $
G w'  $
G | 0;## $
G )  '$
G | '$
G ) $
G )  '   $
G   

|  
 |

$
 
G w$
G wF   ' 
$
G )5  $
G )   4
  
 $
G   J
|  
 |
i
 i [
 
$

|  
 |
i '!(
G ! 
G &
G 
G & 4 -4
G. '
G (&

G 0)
G|  ( 
G  *!
u 
G !'
G ' 
G .+   

|  
 |
w

 


  
G Ñ 
G .4 
  i 
i2Ñ=.3
G 8,,- , ,-,
G 8,,- , ,-5,
G 8,,''' ,-,2  =
3

|  
 |