ec-unit 1b enterprise foundations
Post on 03-Apr-2018
235 Views
Preview:
TRANSCRIPT
-
7/28/2019 EC-Unit 1B Enterprise Foundations
1/29
Unit 1
Enterprise Foundations
Part BBalasubba Raman Guruswamy
11 January 2011 1Enterprise ComputingEnterprise Foundations
-
7/28/2019 EC-Unit 1B Enterprise Foundations
2/29
Goal to represent business concepts to
Applications within the business
An XML interface for vendors or clients
Web interface for customers
Requirements
Must have minimal proprietary components Platform independent, database independent
Able to integrate new technologies
Must be capable of personlised experience
Internationalisation, Localsation, Accessibilty, Customisation
Must be authoritative, shared source for businessconcepts
Transparent to the user
-
7/28/2019 EC-Unit 1B Enterprise Foundations
3/29
Multi-tier typical application
-
7/28/2019 EC-Unit 1B Enterprise Foundations
4/29
Stand-alone client
-
7/28/2019 EC-Unit 1B Enterprise Foundations
5/29
Web-centric application
-
7/28/2019 EC-Unit 1B Enterprise Foundations
6/29
Business-to-business
-
7/28/2019 EC-Unit 1B Enterprise Foundations
7/29
JDBC JavaMail
Java API for XML Parsing (JAXP)
Web services APIs
-
7/28/2019 EC-Unit 1B Enterprise Foundations
8/29
JDBCTM technology is an API that lets you
access virtually any tabular data source fromthe JavaTM programming language.
Cross-DBMS connectivity to a wide range of SQLdatabases
Access to other tabular data sources, such asspreadsheets or flat files.
http://java.sun.com/products/jdbc/index.html
http://java.sun.com/products/jdbc/index.htmlhttp://java.sun.com/products/jdbc/index.html -
7/28/2019 EC-Unit 1B Enterprise Foundations
9/29
JVM
Application
JDBC API
JDBC Driver
Database
Server
Database
Java Application
Java Core
APIs
-
7/28/2019 EC-Unit 1B Enterprise Foundations
10/29
Level 1 - AJDBC-ODBC bridge provides JDBC APIaccess via one or more ODBC drivers.
Level 2 - A native-API partly Java technology-enableddriverconverts JDBC calls into calls on the clientAPI for Oracle, Sybase, Informix, DB2, or otherDBMS.
Level 3 - A net-protocol fully Java technology-enableddrivertranslates JDBC API calls into a DBMS-independent net protocol which is then translatedto a DBMS protocol by a server.
Level 4 - A native-protocol fully Java technology-enabled driverconverts JDBC technology calls intothe network protocol used by DBMSs directly.
http://java.sun.com/products/jdbc/driverdesc.html
http://java.sun.com/products/jdbc/driverdesc.htmlhttp://java.sun.com/products/jdbc/driverdesc.html -
7/28/2019 EC-Unit 1B Enterprise Foundations
11/29
Type 1:
ODBC
Bridge
Java
App
JDBC
API
JDBC
Bridge
Driver
MS
ODBC
Database
Server
Database
Type 2:
NativeDB API
Java
App
JDBC
API
JDBC
Driver
Native API
Library
Native
APIDatabase
-
7/28/2019 EC-Unit 1B Enterprise Foundations
12/29
Type 3:
Pure
Java
Java
App
JDBC
API
JDBC
DriverDatabase
Translator
Database
Server
Database
Type 4:
Pure
JavaDirect
Java
App
JDBC
API
JDBC
Driver
Database
Server
Database
-
7/28/2019 EC-Unit 1B Enterprise Foundations
13/29
Basic sequence of operations for a program that will
use JDBC to interact with a database:
Load the correct JDBC driver (using the classname)
Open a Connection to the database (using a URL)
Create statements (using the Connection) Execute statements and process results
Close the Connection
-
7/28/2019 EC-Unit 1B Enterprise Foundations
14/29
JDBC always refers to databases by URLs.
General structure of a JDBC URL is:
jdbc::[params]
Some common examples:jdbc:odbc:aMSAccessDBjdbc:odbc:oDatabase2;UID=jeffsix;PWD=simplePW
jdbc:mysql://dbserver/myDB?user=jeff&password=easy
jdbc:postgresql://psServer.mycorp.com:1878/myDB
jdbc:sequelink://host5/someDB;uid=bob;password=pop
-
7/28/2019 EC-Unit 1B Enterprise Foundations
15/29
There are many methods a program can call
on its valid Connection object. createStatement() method will create a Statement
object that can be used to assemble and run SQLcommands.
preparedStatement() creates an object that isassociated with a predefined SQL command (theStatement object can be used for arbitrary statements and can be reused for other SQL commands)
getMetaData() method will return metadata
associated with the database, including descriptions ofall of the tables in the DB.
prepareCall() method is used to call storedprocedures in the SQL database.
-
7/28/2019 EC-Unit 1B Enterprise Foundations
16/29
There are many methods a program can
call on its valid Statement object. executeQuery() method executes a SQL query
statement and returns the results (as a ResultSet)
executeUpdate() method executes a SQL update
(such as UPDATE, INSERT, or DELETE) execute() method executes other, more generic,
SQL statements.
setMaxRows() method allows the program to setthe maximum number of data elements (rows)that can be returned from a query statement.
There are MANY more methods for boththe Connection and Statement classes.
-
7/28/2019 EC-Unit 1B Enterprise Foundations
17/29
-
7/28/2019 EC-Unit 1B Enterprise Foundations
18/29
Performing a query and interacting with
the ResultSet ResultSet queryResults;
theQueryResults = theStatement.executeQuery(
"select distinct dept from instructors");
while(theQueryResults.next()){
System.out.println(Dept:" +
queryResults.getInt(1));
}
theQueryResults.close(); ResultSet objects keep track of their
position internally, starting before the firstrow. You can use next() and other
navigation methods to move through them
-
7/28/2019 EC-Unit 1B Enterprise Foundations
19/29
There are multiple methods of extracting datafrom the current row in a ResultSet.
getString() method returns the value of a particularcolumn in the current row as a String.
getInt() method returns the value of a particular column in
the current row as an int. getBoolean() method returns the value of a particularcolumn in the current row as an boolean.
getDouble() method returns the value of a particularcolumn in the current row as an double.
getObject()method returns the value of a particular
column in the current row as an Object.
Two versions of both of these methodsonetakes the column as an integer index, one takesthe columns name as a String.
-
7/28/2019 EC-Unit 1B Enterprise Foundations
20/29
There are three types of statement objects
in JDBC programming: Statement - allows the execution of arbitrary
statements on the database.
PreparedStatement - allows the program toexecute the same SQL command repeatedly,while allowing substitutions for particular words orvalues (to make it more useful).
CallableStatement - allows the program toexecute SQL stored procedures, with substitutions
for arguments. A stored procedure is a functionthat is part of, and stored inside, a SQL database(hence the name).
-
7/28/2019 EC-Unit 1B Enterprise Foundations
21/29
import java.sql.*;
public class IDQuery {
static String url = "jdbc:mysql://localhost/instructors";
public static void main(String [] args) {
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection theConnection;
PreparedStatement prep_statement;
ResultSet theQueryResults;
theConnection = DriverManager.getConnection(url,jeffsix,arrrr!);
prep_statement =
theConn.prepareStatement("SELECT name,dept FROM instructors WHEREID=?");
for(int i = 0; i < args.length; rs.close(), i++) {
prep_statement.setString(1, args[i]);
theQueryResults = prep_statement.executeQuery();
if (theQueryResults.next())System.out.println(theQueryResults.getString(1) + ","
+ theQueryResults.getString(2));
else System.out.println(Query + i + not found!);
}
prep_Statement.close(); theConnection.close();
} catch (Exception exp) { System.err.println("Execption! - " + exp); }
}}
-
7/28/2019 EC-Unit 1B Enterprise Foundations
22/29
CallableStatement calls a SQL stored procedure insidethe DB. The stored procedure is specified upon objectcreation (using the Connections prepareCall()method).
Syntax of this call is
? = call procedurename(?,?) }
for a procedure that takes 2 arguments; returns a value { call procedurename(?,?,?,?) }
for a procedure that takes 4 arguments; does not return avalue
The syntax is
CallableStatement callable;callable = conn.prepareCall("{ call replace(?,?) }");callable.setString(1, oldString);callable.setString(2, newString);int rows_modified = callable.executeUpdate();
-
7/28/2019 EC-Unit 1B Enterprise Foundations
23/29
Your program can create a Batch of statements (anyof the three types) and execute that batch on thedatabase all at once
The addBatch() method adds a statement to the currentbatch
The executeBatch() method runs all of the statements in the
batch on the database The clearBatch() method clears the statements from the
batch without running them
-
7/28/2019 EC-Unit 1B Enterprise Foundations
24/29
As SQL defines its own datatypes, it is important
to understand the mapping between SQL and Javadatatypes
SQL Java SQL Java--------------------- ------------ -------------BIT boolean NUMERIC BigDecimal
BIGINT long REAL floatBINARY byte[] SMALLINT shortCHAR String TIME TimeDATE Date TIMESTAMPTimestampDOUBLE double TINYINT byteFLOAT float VARBINARYbyte[]
INTEGER int VARCHAR char[]
BLOB Blob REF Ref CLOB Clob STRUCT Struct
-
7/28/2019 EC-Unit 1B Enterprise Foundations
25/29
SQLs offer two ways to support storage and
retrieval of binary data in database tables: Binary columns: BINARY/ VARBINARY
- these kinds of columns are suitable for small arrays ofbytes (they are not very memory efficient).
Binary large object columns: BLOB / LONGBLOB
- these kinds of columns are suitable for large arrays ofbytes, but are not supported in all database systems (theyare much more memory efficient).
BLOBs are useful in many databases:
used to store image, audio, signal, or video data
used to store serialized Java objects
suitable for storing entire documents inside databases (asdocuments are really just binary data)
-
7/28/2019 EC-Unit 1B Enterprise Foundations
26/29
Storing binary data into the database
Using the SQL INSERT verb, the binary column is set usingeither the setObject() method and passing it a byte arrayor using the setBinaryStream() method and passing it anInputStream (which the JDBC driver will then use to fill inthe columns data field)
Retrieve binary data from the database
Use a normal query, and either getObject() to return a bytearray, getBlob() to return a Blob object, orgetBinaryStream() to get an InputStream which can be
used to read from this data field
-
7/28/2019 EC-Unit 1B Enterprise Foundations
27/29
JDBC is a core Java technology that supports access torelational databases and database servers.
JDBC uses the standard Structured Query Language(SQL) for all database interaction.
In order to use JDBC with a particular database, you
must install a driver for the database server you areusing and load that driver in your programs.
Almost all JDBC functionality is accomplished throughthe use of SQL statements, of three kinds Statement,
PreparedStatement, and CallableStatement.
-
7/28/2019 EC-Unit 1B Enterprise Foundations
28/29
1. What are the goals and objectives of True EnterpriseSystem ?
2. What is JDBC?
3. Give basic sequence of operations for a program usingJDBC to interact with a database.
4. What is ResultSet in JDC?
5. How does JDBC refer to a database in a program code?Give the syntax.
6. What are the three types of JDBC statement objects?
11 January 2011Enterprise ComputingEnterprise Foundations 28
-
7/28/2019 EC-Unit 1B Enterprise Foundations
29/29
1. Give details of different types of JDBC drivers.
2. Give brief note on JDBC Connection Class and JDBCStatement Class.
3. Describe the four methods of extracting data fromcurrent row n a ResultSet.
4. Give s brief note on JDBCs support for binary data in
dstabase tables.
11 J 2011Enterprise ComputingE t i F d ti 29
top related