02 JDBC
02 JDBC
Type 1- ODBC Drivers-DBC is a JDBC bridge that allows, among other things,
integration of Java code with a variety of DBs it supports
(such as: SQL Servers, AccessOracle, etc.)
Type 2 - Native Drivers- Drivers of this type are written in languages such as
C or C++ and allow fast integration with the various DB they are written for
Type 3 - Network drivers – the use of drivers of this kind is appropriate for
clients that want to connect to a DB through a network.
Type 4 - Java drivers – any driver written in Java. These drivers require no
client side installation, since they are written in Java, however, they may
have some performance problems.
JDBC
JDBC API consists of the following main components:
JDBC
1. JDBC Driver
2. Connection
3. Statement
4. ResultSet
A JDBC driver is set of Java classes that implement JDBC
interfaces for interacting with a specific database.
JDBC
Almost all database vendors such as MySQL, Oracle,
Microsoft SQL Server, provide JDBC drivers.
Connection Through the Connection object, Java/DB GUI can interact with
the database for creating a Statement to execute SQL queries
against tables.
• SELECT
JDBC • INSERT
• UPDATE
• DELETE
ResultSet
ResultSet is getting After querying data from the database
The typical flow of using JDBC is as follows:
JDBC
Load the mysql-connector-Java
• Choose the Add External Jar Option and bring the relevant Jar from the local
JDBC
JDBC
• SQLException
• DriverManager
Connecting to MySQL Using
JDBC Driver • Connection
1.url:
the database URL in the form jdbc:subprotocol:subname.
For MySQL, you use the jdbc:mysql://localhost:3306/mysqljdbc
JDBC
• Also you should always close the database connection once you
complete interacting with database by calling close() method of the
Connection object.
Connecting to MySQL database
JDBC
JDBC
JDBC
• For querying data, you just need to use the Statement object as follows:
Querying Data From MySQL Using
JDBC Statement stmt = conn.createStatement();
Once you have a Statement object created, you can use it to execute any
valid MySQL query like the following:
• The result is in the form of rows with columns of data based on the
SELECT statement.
Querying Data From MySQL Using JDBC
• The ResultSet object provides you with methods to traverse the result and
read the data.
JDBC • The next() method returns true and move to the next row in the ResultSet
if there are rows available, otherwise it returns false.
• You must call the next() method at least one before reading data
because before the first next() call, the ResultSet is located before the
Querying Data From MySQL Using
first row.
JDBC while (rs.next()) {
System.out.println(rs.getString("first_name") + "\t" +
rs.getString("last_name") + "\t" +
rs.getString("email"));
}
executeQuery
• Use the getDataType() method to get column data of the current row
where DataType is the data type of the column
JDBC
executeQuery
Querying Data From MySQL Using JDBC
• Add parameters into you SQL statement using placeholders in the form
of question marks (?). This helps you avoid SQL injection.
Querying Data From MySQL Using
JDBC • Reuse the PreparedStatement with new parameters in case you need to
execute the SQL statement multiple times with different parameters.
• Send the executed statement with the values for the placeholders to
JDBC MySQL by calling executeUpdate() method of the PreparedStatement
interface.
• This method takes no arguments and returns the number of row affected.
PreparedStatement
JDBC
PreparedStatement
Setting auto-commit mode