Вы находитесь на странице: 1из 3

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.

import java.sql.* - import all of the sql library classes in the current program.
jdbc:mysql://localhost:3306/juhi","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 = Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/juhi","root DriverManager.getConnection("jdbc:mysql://localhost:3306/juhi","root
", "123456"); ", "123456");
int r,m; int r,m,r1;
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());
String query; r1=Integer.parseInt(jtfsearch.getText());
query="insert into student values("+r+",'"+n+"',"+m+")"; String query;
stmt=con.createStatement(); // connectoin class ke object con ko
use karte hue statement create karo query="update student set
stmt.executeUpdate(query); // query ko execute karo roll="+r+",name='"+n+"',marks="+m+" where roll="+r1;
JOptionPane.showMessageDialog(null,"Record Saved"); stmt=con.createStatement(); // connectoin class ke object con ko
use karte hue statement create karo
} catch(Exception e) { stmt.executeUpdate(query); // query ko execute karo
System.out.println(e); JOptionPane.showMessageDialog(null,"Record Updated");
}
} catch(Exception 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);
}
try
{ try
Connection con = {
DriverManager.getConnection("jdbc:mysql://localhost:3306/juhi","root Connection con =
", "123456"); DriverManager.getConnection("jdbc:mysql://localhost:3306/juhi","root
int r,m,r1; ", "123456");
String n; int r,m,r1;
String n;
r1=Integer.parseInt(jtfsearch.getText());
String query; r1=Integer.parseInt(jtfsearch.getText());
String query;
query="delete from student where roll="+r1; query="select * from student where roll="+r1;
stmt=con.createStatement(); // connectoin class ke object con ko stmt=con.createStatement(); // connectoin class ke object con ko
use karte hue statement create karo use karte hue statement create karo
stmt.executeUpdate(query); // query ko execute karo rs=stmt.executeQuery(query); // query ko execute karo
JOptionPane.showMessageDialog(null,"Record Deleted"); if(rs.next())
{
} catch(Exception e) { r=rs.getInt("roll");
System.out.println(e); n=rs.getString("name");
} m=rs.getInt("marks");
jtfroll.setText(""+r);
jtfname.setText(""+n);
jtfmarks.setText(""+m);
}
else
{
JOptionPane.showMessageDialog(null,"Record Not Found");
}

} catch(Exception e) {
System.out.println(e);
}

Вам также может понравиться