Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reflection API, JDBC, Hibernate, RMI

Similar presentations


Presentation on theme: "Reflection API, JDBC, Hibernate, RMI"— Presentation transcript:

1 Reflection API, JDBC, Hibernate, RMI
James Atlas July 22, 2008

2 Reflection API Examine or modify the runtime behavior
Allows programatic access to Java internals Fields Methods Constructors Classes Access by name and argument type java.lang.reflect.* July 22, 2008 James Atlas - CISC370

3 Reflection API java.lang.Class java.lang.reflect.Constructor
annotations modifiers constructors, methods, fields java.lang.reflect.Constructor newInstance(object..) July 22, 2008 James Atlas - CISC370

4 Reflection API java.lang.reflect.Field java.lang.reflect.Method
annotations declaring class modifiers get(object) java.lang.reflect.Method invoke(object..) July 22, 2008 James Atlas - CISC370

5 JDBC: Java Database Connectivity
Database-independent connectivity Connect to database Get information from database Update data in database Classes in java.sql.* package In Java, two steps Establish a connection with a database Execute queries, statements against database July 22, 2008 James Atlas - CISC370

6 JDBC: Creating a Connection
Load the DB driver Classes/library used to connect to the database Specific to the type of database Load the class by name Class.forName("sun.jdbc.odbc.JdbcOdbcDriver”); Create the connection (see API for all ways) String url = ”jdbc:mysql://localhost:3306/masplasDB"; Connection con = DriverManager.getConnection(url, loginname, password); Need to close connection when done con.close() Type of DB Location of DB, port DB name July 22, 2008 James Atlas - CISC370

7 JDBC: Executing Queries: Statements
Statement stmt = con.createStatement(); executeQuery(String query) Returns a ResultSet Iterate through ResultSet, row-by-row, getting results from each column stmt.executeQuery(“SELECT * FROM COFFEES”); executeUpdate(String query) to update table Returns an integer representing the number of affected rows or 0 for SQL statements that don’t execute anything July 22, 2008 James Atlas - CISC370

8 JDBC: Prepared Statements
prepareStatement(String template) Compile SQL statement “templates” Reuse statement, passing in parameters Java handles formatting of Strings, etc. as parameters updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?"); Set parameters updateSales.setInt(1, 100); updateSales.setString(2, “French Roast”); Parameters July 22, 2008 James Atlas - CISC370

9 JDBC API Documentation: java.sql.* Limitations
Statements, Connections, ResultSets, etc. are all Interfaces Driver/Library implements interfaces for its database Limitations Java doesn’t compile the SQL statements Exact syntax depends on DB Compile, run, verify queries outside of Java for your database Then copy and use in Java code July 22, 2008 James Atlas - CISC370

10 Hibernate “Hibernate's goal is to relieve the developer from 95 percent of common data persistence related programming tasks, compared to manual coding with SQL and the JDBC API” Object to relational mapping Integrated caching/pooling Transparent persistence Object-oriented query language (HQL) Very common in high-end business Java applications July 22, 2008 James Atlas - CISC370

11 Hibernate example: Event persistence
We want to persist an Event object in a relational database Event has properties: ID (Long) Title (String) Date (Date) JavaBean pattern getProperty setProperty empty constructor July 22, 2008 James Atlas - CISC370

12 Hibernate example: mapping XML
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" " <hibernate-mapping> <class name="events.Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping> July 22, 2008 James Atlas - CISC370

13 Hibernate example: insert/select
Session session = getSessionFactory().getCurrentSession(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); select List events = session.createQuery("from Event").list(); for (int i = 0; i < events.size(); i++) { Event theEvent = (Event) events.get(i); System.out.println("Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()); } July 22, 2008 James Atlas - CISC370

14 Distributed Programming
Two primary ways to communicate Message passing (servlets, web services) Remote method calls an object on one computer calls methods of an object on a different computer method call looks like a method call on a local object Remote Method Invocation (RMI) July 22, 2008 James Atlas - CISC370

15 Java RMI: Remote Method Invocation
Lookup an object by name registry Naming service Client Object location Bind object to a name RMI Server Invoke methods on object “stubs” Appears as calling method on local objects Objects that implement java.rmi.Remote July 22, 2008 James Atlas - CISC370

16 Why Java RMI? Write distributed programs
RMI automates a lot of process Does not require network programming, sockets Load class definitions for objects that are passed around July 22, 2008 James Atlas - CISC370

17 Example: Compute Engine
Compute Engine runs on a high performance machine Clients submit compute intensive tasks and receive results July 22, 2008 James Atlas - CISC370

18 Example: Compute Engine
Server java.rmi.Remote Task Compute +executeTask(Task<T>):T Pi Pi BigDecimal ComputePi ComputeEngine July 22, 2008 James Atlas - CISC370


Download ppt "Reflection API, JDBC, Hibernate, RMI"

Similar presentations


Ads by Google