Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Database Connectivity By: Abe Marji CS616 Agenda 1. Quick Review of Databases 2. What is SQL? 3. What is JDBC? 4. Advanced Functions of JDBC 5. Summary What are Databases? • A database is a collection of tables, which organizes and stores information • The columns each contain a category of information. • A table consists of rows (records) of columns (fields) • The rows contain the information for each of the categories related to an individual event Database Samples Columns (Attributes) Table 1 COL1 Rows (Data) COL3 COL4 COL5 ROW1 ROW2 Table 2 COL2 DATA DATA ROW3 ROW4 Relationship DATA DATA ROW5 ROW6 ROW7 DATA DATA COL6 What is SQL? • SQL is “Structure Query Language” • It is considered to be the standard for database handling. • SQL provides functions to View, Update, Delete, and Add data to a database. • SQL also handles the database structure such as changing the properties of its tables or adding or delete new Tables Types of SQL Calls • SELECT – Queries existing Data • UPDATE – Updates existing data • INSERT – Adds new data in a table • DELETE – Deletes data or tables • CREATE – Creates new tables Samples of SQL Queries A simple example of an SQL statement is Standard SQL Statement: Format: “Function Col from condition” “SELECT * FROM Animal“ Not Case Sensitive: “Select NaMe from ANIMAL“ Data is Case Sensitive: “SELECT * from CUSTOMERS where NAME =‘JOE’” Is different from “SELECT * from CUSTOMERS where NAME =‘Joe’” SQL Standard Data Types Java type SQL Type Values Char Int Short Long Double Double Float Byte Java.math.BigDecimal Java.math.BigDecimal Char or Character Integer Small Integer Big Integer Double Float Real Tiny Integer Numeric Decimal String 32 Bit 16 Bit 64 Bit 15 Digits 15 Digits 7 Digits 8 Bits Fixed Point with decimal Digits SQL Data Types – Other Java type SQL Type Values Boolean Java.sql.Date Java.sql.time Java.sql.datestamp Bit Date Time datestamp True/False Month/Day/Year Hour / Min / Sec Date and Time What is JDBC? • JDBC is JAVA Database Connectivity. • JAVA Applications uses JDBC to connect to a Database. • JDBC API are available from Sun Systems. • Different Database Vendors make their own JDBC Drivers. JDBC Driver Layer Model Querying through Drivers Sun’s JDBC/ODBC Bridge Vendor Specific JDBC Drivers Creating an ODBC Connection Typical JDBC Code • import java.sql.*; •Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); • a_connection = DriverManager.getConnection ("jdbc:odbc:CS616DB",“admin",“password"); • a_statement = a_connection.createStatement(); • ResultSet rs = a_statement.executeQuery("SQL Query”); Sample JDBC Code Static Connection DriverManager.getConnection (String datasource, String userid, String password) Connection a_conn = DriverManager.getConnection(“jdbc:odbc: CS616DB”, “Admin”, “password”); JDBC Exceptions • Exceptions occur in Java when errors occur in SQL code. • Catching exceptions means dealing with those errors. • You can use several methods to get further information on an SQLException: getSQLState() getErrorCode() getNext Exception() getWarnings() Flow for JDBC Exceptions Advanced Functions • JDBC has the ability to support Prepared Statements and CALL statements • Prepared Statements lets you use one sql statement to run different queries by passing it different parameters. • CALL statements allows the developer to use database queries that are created by the Database Admin. Summary • JDBC is used in applications that require database connectivity • Different Vendors support various types of JDBC Drivers. • JDBC can take advantage of advanced SQL functions/queries using the CALL and Execute procedures()