Departamento de Lenguajes y Sistemas Informáticos
! "
# $% &'' () * +,
! -. /,#0 &. +, +*,1 $23.*4.5*46.-.2) 7.,8 +*,1 $ 6 +*,1) $23.*4.5 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
-+. # * 3 Connection conn = DriverManager.getConnection(dBUri, username, password); 3 conn.close();
9 /0
/0
Statement,1 ; PreparedStatement,1@ $;;)* ; CallableStatement
PreparedStatement, B preparestatement PreparedStatement preparestatement(string s) '; s 2 :,1@ 4 ; String sql = "DELETE FROM Customer WHERE (CUSTOMERID =?)"; ; @B setxxx(x,y) $x : ; @y 4 )
)1 setxxx(x,y) )1 PreparedStatement setboolean(x,y) setbyte(x,y) setdate(x,y) setdouble(x,y) setfloat(x,y) setint(x,y) setlong(x,y) setstring(x,y) settime(x,y) settimestamp(x,y) setobject(x,y) #, y boolean byte Date double float int long String Time TimeStamp Object
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() ' %90,'A(@ %#'( %'''(. C boolean execute() @D
#
# ','' ResulSet +B B3 next()3 : ResulSet $ : ) getxxx(s)3 @ :@ :s $E )
)1 getxxx(s) )1 3 getboolean(s) getbyte(s) getdate(s) getdouble(s) getfloat(s) getint(s) getlong(s) getstring(s) gettime(s) gettimestamp(s) getobject(s) #, boolean byte Date double float int long String Time TimeStamp Object
/
/ 3 PreparedStatement ResulSet $2,'') PreparedStatement stmt = null; ResulSet result = null;... stmt.close(); result.close();
2* <<Class Model>> <<Physical Data Model>>!"#!"!"
2* Select4$5 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();
2* Select4&5 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();
2* Insert 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, oid); stmt.setstring(2, c.getcustomerid()); stmt.setstring(3, c.getname()); stmt.setstring(4, c.getsurname()); stmt.executeupdate(); stmt.close();
2* Delete 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 * : E3 : ' : : * $: 4 )
2 - %!! "$ " #!
6 & '( )(* )+'(* )+)( $ )+,(-,(&.%/ 0(&.%/ 1(& 1+'(*.%/ 1+)(*.%/ 1+,(&
2* <<Class Model>> domain -customerid:string -name:string -surname:string Customer +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* data <<Class Model>> << interface >> ICustomerDAO +selectcustomer(customerid:string):customer +insertcustomer(c:customer):void +deletecustomer(customerid:string):void +selectallcustomers():list XMLCustomerDAO JDBCCustomerDAO JDBCConnectionManager << create >>+JDBCCustomerDAO():JDBCCustomerDAO +selectcustomer(customerid:string):customer +deletecustomer(customerid:string):void +insertcustomer(c:customer):void +selectallcustomers():list #finalize():void uses -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.
787 9 *: +; F..