Chapter 4 Database Connectivity

77 219 0
Chapter 4 Database Connectivity

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CHAPTER 4 DATABASE CONNECTIVITY Lecturer: Ho Tien Lam Contents  The Design of JDBC  The Structured Query Language  JDBC Installation  Basic JDBC Programming Concepts 2  Basic JDBC Programming Concepts  Query Execution  Scrollable and Updatable Result Sets  Metadata  Transactions Created by Ho Tien Lam Contents  The Design of JDBC  The Structured Query Language  JDBC Installation  Basic JDBC Programming Concepts 3  Basic JDBC Programming Concepts  Query Execution  Scrollable and Updatable Result Sets  Metadata  Transactions Created by Ho Tien Lam What Is JDBC?  JDBC is simply an API for manipulating a database.  connecting to the database  executing SQL statements 4  executing SQL statements  returning results  JDBC is an intermediary between Java language and a database. Created by Ho Tien Lam What Is JDBC? (cont.)  Portability is one of the key aspects of JDBC.  Can you imagine using Java for writing an application once and executing it on a large number of platforms — and then having to 5 number of platforms — and then having to change the code when your organization switches from Microsoft SQL Server to MySQL? Created by Ho Tien Lam Created by Ho Tien Lam 6 Fig. JDBC-to-database communication path What Is JDBC? (cont.) 7  Application programmers use the JDBC API.  Database vendors and tool providers use the JDBC Driver API.  This architecture follows the successful model Created by Ho Tien Lam  This architecture follows the successful model of Microsoft’s ODBC, which provided a C programming interface for database access. Created by Ho Tien Lam 8 JDBC vs. ODBC 9  Why not just use ODBC from Java?  Answer: You can use ODBC from Java, but with the help of JDBC in the form of the JDBC- ODBC Bridge. Created by Ho Tien Lam ODBC Bridge.  The question now becomes, "Why do you need JDBC?" JDBC vs. ODBC (cont.) 10  There are several reasons to this question:  ODBC is hard to learn. It mixes simple and advanced features together, and it has complex options even for simple queries. Created by Ho Tien Lam  ODBC is not appropriate for direct use from Java because it uses a C interface. Calls from Java to native C code have a number of drawbacks in the security, implementation, robustness, and automatic portability of applications. [...]... String dburl = "jdbc:sqlserver://localhost: 143 3;" + "database= AdventureWorks"; Connection con = DriverManager.getConnection(url, "sa", ""); MySQL Class.forName("com.mysql.jdbc.Driver"); String dburl = "jdbc:mysql://localhost:3306/" + database; Connection con = DriverManager.getConnection(url, username, password); Created by Ho Tien Lam Executing SQL Commands 24 Firstly, create a Statement object To do... select a file to save the database to (I chose "d:\java\mdbTEST.mdb") - this creates a new blank MS Access database! Click “OK" all the way out Created by Ho Tien Lam Contents 20 The Design of JDBC The Structured Query Language JDBC Installation Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets Metadata Transactions Created by Ho Tien Lam Database URLs 21 All classes... JDBC API supports both two-tier and three-tier models for database access In the two-tier model, a Java applet or application talks directly to the database This requires a JDBC driver deployed on the client Figure 1: Two-tier Architecture for Data Access Created by Ho Tien Lam Typical Uses of JDBC (cont.) 13 A user's commands are delivered to the database, and the results of those statements are sent... Metadata Transactions Created by Ho Tien Lam Database URLs 21 All classes used for JDBC programming are contained in the java.sql and javax.sql packages Database URLs: jdbc:subprotocol : other stuff subprotocol selects the specific driver for connecting to the database The format for other stuff parameter depends on the subprotocol used Look up your vendor’s documentation for the specific format Example:... data source as the server Created by Ho Tien Lam Typical Uses of JDBC (cont.) 14 Figure Three-tier Architecture for Data Access Java applet or HTML browser Client machine (GUI) HTTP, RMI, CORBA, or other calls Application Server (Java) JDBC Server machine (business logic) DBMS-proprietary protocol DBMS Created by Ho Tien Lam Database server Contents 15 The Design of JDBC The Structured Query Language... Transactions Created by Ho Tien Lam JDBC Installation 18 The JDBC/ODBC bridge driver comes with the Java 2 SDK With MS SQL Server 2000, we must install the driver to access data Instead of accessing the database directly, we can access it via a Data Source Name (DSN) Created by Ho Tien Lam JDBC Installation (cont.) 19 Set up a DSN and get a connection through that: Choose Control Panel > Administrative... Structured Query Language JDBC Installation Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets Metadata Transactions Created by Ho Tien Lam The Structured Query Language 16 A database: a bunch of named tables with rows and columns Each column has a column name The rows contain the actual data or records The Authors Table Author_ID Name Fname ALEX Alexander Christopher BROO... rs.getDouble(“Price”); There are accessors for various types: getString, getDouble,… Each accessor has two form: one with a numeric argument (column number), one with a string argument (column name) Unlike array indexes, database column numbers start at 1 Created by Ho Tien Lam Executing SQL Commands (cont.) 28 SQL data types and Java data types are not exactly the same See the below table SQL Data Type Java Data Type... java.sql.Time TIMESTAMP java.sql.Timestamp BLOB java.sql.Blob CLOB java.sql.Clob ARRAY java.sql.Array Created by Ho Tien Lam Data Types in MS Access 30 Created by Ho Tien Lam Advanced SQL Types 31 Many databases can store large objects such as images or other data Binary large objects: BLOBs Character large objects: CLOBs The getBlob and getClob methods return objects of type java.sql.Blob and java.sql.Clob... result set These objects use large data structures, and you don’t want to wait for the garbage collector to deal with them Created by Ho Tien Lam Managing Connections, Statements, and Result Sets (cont.) 34 try { Connection con = ; try { Statement stat = con.createStatement(); ResultSet result = stat.executeQuery(queryString); process query result } finally { Use the try/finally block con.close(); just . CHAPTER 4 DATABASE CONNECTIVITY Lecturer: Ho Tien Lam Contents  The Design of JDBC  The Structured Query Language  JDBC Installation  Basic JDBC Programming Concepts 2  Basic JDBC Programming. Transactions Created by Ho Tien Lam Contents  The Design of JDBC  The Structured Query Language  JDBC Installation  Basic JDBC Programming Concepts 3  Basic JDBC Programming Concepts  Query Execution . Lam What Is JDBC?  JDBC is simply an API for manipulating a database.  connecting to the database  executing SQL statements 4  executing SQL statements  returning results  JDBC is an intermediary

Ngày đăng: 13/05/2014, 11:00