Java Database Connectivity
Java Database Connectivity
DATABASE
SERVER
VENDOR DB
LIBRARY
Type 3: JDBC-Net pure Java
•In a Type 3 driver, a three-tier approach is CLIENT SERVER DATABASE
Server)
Driver
administration issues.
•MySQL’s Connector/J driver is a Type 4
driver.
Which Driver Should be Used?
•If any one type of database is accessed, such as Oracle, SQL Server or
MYSQL, the preferred driver type is 4.
• If multiple types of databases are accessed at the same time by the
Java application, the Type 3 is the preferred driver.
•If Type 3 or Type 4 drivers are not available for the database then
Type 2 drivers are useful in this situation.
• The Type 1 driver is used for development and testing purposes only
since it is not considered as the deployment-level driver.
Connection:
Connection interface contains concept for the admission of recognition
with a database.
The DriverManager implements the Connection interface.
A Connection object is obtained using the getConnection() function
call.
Syntax:
Emp Table
Above example selects all the tuples from the employee table defined as ‘EMP’.
res.next() method returns true until and unless it reaches the EOF and false otherwise.
PreparedStatement :
PreparedStatement remains under the Statement as a sub interface hierarchically.
It uses compilation before hand and hence execution procedure of the
PreparedStatement is comparably faster than the Statement interface, since, the
PreparedStatement is already prepared for execution.
PreparedStatement object is retrieved from the object of Connection using the following
method.
PreparedStatement has a set of setXX() functions too along with
parameters.
In setString() the index starts from 1 (means the first attribute) and not
0.
The setXX() methods, like setString(), setInt() should be called prior to
the calling of the executeUpdate() method.
CallableStatement :
It inherits the properties of the PreparedStatement and includes methods suitable for
procedure calls .
We can have business logic on the database by the use of stored procedures and
functions that will make the performance better because these are precompiled.
Suppose you need to get the age of the employee based on the date of birth, you may
create a function that receives date as the input and returns age of the employee as the
output.
The prepareCall() method of Connection interface returns the instance
of CallableStatement.
public CallableStatement prepareCall("{ call procedurename(?,?...?)}");
example:
CallableStatement stmt=con.prepareCall("{call myprocedure(?,?)}");
ResultSet :
•The ResultSet interface provides a data set in the form of a table.
• It is given as the output of the resultant data by implementing a SQL statement within
JDBC program.
•It defines an iterative process based on the “rs.next()” method returning false if the EOF
marker (End Of File marker) is reached.
RowId:
• Defines the memory location on which a row or dataset is stored.
•It returns the address of a row.
•It spots a row in a database uniquely.
•RowId is a built-in data type in JDBC particularly used during the
returning of the address of a row.
SQLException Methods:
A SQLException pops up when some conditional mismatch occurs either in the driver or
in the database itself.
If this type of exception pops up, an object of SQLException is cascaded as input to the
catch block.
The following methods are some basic common exceptions that are handled for getting
information for the thrown exception.
JDBC Data Types:
JDBC Example
A JDBC application should include the following steps:
1.Importing the different packages
2.Registration of the JDBC driver
3.Commence a connection
4.Generate a statement
5.Run a query
6.Take out required data based on the result set
7.Close result set, statement and connection (Data Cleaning)
MYSQL BASICS
One has to install MySql 5.7
MYSQL BASICS
Once installed open Mysql 5.7 Command Line Client
For instance, Oracle will have its own JDBC driver and MySQL another definitive JDBC
driver implementing enormous interfaces.
While using a given driver, it uses the model JDBC interface.
Consequently, we can generate a new JDBC driver considering the JDBC code as a black
box.
// Here dsn is the “Data Source Name” DSN stands for Data Source Name.
conn= DriverManager.getConnection(“jdbc:odbc:dsn”);
DSN Name:
creates a link between the JDBC driver and the ODBC driver (created to
connect Oracle/MySQL/SQLServer/MSAccess).
A DSN must be created prior to the execution of the program and
following are the steps for doing that in Windows versions.
The system may be a 32-bit processor or a 64-bit processor in Windows
platform.
1.Search for ODBC Data Sources app and click on one based on your
system’s processor (64 bit or 32 bit)resp.
2. Double click to open the following dialog box
3. Select “System DSN” tab. As a result, DSN can be created, connecting
databases with applications across other computer systems connected
in a network (LAN). This is very important for ‘Distributed Systems’.
4. For adding a new DSN, the ‘ADD’ button should be clicked. It opens
the following dialog showing all the drivers of different types of files
based on which a DSN can be created. Selecting a required driver, the
‘Finish’ button is clicked.
5. After clicking on the ‘Finish’ button the dialog ‘ODBC Microsoft
Access Setup’ is invoked.
6. After this ‘Select’ button should be clicked and necessary database
should be selected.
6. After this ‘Select’ button should be clicked and necessary database
should be selected.
6. Thus, the required DSN is created after clicking on ‘OK’ button.
[in our case Microsoft Access Database has been created. ]
6. Thus, the required DSN is created after clicking on ‘OK’ button.
[in our case Microsoft Access Database has been created. ]
Now you can write a program to retrieve data from MS Access database
INSERT BULK DATA Transection INTO THE TABLE USING PreparedStatement Interface
Output:
Thank You