Вы находитесь на странице: 1из 12

Dynamic SQL

Dynamic SQL
 Allows programs to construct and submit SQL queries at
run time.
 Example of the use of dynamic SQL from within a C
program.
char * sqlprog = ”update account set balance =
balance ∗1.05 where account-number = ?”
EXEC SQL prepare dynprog from :sqlprog;
char account[10] = ”A-101”;
EXEC SQL execute dynprog using :account;
 The dynamic SQL program contains a ?, which is a place
holder for a value that is provided when the SQL program
is executed.
1. ODBC
 Open DataBase Connectivity(ODBC)
standard
 standard for application program to
communicate with a database server.
 application program interface (API) to
 open a connection with a database,
 send queries and updates,

 get back results.


ODBC Code
int ODBCexample()
{ SQLBindCol(stmt, 2, SQL C FLOAT, &balance, 0 ,
RETCODE error; &lenOut2);
HENV env; /* environment */ while (SQLFetch(stmt) >= SQL SUCCESS)
HDBC conn; /* database connection */ {
SQLAllocEnv(&env); printf (” %s %g\n”, branchname, balance);
SQLAllocConnect(env, &conn); }
SQLConnect(conn, ”aura.bell-labs.com”, SQL NTS, }
”avi”, SQL NTS,”avipasswd”, SQL NTS); }
{ SQLFreeStmt(stmt, SQL DROP);
char branchname[80]; SQLDisconnect(conn);
float balance; SQLFreeConnect(conn);
int lenOut1, lenOut2; SQLFreeEnv(env);
HSTMT stmt; }
SQLAllocStmt(conn, &stmt);
char * sqlquery = ”select branch name, sum
(balance) from account group by branch
name”;
error = SQLExecDirect(stmt, sqlquery, SQL NTS);
if (error == SQL SUCCESS)
{
SQLBindCol(stmt, 1, SQL C CHAR, branchname , 80,
&lenOut1);
JDBC
 JDBC is a Java API for communicating with database systems
supporting SQL
 JDBC supports a variety of features for querying and updating data,
and for retrieving query results
 JDBC also supports metadata retrieval, such as querying about
relations present in the database and the names and types of
relation attributes
 Model for communicating with the database:
 Open a connection

 Create a “statement” object

 Execute queries using the Statement object to send queries and


fetch results
 Exception mechanism to handle errors
JDBC Code
public static void JDBCexample(String dbid, String ResultSet rset = stmt.executeQuery(”select branch
userid, String passwd) name, avg (balance) from account
{ group by branch name”);
try while (rset.next())
{ {
Class.forName (”oracle.jdbc.driver.OracleDriver”); System.out.println(rset.getString(”branch name”) + ”
Connection conn = DriverManager.getConnection( ” +rset.getFloat(2));
”jdbc:oracle:thin:@aura.bell-labs.com: }
2000:bankdb”,userid, passwd); stmt.close();
Statement stmt = conn.createStatement(); conn.close();
try }
{ catch (SQLException sqle)
stmt.executeUpdate(”insert into account values( {
’A-9732’, ’Perryridge’, 1200)”); System.out.println(”SQLException : ” + sqle);
} }
catch (SQLException sqle) }
{
System.out.println(”Could not insert tuple. ” + sqle);
}
Client–Server Architectures
 Centralized database systems are those
that run on a single computer system and
do not interact with other computer
systems.
 Client–server systems, on the other hand,
have functionality split between a server
system, and multiple client systems.
A centralized computer system
Client–Server Systems
Distributed Databases
It consists of a collection of sites, connected together via
some kind of communication network in which
 Each site is a full database system site on its own right,

 The sites have agreed to work together so that a auser at


any site can access data anywhere in the network exactly
as if the data were all stored at the user’s own site
Reasons for building distributed
database systems
 sharing of data
 Autonomy
 availability
Objectives
1. Local autonomy
2. No reliance on a central site (bottleneck, vulnerability)
3. Continuous operation (reliability, availability)
4. Location independence
5. Fragmentation independence
6. Replication independence
7. Distributed Query Processing (optimization)
8. Distributed Transaction Management (concurrency, recovery)
9. Hardware independence
10. OS independence
11. Network independence
12. DBMS independence

Вам также может понравиться