DATABASE CONNECTIVITY TO MYSQL
Introduction
=>A real life application needs to manipulate data stored in a Database.
=>A database is a collection of related data in the form of Tables. Most of the database uses SQL (Structured Query Language) to Insert, Delete, Update or retrieve stored data.
=>In order to connect a Java application to a database designed in MySQL, Oracle, Sybase, MS SQL Server etc, you need a Bridge/Interface Driver Program.
=>Java Provides JDBC (Java Database Connection) and JDBC-ODBC interface/ Driver to connect a database. JDBC is commonly used to connect MySQL database.
=>What is JDBC ?JDBC is JAVA’s Database connection
driver interface which performs the following task for the application.
(1).Establish a connection with a Database.(2).Send SQL request (Query) to a Database
Server.(3.Returns Result obtained against Query.Some RDBMS like MS Access requires
ODBC (Open Database Connection), which can be connect through JDBC-ODBC driver(jdbc.odbcbridge).
Classes used for Database ConnectivityThe Core element of JDBC is JDBC API, which consists
of a set of Java classes equipped with predefined methods to handle various data access functions such as Selecting appropriate database driver, establishing connection, submitting SQL query and processing results.
There are four main classes in the JDBC.1.Driver Manager Class : The JDBC Driver Manager
Class Loads the JDBC driver needed to access a particular data source, locates & logs on to the database & returns a Connection Objects.
2.Connection Class : The JDBC Connection class manages the communications between a java client application & a specific database (e.g. MYSQL database), including passing SQL statement to the DBMS & managing transactions.
=>3.Statement Class : The JDBC Statement class contains SQL string that are submitted to the DBMS .An SQL Select statement returns a result Set object that contains the data retreived as the result of SQL statement.
=>4.Result set class: The JDBC Result Set class provides predefined methods to access, analyze,& convert data values returned by an executed SQL select statement.
A JDBC driver must be registered with JDBC Driver Manage using Class.forName() method before establishing a connection.
CONNECTIVITY TO MYSQL FROM JAVASteps for creating Database Connectivity
Applications
There are mainly six steps :Step1: Import the packages Required for
Database Programming.Step2: Register the JDBC Driver to JDBC
Driver managerStep3: Open a connectionStep4: Execute a QueryStep5: Extract Data From Result setStep6: Close Connection.
=> Connection: A connection(represented through connection object) is the session between the application program and the database .to do anything with database, one must have a connection object.
=>Result Set: A Result set (represent by a Result Set Object) refers to a logical set of records that are fetched from the database by executing a query and made avalible to the application-program.