JDBC Notes
JDBC Notes
JDBC stands for java database connectivity. It is a standard API provided by Oracle for java
application to interact with different set of databases.
What is API?
Why JDBC?
To store users data permanently we are using JDBC by using java programs .
Example
Class Test{
Int a =15;//this will there in memory when will execute the program after that it will be
deleted
String s = “Hello”;
Oracle
Oracle
Driver
Step-1 Go to your file location where you downloaded your database driver and
copy that path.
Step-2 Go to environment variable and set your class path go to class path and
edit new and paste your path what you copied and end with ;.;
Step-3 for checking connection open your cmd and type there
javap com.mysql.jdbc.Driver
ByUsing Eclipse:
Step-2: If you are getting classpath in libraries then clicked on classpath and
right side you will get add external jar file clicked there and browse the location
where you have downloaded the mysql connector jar file.
3) Create query:
Statement //simple query
PreparedStatement //parameterized query
CallableStatement //Stored procedure
Example:
String s = "select * from students";
Statement stmt = conn.createStatement();
ResultSet set=stmt.excecuteQuery(q); //we can write update also
4) Process the data/Execute the Query:
While(set.next())
{
int id = set.getInt("StudentID");//or we can pass number also 1 2 3 row wise
String name = set.getString("StudentName");
System.out.println(id);
System.out.println(name);
}
5) Close the Connection
Conn.close();
#Example
package jdbc_Programs;
import java.sql.*;
public class FirstJDBC {
try {
//load the driver :
Class.forName("com.mysql.cj.jdbc.Driver");//fully qualified name
//creating a connection
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "Aliya@123";
Connection conn = DriverManager.getConnection(url,username,password);
if(conn.isClosed()) {
System.out.println("Connection is still closed!!!!");
}
else {
System.out.println("connection created!!!!");
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.sql.*;
try {
//Load the Driver
Class.forName("com.mysql.cj.jdbc.Driver");
//Creating a connection
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "Aliya@123";
//create a query
String query = "create table table1(tId int(20) primary key auto_increment,"
+ "tName varchar(30) not null, tCity varchar(400))";
//create a statement
Statement stmt = conn.createStatement();
stmt.executeUpdate(query);// it will create the table in database
System.out.println("table created in database!!!");
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
#How to insert data into table by using Prepared Statement
#Example:
package jdbc_Programs;
import java.sql.*;
public class InsertDataIntoTable1 {
//create a connection
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "Aliya@123";
}
}
}
#inserting Data to Table with dynamic input JDBC
Here we have to use buffer reader for inserting dynamic data in the table
package jdbc_Programs;
import java.io.*;
import java.sql.*;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//create a connection
System.out.println("Enter City");
pstmt.setString(1, name);
pstmt.setString(2, city);
pstmt.executeUpdate();
System.out.println("Inserted....");
conn.close();
}catch(Exception e)
e.printStackTrace();
Create table images (id int primary key auto_increment, pic blob)
package jdbc_Programs;
import java.io.FileInputStream;
import java.sql.*;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//create a connection
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","Aliya@123");
//ptsmt.setString(1, "values");
ptsmt.setBinaryStream(1,fis,fis.available());
ptsmt.executeUpdate();
System.out.println("done....");
conn.close();
}catch(Exception e)
e.printStackTrace();
}
}
package jdbc_Programs;
//Singleton Class
import java.sql.Connection;
import java.sql.DriverManager;
try {
if(conn == null) {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","Aliya@123");
}catch(Exception e)
e.printStackTrace();
return conn;
#LargeImageApp.java
package jdbc_Programs;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
try
Connection c = ConnectionProvider.getConnection();
//Java swing
pstmt.setBinaryStream(1,fis,fis.available());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "success");
}catch(Exception e)
e.printStackTrace();
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.RowId;
import java.sql.Statement;
try {
Statement stmt=conn.createStatement();
while(set.next()) {
int id = set.getInt(1);
System.out.println(name+":"+id+":"+city);
}catch(Exception e) {
e.printStackTrace();
Start.java
package studentapp.crudoperation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
while(true) {
int c = Integer.parseInt(br.readLine());
if(c==1)
//add student
boolean answer=StudentDao.insertStudentToDB(st);
if(answer) {
}else {
System.out.println(st);
}else if(c==2)
//delete student
int userId=Integer.parseInt(br.readLine());
boolean f =StudentDao.deleteStudent(userId);
if(f) {
System.out.println("Deleted......");
}else {
}else if(c==3)
//display student
StudentDao.showAllStudent();
}else if(c==4)
//exit
break;
}else {
Student.java
package studentapp.crudoperation;
this.student_id = student_id;
this.student_name = student_name;
this.student_phone = student_phone;
this.student_city = student_city;
}
public Student(String student_name, String student_phone, String student_city) {
this.student_name = student_name;
this.student_phone = student_phone;
this.student_city = student_city;
return student_id;
this.student_id = student_id;
return student_name;
this.student_name = student_name;
return student_phone;
this.student_phone = student_phone;
return student_city;
}
public void setStudent_city(String student_city) {
this.student_city = student_city;
@Override
#ConnectionGenerator.java
package studentapp.crudoperation;
import java.sql.Connection;
import java.sql.DriverManager;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
return conn;
#StudentDao.java
package studentapp.crudoperation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
boolean f = false;
//Jdbc Code....
try {
//prepared statement
pstmt.setString(1, st.getStudent_name());
pstmt.setString(2, st.getStudent_phone());
pstmt.setString(3,st.getStudent_city());
//excecute...
pstmt.executeUpdate();
f = true;
}catch(Exception e)
e.printStackTrace();
return f;
boolean f = false;
//Jdbc Code....
try {
//prepared statement
PreparedStatement pstmt = con.prepareStatement(insert);
pstmt.setInt(1,userId);
//excecute...
pstmt.executeUpdate();
f = true;
}catch(Exception e)
e.printStackTrace();
return f;
//Jdbc Code....
try {
while(set.next()) {
int id = set.getInt(4);
System.out.println("+++++++++++++++++++++++++++++++++");
}catch(Exception e)
e.printStackTrace();
}
Stored Procedure:
A stored procedure is a prepared SQL code that you can save, so the code can
be reused over and over again.
Delimiter $$
BEGIN
---statements---
END $$
DELIMITER;