Vous êtes sur la page 1sur 21

JDBC

Database
Programming
Presented By: Mr. Angelo P. Del Rosario

Java Database Connectivity


(JDBC)
JDBCis a Java database connectivity technology
(Java Standard Edition platform) fromOracle
Corporation.
This
technology
is
an
API
(Application
Programming
Interface)for
theJava
programming languagethat defines how a client
may access adatabase.
It provides methods for querying and updating data
in a database.

Creating JDBC Application:


There are following six steps involved in building a JDBC
application:
Import the packages .Requires that you include the packages
containing the JDBC classes needed for database programming.
Most often, usingimport java.sql.*will suffice.
Register the JDBC driver .Requires that you initialize a driver
so you can open a communications channel with the database.
Open a connection .Requires using
theDriverManager.getConnection()method to create a
Connection object, which represents a physical connection with
the database.

Creating JDBC Application:


cont..
Execute a query .Requires using an object of type
Statement for building and submitting an SQL
statement to the database.
Extract data from result set .Requires that you use
the appropriateResultSet.getXXX()method to
retrieve the data from the result set.
Clean up the environment .Requires explicitly
closing all database resources versus relying on the
JVM's garbage collection.

NetBeans CRUD
Application
Integrating CRUD Functionality.

Getting Started
Before we start we need to install the
following requirements:
MySQL Server
JDK (Java Development Kit)
NetBeans
MySQL Connector/J Driver
http://www.mysql.com/products/connector/

Creating the Database


Database Name: studentdb
Table Name: student
Field Name:
No.
1
2
3
4

Name
studno
studlname
studfname
studage

Type
int
varchar(30)
varchar(30)
int

Designing the User Interface

Creating a class and


method for database
connection

Importing the class sql class

import java.sql.*;

Declaring the Variables


Connection conn = null;

Creating a method for the


connection
public static Connection ConnectDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/dbase",userID",pword");
JOptionPane.showMessageDialog(null,"Successfully Connected To Database Server");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}

Creating a JFrame Form

JFrame Form Design

Declaring the Variables in


JFrame Form
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
String strSQL="";

Open the database connection

conn = MySQLConnection.ConnectDB();

Integrating CRUD Functionality:


Create
strSQL ="INSERT INTO student VALUES(?,?,?,?)";
try{
pst = conn.prepareStatement(strSQL);
pst.setString(1, txtsno.getText());
pst.setString(2, txtlname.getText());
pst.setString(3, txtfname.getText());
pst.setString(4, txtage.getText());
pst.execute();
JOptionPane.showMessageDialog(null,"Record Saved");
clearField();
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);

strSQL = "SELECT * FROM student WHERE studno=?";

Integrating CRUD try{


Functionality: Read

pst = conn.prepareStatement(strSQL);
pst.setString(1, txtsearch.getText());
rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null,"Record Found");
txtsno.setText(rs.getString("studno"));
txtlname.setText(rs.getString("lastname"));
txtfname.setText(rs.getString("firstname"));
txtage.setText(rs.getString("age"));
}else{
JOptionPane.showMessageDialog(null,"Record Not Found");
}

}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}

Integrating CRUD Functionality:


Update
strSQL = "UPDATE student SET lastname=?,firstname=?,age=? WHERE studno=?";
try{
pst = conn.prepareStatement(strSQL);
pst.setString(1, txtlname.getText());
pst.setString(2, txtfname.getText());
pst.setString(3, txtage.getText());
pst.setString(4, txtsno.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Record Updated");
clearField();
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}

Integrating CRUD Functionality:


Delete
strSQL = "DELETE FROM student WHERE studno=?";
try{
pst = conn.prepareStatement(strSQL);
pst.setString(1, txtsno.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Record Deleted");
clearField();
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}

THANK YOU..