





















Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!






















(^) Integrating database access into JSP applications
(^) 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
(^) Standardizes (^) Mechanism for connecting to DBMSs (^) Syntax for sending queries (^) Structure representing the results (^) Does not standardize (^) SQL syntax: dialects, variants, extensions, ...
(^) 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
(^) 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
(^) 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
(^) 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.
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 } }
(^) 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
(^) 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
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()); }
(^) 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
(^) 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