Sunteți pe pagina 1din 16

JDBC WITH MYSQL

Group

Members:
Syed Hassan Shah (11F-BEIT-29)
Saifullah Khan
(11F-BEIT-31)

Introduction to JDBC
JDBC

is used for accessing databases from


Java applications

An

interface to communicate with a


relational database

Treat

database tables/rows/columns as Java


objects

Introduction to JDBC
JDBC

is a means of accessing SQL databases


from Java

JDBC

is also a specification for how third-party


vendors should write database drivers to access
specific SQL versions

JDBC Architecture

Connecting to the Database


Every

database is identified by a URL

Given

a URL, Driver Manager looks for the


driver that can talk to the corresponding
database

Driver

Manager tries all registered drivers,

until a suitable one is found

Interaction with the Database


We

use Statement objects in order to

Query

the database

Update
All

the database

are interfaces, hence cannot be instantiated

They

are created by the Connection

Querying with Statement


The execute query method returns a Result
Set object representing the query result.

Result Set

Result Set objects provide access to the tables

generated as results of executing a Statement queries


Only

one Result Set per Statement can be open at the


same time!

The

table rows are retrieved in sequence

A Result Set
The next()

maintains a cursor pointing to its current row

method moves the cursor to the next row

JDBC Steps:
1.

Connect to database

2.

Query database (or insert/update/delete)

3.

Process results

4.

Close connection to database

1. Connect to database
Load

JDBC driver
Class.forName("com.mysql.jdbc.Driver").ne
wInstance();
Make connection
Connection conn =
DriverManager.getConnection(url);
The

format of a database URL is:

String

DB_URL =
"jdbc:mysql://dbserver:3306/world";

2. Query database
a.

b.

Create statement

Statement stmt =
conn.createStatement();

stmt object sends SQL commands to database

Send SQL statements

stmt.executeQuery(SELECT );

stmt.executeUpdate(INSERT );

3. Process results
Result

of a SELECT statement (rows/columns)


returned as a ResultSet object
ResultSet

rs =
stmt.executeQuery("SELECT * FROM
users");

Step

through each row in the result

rs.next()

Get

column values in a row

String

userid = rs.getString(userid);
int type = rs.getInt(type);

Print the users table


ResultSet rs = stmt.executeQuery("SELECT * FROM
users");
while (rs.next()) {
String userid = rs.getString(1);
String firstname = rs.getString(firstname);
String lastname = rs.getString(lastname);
String password = rs.getString(4);
int type = rs.getInt(type);
System.out.println(userid + + firstname +
+ lastname + + password + + type);
}

Add a row to the table


String str =
"INSERT INTO users
VALUES('Bob', 'Bob', 'King',
'cat', 0);
// Returns number of rows in table
int rows = stmt.executeUpdate(str);

4. Close connection
Close

the ResultSet object

rs.close();

Close

the Statement object

stmt.close();

Close

the connection

conn.close();

Thank You