Class Presentations On Object Oriented Programming by Subhash Chand Agrawal
Class Presentations On Object Oriented Programming by Subhash Chand Agrawal
JDBC
Class Presentations on Object Oriented Programming
by Subhash Chand Agrawal
Software and drivers
Jdk 1.8
https://www.oracle.com/in/java/technologies/javase/javas
e-jdk8-downloads.html
NetBeans
https://download.netbeans.org/netbeans/8.0/final/bundle
s/netbeans-8.0-windows.exe
MySQL 5.5
https://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.41-
winx64.msi
JDBC driver
https://static.javatpoint.com/src/jdbc/mysql-connector.jar
Database Concepts
Database − A database is a collection of tables,
with related data.
Table − A table is a matrix with data. A table in a
database looks like a simple spreadsheet.
Column − One column (data element) contains data
of one and the same kind, for example the column
rollno.
Row − A row (= tuple, entry or record) is a group
of related data, for example the data of one
subscription.
DataTypes
Numeric Data Type
INT
FLOAT(m,d)
DOUBLE(m,d)
String Data Types:
VARCHAR(size)
Open your MySQL Command Line Client; it should
have appeared with a mysql> prompt.
Check the already created databases with show
databases command:
show databases;
MySQL Create Database
mysql> CREATE DATABASE subhash;
Create statement
Execute queries
Close connection
1) Register the driver class
"jdbc:oracle:thin:@localhost:1521:xe","system","pas
sword");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/subhash","root","root");
Table
create table employee
(
id int,
name varchar(20),
age int,
salary float(10,4),
dept varchar(20)
);
Method Description
executes the query. It is used for create,
public int executeUpdate()
drop, insert, update, delete etc.
executes the select query. It returns an
public ResultSet executeQuery()
instance of ResultSet.
5) Close the connection object
"jdbc:mysql://localhost:3306/subhash","root",“Subhash");
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into employee
values(3553,'Subhash2',12,56.12,'physics')");
System.out.println("Data Inserted Successfully");
con.close();
}}
PreparedStatement
Use this when you plan to use the SQL statements
many times.
The PreparedStatement interface accepts input
parameters at runtime.
The PreparedStatement interface is a subinterface
of Statement. It is used to execute parameterized
query.
PreparedStatement stmt=con.prepareStatement("insert
into employee values(?,?,?,?,?)");
stmt.setInt(1,101);//1 specifies the first parameter in
the query
stmt.setString(2,"Ratan");
stmt.setFloat(3,79000.66f);
stmt.setInt(4,27);
stmt.setString(5,"Chemistry");
int i=stmt.executeUpdate();
Methods of PreparedStatement interface
Method Description
public void setInt(int paramIndex, int sets the integer value to the given
value) parameter index.
public void setString(int paramIndex, sets the String value to the given
String value) parameter index.
public void setFloat(int paramIndex, sets the float value to the given
float value) parameter index.
public void setDouble(int paramIndex, sets the double value to the given
double value) parameter index.
executes the query. It is used for create,
public int executeUpdate()
drop, insert, update, delete etc.
executes the select query. It returns an
public ResultSet executeQuery()
instance of ResultSet.
CallableStatement
Use this when you want to access the database
stored procedures.
The CallableStatement interface can also accept
runtime input parameters.
How to get the instance of CallableStatement?
Call test1();
JDBC Driver
Class.forName("com.mysql.jdbc.Driver");
1) JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to
the database.
The JDBC-ODBC bridge driver converts JDBC method calls into
the ODBC function calls.
In Java 7, the JDBC-ODBC Bridge has been removed.
1) JDBC-ODBC bridge driver
Class.forName("com.mysql.jdbc.Driver");
Advantages:
easy to use.
can be easily connected to any database.
Disadvantages:
Performance degraded because JDBC method call is
converted into the ODBC function calls.
The ODBC driver needs to be installed on the client machine.
Type 2
In a Type 2 driver, JDBC API calls are converted
into native C/C++ API calls, which are unique to the
database.
These drivers are typically provided by the
database vendors and used in the same manner as
the JDBC-ODBC Bridge.
The vendor-specific driver must be installed on each
client machine.
If we change the Database, we have to change the
native API.
2) Native-API driver