You are on page 1of 4

visit @ http://www.infotechcomputer.

in

“Jh x.ks'kk; ue%“

‘Java Database Connectivity’


Q1. What is JDBC
Ans. JDBC stands for Java Database Connectivity, which is a standard Java API (Application Programming
Interface) for database-independent connectivity between the Java programming language and a wide range
of databases.
Important JDBC Classes

 DriverManager: This class manages a list of database drivers. Matches connection requests from the
java application with the proper database driver using communication sub protocol. The first driver
that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.

 Driver: This interface handles the communications with the database server. You will interact directly
with Driver objects very rarely. Instead, you use DriverManager objects, which manages objects of this
type. It also abstracts the details associated with working with Driver objects.

 Connection: The connection object represents communication context, i.e., all communication with
database is through connection object only.

 Statement: Object of this class is used to submit the SQL statements to the database.

 ResultSet: These objects hold data retrieved from a database after you execute an SQL query.

 SQLException: This class handles any errors that occur in a database application.

 class.forName() : The most common approach to register a driver is to use


Java's Class.forName() method, to dynamically load the driver's class file into memory.

 try and catch – both of these are used for error handling or exception handling which is necessary in
java.

o try block – in this block we write all of the statements that are prone to error.

o catch block – in this block we catch all of the errors.

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 1


visit @ http://www.infotechcomputer.in

import java.sql.* - import all of the sql library classes in the current program.
jdbc:mysql://localhost:3306/infotech","root", "123456"

jdbc – it is for java database connectivity


localhost – is the server name for the local computer
3306 – is the port number for java to mysql connection
juhi – is the database name
root – is the user name at mysql
123456 – is the password of mysql (which is specified at the time of mysql installation)

Difference Between ExecuteQuery and ExecuteUpdate


ExecuteQuery method is used for select query
whereas
executeUpdate method is used for insert, delete, update query

rs.next() – next method of ResultSet class is used to move to the next record of resultset.

Insert Update

Statement stmt; Statement stmt;

try try

{ {

Class.forName("java.sql.Driver"); Class.forName("java.sql.Driver");

} }

catch(Exception e) catch(Exception e)

{ {

System.out.println(e); System.out.println(e);

} }

try

try {

{ Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:330
Connection con =
6/juhi","root", "123456");
DriverManager.getConnection("jdbc:mysql://localhost:330
6/juhi","root", "123456"); int r,m,r1;

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 2


visit @ http://www.infotechcomputer.in
int r,m; String n;

String n; r=Integer.parseInt(jtfroll.getText());

r=Integer.parseInt(jtfroll.getText()); n=jtfname.getText();

n=jtfname.getText(); m=Integer.parseInt(jtfmarks.getText());

m=Integer.parseInt(jtfmarks.getText()); r1=Integer.parseInt(jtfsearch.getText());

String query; String query;

query="insert into student query="update student set


values("+r+",'"+n+"',"+m+")"; roll="+r+",name='"+n+"',marks="+m+" where roll="+r1;

stmt=con.createStatement(); // connectoin class ke stmt=con.createStatement(); // connectoin class ke


object con ko use karte hue statement create karo object con ko use karte hue statement create karo

stmt.executeUpdate(query); // query ko execute stmt.executeUpdate(query); // query ko execute


karo karo

JOptionPane.showMessageDialog(null,"Record JOptionPane.showMessageDialog(null,"Record
Saved"); Updated");

} catch(Exception e) { } catch(Exception e) {

System.out.println(e); System.out.println(e);

} }

Delete Search

Statement stmt; Statement stmt;

try ResultSet rs;

{ try {

Class.forName("java.sql.Driver"); Class.forName("java.sql.Driver");

} }

catch(Exception e) catch(Exception e) {

{ System.out.println(e);

System.out.println(e); }

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 3


visit @ http://www.infotechcomputer.in
} try {

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:330
try 6/juhi","root", "123456");
{ int r,m,r1;
Connection con = String n;
DriverManager.getConnection("jdbc:mysql://localhost:330
6/juhi","root", "123456"); r1=Integer.parseInt(jtfsearch.getText());

int r,m,r1; String query;

String n; query="select * from student where roll="+r1;

r1=Integer.parseInt(jtfsearch.getText()); stmt=con.createStatement(); // connectoin class ke


object con ko use karte hue statement create karo
String query;
rs=stmt.executeQuery(query); // query ko execute
karo
query="delete from student where roll="+r1; if(rs.next()) {
stmt=con.createStatement(); // connectoin class ke
r=rs.getInt("roll");
object con ko use karte hue statement create karo
n=rs.getString("name");
stmt.executeUpdate(query); // query ko execute
karo m=rs.getInt("marks");

JOptionPane.showMessageDialog(null,"Record jtfroll.setText(""+r);
Deleted");
jtfname.setText(""+n);

jtfmarks.setText(""+m);
} catch(Exception e) {
}
System.out.println(e);
else {
}
JOptionPane.showMessageDialog(null,"Record Not
Found"); }

} catch(Exception e) {

System.out.println(e);

INFOTECH Computer Education, Kishangarh (Raj)  9829171122 Page 4

You might also like