Docsity

Appunti Informatica - Database access and JDBC, Appunti di Elementi di Informatica

Anteprima parziale del testo

Scarica Appunti Informatica - Database access and JDBC e più Appunti in PDF di Elementi di Informatica solo su Docsity!

Database access

and JDBC

 (^) Integrating database access into JSP applications

Goals

 (^) Access SQL DBMS’s from JSP pages  (^) JDBC technology  (^) Integrate SQL query results into the resulting HTML content  (^) Generate SQL queries according to FORM values

Architecture

JDBC scope

 (^) Standardizes  (^) Mechanism for connecting to DBMSs  (^) Syntax for sending queries  (^) Structure representing the results  (^) Does not standardize  (^) SQL syntax: dialects, variants, extensions, ...

Basic steps

 (^) Load the JDBC driver  (^) Define the connection URL  (^) Establish the connection  (^) Create a statement object  (^) Execute a query or update  (^) Process the results  (^) Close the connection

1. Loading the driver

 (^) A Driver is a DMBS-vendor provided class, that must be available to the Java application  (^) Must reside in Tomcat’s CLASSPATH  (^) The application usually doesn’t know the driver class name until run-time (to ease the migration to other DMBSs)  (^) Needs to find and load the class at run-time  (^) Class.forName method in the Java Class Loader

Types of drivers (2/3)

 (^) A net-protocol fully Java technology- enabled driver  (^) translates JDBC API calls into a DBMS- independent net protocol which is then translated to a DBMS protocol by a server. Specific protocol depends on the vendor. The most flexible alternative

Types of drivers (3/3)

 (^) A native-protocol fully Java technology- enabled driver  (^) converts JDBC technology calls into the network protocol used by DBMSs directly. Direct call from the client machine to the DBMS server. Many of these protocols are proprietary: the database vendors will be the primary source for this style of driver.

Loading the MySQL driver

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; // Notice, do not import com.mysql.jdbc.* or you will have problems! public class LoadDriver { public static void main(String[] args) { try { // The newInstance() call is a work around for some // broken Java implementations Class.forName("com.mysql.jdbc.Driver") .newInstance(); } catch (Exception ex) { // mostly ClassNotFoundException // handle the error } }

2. Define the connection URL

 (^) The Driver Manager needs some information to connect to the DBMS  (^) The database type (to call the proper Driver, that we already loaded in the first step)  (^) The server address  (^) Authentication information (user/pass)  (^) Database / schema to connect to  (^) All these parameters are encoded into a string  (^) The exact format depends on the Driver vendor

3. Establish the connection

 (^) Use DriverManager.getConnection  (^) Uses the appropriate driver according to the connection URL  (^) Returns a Connection object  (^) Connection connection = DriverManager.getConnection(URLString)  (^) Contacts DBMS, validates user and selects the database  (^) On the Connection object subsequent commands will execute queries

Example

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; try { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test? user=monty&password=greatsqldb"); // Do something with the Connection .... } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); }

5. Execute a query

 (^) Use the executeQuery method of the Statement class  (^) ResultSet executeQuery(String sql)  (^) sql contains a SELECT statement  (^) Returns a ResultSet object, that will be used to retrieve the query results

Other execute methods

 (^) int executeUpdate(String sql)  (^) For INSERT, UPDATE, or DELETE statements  (^) For other SQL statements that don’t return a resultset (e.g., CREATE TABLE)  (^) returns either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing  (^) boolean execute(String sql)  (^) For general SQL statements