Diseño de la capa de datos - Departamento de Lenguajes y

Anuncio
Departamento de
Lenguajes y Sistemas Informáticos
♦
♦
♦
♦
♦
♦
♦
♦!
"
♦#
$
%
()
*
+,
&''
!
-.
&.
/
,#0
+,
♦ +*,1
$2
3
. * 4.
7.
5
* 4 6.-.2
)
,8
♦ +*,1
+*,1 )
$2
3
$
. * 4.
5
6
7.-.2
)
♦ 9
♦
♦
♦
♦
♦
♦
♦
:
$java.sql.*)
,
,1
$
;
0,9,1 <&)
+,
♦
:
$
)
"
♦ #
$%$
<
)
9
♦ #
&%#
♦ #
'%
-==>
.,
+,
♦ #
(%
-==>
*
.?
+,
)
*
♦ 9
♦
♦
♦
♦
♦
♦
+
,
+
,
♦
3
Driver dBDriver = (Driver) Class.forName(driverName)
.newInstance();
DriverManager.registerDriver(dBDriver);
♦
3
DriverManager.deregisterDriver(dBDriver);
♦ '
♦
3
com.microsoft.jdbc.sqlserver.SQLServerDriver
(,1 ,
)
@com.mysql.jdbc.Driver (+*,1
)
♦ 9
♦
♦
♦ ♦
♦
♦
+
.
)
♦,
#A9
:
jdbc:mysql://127.0.0.1:3306/POS
♦
3jdbc
♦
3mysql
♦0
♦
♦
3127.0.0.1
33306
3POS
+,
-
+
.
♦#
*
Connection conn = DriverManager.getConnection(dBUri,
username, password);
♦
conn.close();
3
3
♦ 9
♦
♦
♦
♦ /
♦
♦
0
/
0
♦ Statement
♦
,1
;
♦ PreparedStatement
♦
,1 @
$ ;
;
♦ CallableStatement
♦
)*
;
PreparedStatement
♦,
prepareStatement
B
PreparedStatement prepareStatement(String s)
♦'
;
,1 @
s2
:
4
;
String sql = "DELETE FROM Customer WHERE (CUSTOMERID = ?)";
♦
;
setXXX(x,y)
;
@y
@
4
B
$
x
)
:
setXXX(x,y)
)1
)1
PreparedStatement
#
,
setBoolean(x,y)
boolean
setByte(x,y)
byte
setDate(x,y)
Date
setDouble(x,y)
double
setFloat(x,y)
float
setInt(x,y)
int
setLong(x,y)
long
setString(x,y)
String
setTime(x,y)
Time
setTimeStamp(x,y)
TimeStamp
setObject(x,y)
Object
…
…
y
2*
PreparedStatement stmt = null;
String sql = "DELETE FROM Customer WHERE (CUSTOMERID = ? ) ";
stmt = conn.prepareStatement(sql); //conn es una conexión válida
stmt.setString(1, customerID);
PreparedStatement stmt = null;
String sql = "SELECT * FROM Customer";
stmt = conn.prepareStatement(sql); //conn es una conexión válida
PreparedStatement stmt = null;
String sql = "INSERT INTO CUSTOMER (OID, CUSTOMERID, NAME, SURNAME)”+
“ VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, “33423”);
stmt.setString(2, “Customer 1”);
stmt.setString(3, “Name 1”);
stmt.setString(4, “Name 2”);
2*
)1
PreparedStatement
♦ ResultSet executeQuery()
♦'
%,' '
(@
ResultSet
♦ int executeUpdate()
♦'
%#
'(
% ' ' '(.
♦ boolean execute()
♦
@D
%9
0,'A (@
C
#
#
♦'
♦ +B
,' '
ResulSet
B 3
♦ next()3
ResulSet $
:
:
♦ getXXX(s)3
: @
)
@
s $E
:
)
)1
getXXX(s)
)1
3
getBoolean(s)
#
,
boolean
getByte(s)
byte
getDate(s)
Date
getDouble(s)
double
getFloat(s)
float
getInt(s)
int
getLong(s)
long
getString(s)
String
getTime(s)
Time
getTimeStamp(s)
TimeStamp
getObject(s)
Object
…
…
/
/
♦
3
♦ PreparedStatement
♦ ResulSet $ 2
PreparedStatement stmt = null;
ResulSet result = null;
...
stmt.close();
result.close();
,' ' )
2*
<<Class Model>>
!
!
!
" #
"
"
<<Physical Data Model>>
Select 4$5
2*
PreparedStatement stmt = null;
ResultSet result = null;
Customer c = null;
String sql = "SELECT * FROM CUSTOMER WHERE (CUSTOMERID = ? ) ";
stmt = conn.prepareStatement(sql); //conn es una conexión válida
stmt.setString(1, customerID);
result = stmt.executeQuery();
result.next();
c = new Customer();
c.setCustomerID(result.getString("CUSTOMERID"));
c.setName(result.getString("NAME"));
c.setSurname(result.getString("SURNAME"));
result.close();
stmt.close();
Select 4&5
2*
PreparedStatement stmt = null;
List searchResults = new LinkedList();
ResultSet result = null;
String sql = "SELECT * FROM Customer";
stmt = conn.prepareStatement(sql); //conn es una conexión válida
stmt.executeQuery();
result = stmt.executeQuery();
while (result.next()) {
Customer temp = new Customer();
temp.setCustomerID(result.getString("customerID"));
temp.setName(result.getString("name"));
temp.setSurname(result.getString("surname"));
searchResults.add(temp);
}
result.close();
stmt.close();
Insert
2*
PreparedStatement stmt = null;
String oid = UIDGenerator.getInstance().getKey();
String sql = "INSERT INTO CUSTOMER (OID, CUSTOMERID, NAME, SURNAME)”+
“ VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1,
stmt.setString(2,
stmt.setString(3,
stmt.setString(4,
oid);
c.getCustomerID());
c.getName());
c.getSurname());
stmt.executeUpdate();
stmt.close();
Delete
2*
String sql = "DELETE FROM Customer WHERE (CUSTOMERID = ? ) ";
PreparedStatement stmt = null;
stmt = conn.prepareStatement(sql); //conn es una conexión válida
stmt.setString(1, customerID);
stmt.executeUpdate();
stmt.close();
♦ 9
♦
♦
♦
♦
♦ #
♦
.
#
.
♦,
3
♦
(Class.forName):
♦InstantiationException
♦IllegalAccessException
♦ClassNotFoundException
♦
B
:
4
)3
♦SQLException
:
$
)1
*
,
.
...
try {
// Aquí va el código para cargar el driver
} catch(Exception e) {
System.err.println(e.getMessage());
}
...
...
try {
// Aquí va el código que podría generar la excepción.
} catch(SQLException e) {
// Descripción del error
System.out.println("Message: " + e.getMessage());
// identificación del error
System.out.println("SQLState: " + e.getSQLState());
// Código de error del vendedor
System.out.println("ErrorCode: " + e.getErrorCode());
}
...
)1
4
5
♦
@
...
PreparedStatement stmt;
ResultSet result;
Connection conn;
...
try {
// Aquí va el código que podría generar la excepción.
// y que hace uso de stmt, result y conn
} catch (SQLException e) {
System.out.println("Message: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("ErrorCode: " + e.getErrorCode());
} finally {
conn.close();
if (result != null) {result.close();}
if (stmt != null) {stmt.close();}
}
...
♦ 9
♦
♦
♦
♦
♦
♦
- *
-4
♦
♦
- *
:
%
5
&''
3
♦
*
:
♦
E
3
♦
:
♦'
:
:
*
4
$:
)
(
2
-
!
!
"
$
"
#
!
%
6
&
'(
)(*
)+'(*
)+)(
$
)+,(-
,(&
. % /
0(&
. % /
1(&
1+,(&
1+'(*
. % /
1+)(*
. % /
2*
<<Class Model>>
domain
Customer
-customerID:String
-name:String
-surname:String
+getCustomerID():String
+setCustomerID(customerID:String):void
+getName():String
+setName(name:String):void
+getSurname():String
+setSurname(surname:String):void
Created with Poseidon for UML Community Edition. Not for Commercial Use.
2*
<<Class Model>>
data
<< interface >>
ICustomerDAO
+selectCustomer(customerID:String):Customer
+insertCustomer(c:Customer):void
+deleteCustomer(customerID:String):void
+selectAllCustomers():List
XMLCustomerDAO
JDBCCustomerDAO
JDBCConnectionManager
uses
<< create >>+JDBCCustomerDAO():JDBCCustomerDAO
+selectCustomer(customerID:String):Customer
+deleteCustomer(customerID:String):void
+insertCustomer(c:Customer):void
+selectAllCustomers():List
#finalize():void
-dBUri:String= "jdbc:mysql://127.0.0.1:3306/pos"
-driverName:String= "com.mysql.jdbc.Driver"
-password:String= "practica"
-username:String= "practica"
<< create >>+ConnectionManager():JDBCConnectionManager
+getInstance():JDBCConnectionManager
+checkOut():Connection
+checkIn(conn:Connection):void
#finalize():void
- cm
Created with Poseidon for UML Community Edition. Not for Commercial Use.
78
♦9
+;
7
*
:
F
. .
Descargar