SlideShare a Scribd company logo
2
Most read
5
Most read
6
Most read
Java Database Connectivity(JDBC) UsingMySql
ADVANCE JAVA PROGRAMING
Subject Code (3360701)
Presented By- Dattani Dhyey-136250307505
Java database connectivity with MySql
JDBC Two TierArchitecture
• Java Application talks
directly to the database.
• Accomplished through the
JDBC driver which sends
commands directly to the
database.
• Results sent back directly to
the application
Application Space
Java Application
JDBC Driver
Database
SQL
Command
Result
Set
JDBC Three Tier Architecture
• JDBC driver sends
commands to a middle
tier, which in turn sends
commands to database.
• Results are sent back to
the middle tier, which
communicates them back
to the application
Application Space
Java Application
JDBC Driver
Database
SQL
Command
Result
Set
Application Server
(middle-tier)
Proprietary
Protocol
The JDBC API
 The JDBC API stands for Java Database Connectivity Application Programming Interface. It
allows an application written in java to communicate and interacts with database.
 It allows JAVA application to:
1) Create and open connection with database.
2) Specify and executes various SQL queries against database.
3) Retrieve records from database.
The JDBC API defines various classes and interfaces to communicate with database.
 The JDBC classes are defined inside java.sql package.
JDBC Components
Interface Purpose
Driver Is used to create a connection object using connect()
method.
Connection Is used to monitor and maintain database sessions.
createStatement() method is used create statement.
Statement Is used to execute SQL statements and retrieve records
from database.
ResultSet Is used to retrieve records that are returned by
executing SQL query.
1) The java.sql package :
 The java.sql package contains set of classes and interfaces that are used to
communicate with database.
 Following are most common interfaces of java.sql package.
JDBC Components
Class Purpose
DriverManager Is used to manage multiple drivers. And also used to
load and register the JDBC drivers and establish
connection with database. The getconnection() method
of DriverManager class is used to create connection
object.
SQLException This class handles any errors that occur in a database
application.
 Following are most common classes of java.sql package.
JDBC Components
2) JDBC Test Suite:
 The JDBC driver test suite helps you to determine that JDBC drivers will run your program. These
tests are not comprehensive or exhaustive, but they do exercise many of the important features in
the JDBC API.
3) JDBC-ODBC Bridge :
 The Java Software bridge provides JDBC access via ODBC drivers. Note that you need to load
ODBC binary code onto each client machine that uses this driver. As a result, the ODBC driver is
most appropriate on a corporate network where client installations are not a major problem, or for
application server code written in Java in a three-tier architecture.
JDBC-ODBC Bridge
 Advantages Of JDBC.
o Can read any database.
o Creates XML structure of data from database.
o No content conversion
o Query and stored procedure supported.
 Disadvantages Of JDBC.
o Not good for large project.
o It needs specific database queries.
o Multiple connections may have complexities
o Exception handling is a big issue with JDBC.
JDBC-ODBC Bridge
JDBC Drivers
 JDBC Driver is a software component that enables java application to interact with the
database.
 To help you understand what different drivers require, the following driver categorization
system id defined :-
o Type 1: JDBC-ODBC Bridge driver (Bridge).
o Type 2: Native-API/partly Java driver (Native).
o Type 3: All Java/Net-protocol driver (Middleware).
o Type 4: All Java/Native-protocol driver (Pure).
Java database connectivity with MySql
Java database connectivity with MySql
Java database connectivity with MySql
Type2: Native-API,PartlyJavaDriver
• Native-API driver converts
JDBC commands into
DBMS-specific native calls
• Directly interfaces with the
database
Application Space
Java Application
Type 2 JDBC Driver
Database
SQL
Command
Result
Set
Native Database
Library
Proprietary
Protocol
Java database connectivity with MySql
Java database connectivity with MySql
Type4: Native-Protocol,PureJavaDriver
 Pure Java drivers that
communicate directly with the
vendor’s database
 JDBC commands converted to
database engine’s native protocol
directly
 Advantage: no additional
translation or middleware layer
 Improves performance
Application Space
Java Application
Type 4 JDBC Driver
Database
SQL Command
Using Proprietary
Protocol
Result Set
Using Proprietary
Protocol
Step-1 : Import JAVA SQl statement.
o import.java.sql.*;
Creating Database
Step-2 : Load and Register JDBC driver.
o Syntax : Class.forName (“Driver Name”);
Step-3 : Establish Connection with Database.
o Syntax : Connection conn= DriverManager.getConnection (“URL”, “Username”, ”Password”);
Step-4 : Create Statement.
o Statement stmt = conn.createstatement();
Step-5 : Execute Query.
o ResultSet rs= stmt.executeQuery("SELECT * FROM STUDENT");
o stmt.executeUpdate("INSERT INTO STUDENT VALUES(7,'abc','Chennai')”);
Step-6 : Retrieve Results (applied for select query)
o while(rs.next())
{
int id = rs.getInt("enroll");
String name= rs.getString("name");
String city= rs.getString("city");
System.out.println(id+"tt");
System.out.println(name+"tt");
System.out.println(city+"tt");
}
Step-7 : Closing Connection and Statement.
o conn.close();
ostmt.close();
Continued…..
// Step-1 : Import java.sql package
import java.sql.*;
public class database
{
public static void main(String args[])
{
Connection conn= null;
Statement stmt= null;
try
{
//Step-2: Load and register the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//Step-3 : Establish connection with Database.
System.out.println("Trying to connect with Database");
conn= DriverManager.getConnection("jdbc:mysql://localhost/","root","");
System.out.println("Connection Established Successfully");
//Step-4 : Create Statement.
System.out.println("Trying to create Database");
//Step-5 : Execute Query.
stmt=conn.createStatement();
String sql= "CREATE DATABASE jdemo";
stmt.executeUpdate(sql);
System.out.println("Database created successfully");
//Step-6: Close Connection.
conn.close();
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.sql.*;
public class dbpreparestmt
{
public static void main(String args[])
{
Connection conn = null;
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Trying to connect with Database");
conn=DriverManager.getConnection("jdbc:mysql://localhost/jdemo","root","");
System.out.println("Connection Established Successfully");
System.out.println("Trying to insert data in table");
stmt = conn.createStatement();
PreparedStatement pst=conn.prepareStatement("INSERT INTO dhyey
VALUES(?,?,?)");
Insertion Using PrepareStatement
pst.setInt(1,7057);
pst.setString(2,"raj");
pst.setString(3,"gujrat");
pst.executeUpdate();
System.out.println("Data inserted successfully");
conn.close();
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Ad

Recommended

JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
Types of Drivers in JDBC
Types of Drivers in JDBC
Hemant Sharma
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
FAKHRUN NISHA
 
Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)
Som Prakash Rai
 
3 Tier Architecture
3 Tier Architecture
guestd0cc01
 
Database Systems Concepts, 5th Ed
Database Systems Concepts, 5th Ed
Daniel Francisco Tamayo
 
INTERNSHIP REPORT
INTERNSHIP REPORT
Mufaddal Vasi
 
Client server s/w Engineering
Client server s/w Engineering
Rajan Shah
 
Java packages
Java packages
Raja Sekhar
 
JDBC
JDBC
People Strategists
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Spring Data JPA
Spring Data JPA
Knoldus Inc.
 
Java database connectivity with MYSQL
Java database connectivity with MYSQL
Adil Mehmoood
 
Oops concepts in php
Oops concepts in php
CPD INDIA
 
Express js
Express js
Manav Prasad
 
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Php and MySQL
Php and MySQL
Tiji Thomas
 
SQLITE Android
SQLITE Android
Sourabh Sahu
 
Spring Boot and REST API
Spring Boot and REST API
07.pallav
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Java database connectivity
Java database connectivity
Vaishali Modi
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
Quick flask an intro to flask
Quick flask an intro to flask
juzten
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Nodejs functions & modules
Nodejs functions & modules
monikadeshmane
 
JDBC.ppt
JDBC.ppt
ChagantiSahith
 
Advanced JAVA
Advanced JAVA
Rajvi Vaghasiya
 

More Related Content

What's hot (20)

Java packages
Java packages
Raja Sekhar
 
JDBC
JDBC
People Strategists
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Spring Data JPA
Spring Data JPA
Knoldus Inc.
 
Java database connectivity with MYSQL
Java database connectivity with MYSQL
Adil Mehmoood
 
Oops concepts in php
Oops concepts in php
CPD INDIA
 
Express js
Express js
Manav Prasad
 
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Php and MySQL
Php and MySQL
Tiji Thomas
 
SQLITE Android
SQLITE Android
Sourabh Sahu
 
Spring Boot and REST API
Spring Boot and REST API
07.pallav
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Java database connectivity
Java database connectivity
Vaishali Modi
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
Quick flask an intro to flask
Quick flask an intro to flask
juzten
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Nodejs functions & modules
Nodejs functions & modules
monikadeshmane
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Java database connectivity with MYSQL
Java database connectivity with MYSQL
Adil Mehmoood
 
Oops concepts in php
Oops concepts in php
CPD INDIA
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Spring Boot and REST API
Spring Boot and REST API
07.pallav
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Java database connectivity
Java database connectivity
Vaishali Modi
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
Quick flask an intro to flask
Quick flask an intro to flask
juzten
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Nodejs functions & modules
Nodejs functions & modules
monikadeshmane
 

Similar to Java database connectivity with MySql (20)

JDBC.ppt
JDBC.ppt
ChagantiSahith
 
Advanced JAVA
Advanced JAVA
Rajvi Vaghasiya
 
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
kingkolju
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
NaveenKumar648465
 
Jdbc introduction
Jdbc introduction
Rakesh Kumar Ray
 
JDBC
JDBC
Manjunatha RK
 
Chap3 3 12
Chap3 3 12
Hemo Chella
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Pooja Talreja
 
Chapter_4_-_JDBC[1].pptx
Chapter_4_-_JDBC[1].pptx
BachaSirata
 
Core jdbc basics
Core jdbc basics
Sourabrata Mukherjee
 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Java database connectivity
Java database connectivity
Vaishali Modi
 
Jdbc new
Jdbc new
sumit kushwah
 
Jdbc
Jdbc
Mumbai Academisc
 
java database connection (jdbc)
java database connection (jdbc)
Sanjay Gunjal
 
JDBC Presentation with JAVA code Examples.pdf
JDBC Presentation with JAVA code Examples.pdf
ssuser8878c1
 
jdbc document
jdbc document
Yamuna Devi
 
Unit 5.pdf
Unit 5.pdf
saturo3011
 
Jdbc
Jdbc
Mumbai Academisc
 
Java Database Connectivity by shreyash simu dbce.pptx
Java Database Connectivity by shreyash simu dbce.pptx
ash909077
 
Ad

Recently uploaded (20)

Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
Ad

Java database connectivity with MySql

  • 1. Java Database Connectivity(JDBC) UsingMySql ADVANCE JAVA PROGRAMING Subject Code (3360701) Presented By- Dattani Dhyey-136250307505
  • 3. JDBC Two TierArchitecture • Java Application talks directly to the database. • Accomplished through the JDBC driver which sends commands directly to the database. • Results sent back directly to the application Application Space Java Application JDBC Driver Database SQL Command Result Set
  • 4. JDBC Three Tier Architecture • JDBC driver sends commands to a middle tier, which in turn sends commands to database. • Results are sent back to the middle tier, which communicates them back to the application Application Space Java Application JDBC Driver Database SQL Command Result Set Application Server (middle-tier) Proprietary Protocol
  • 5. The JDBC API  The JDBC API stands for Java Database Connectivity Application Programming Interface. It allows an application written in java to communicate and interacts with database.  It allows JAVA application to: 1) Create and open connection with database. 2) Specify and executes various SQL queries against database. 3) Retrieve records from database. The JDBC API defines various classes and interfaces to communicate with database.  The JDBC classes are defined inside java.sql package.
  • 6. JDBC Components Interface Purpose Driver Is used to create a connection object using connect() method. Connection Is used to monitor and maintain database sessions. createStatement() method is used create statement. Statement Is used to execute SQL statements and retrieve records from database. ResultSet Is used to retrieve records that are returned by executing SQL query. 1) The java.sql package :  The java.sql package contains set of classes and interfaces that are used to communicate with database.  Following are most common interfaces of java.sql package.
  • 7. JDBC Components Class Purpose DriverManager Is used to manage multiple drivers. And also used to load and register the JDBC drivers and establish connection with database. The getconnection() method of DriverManager class is used to create connection object. SQLException This class handles any errors that occur in a database application.  Following are most common classes of java.sql package.
  • 8. JDBC Components 2) JDBC Test Suite:  The JDBC driver test suite helps you to determine that JDBC drivers will run your program. These tests are not comprehensive or exhaustive, but they do exercise many of the important features in the JDBC API. 3) JDBC-ODBC Bridge :  The Java Software bridge provides JDBC access via ODBC drivers. Note that you need to load ODBC binary code onto each client machine that uses this driver. As a result, the ODBC driver is most appropriate on a corporate network where client installations are not a major problem, or for application server code written in Java in a three-tier architecture.
  • 9. JDBC-ODBC Bridge  Advantages Of JDBC. o Can read any database. o Creates XML structure of data from database. o No content conversion o Query and stored procedure supported.  Disadvantages Of JDBC. o Not good for large project. o It needs specific database queries. o Multiple connections may have complexities o Exception handling is a big issue with JDBC.
  • 11. JDBC Drivers  JDBC Driver is a software component that enables java application to interact with the database.  To help you understand what different drivers require, the following driver categorization system id defined :- o Type 1: JDBC-ODBC Bridge driver (Bridge). o Type 2: Native-API/partly Java driver (Native). o Type 3: All Java/Net-protocol driver (Middleware). o Type 4: All Java/Native-protocol driver (Pure).
  • 15. Type2: Native-API,PartlyJavaDriver • Native-API driver converts JDBC commands into DBMS-specific native calls • Directly interfaces with the database Application Space Java Application Type 2 JDBC Driver Database SQL Command Result Set Native Database Library Proprietary Protocol
  • 18. Type4: Native-Protocol,PureJavaDriver  Pure Java drivers that communicate directly with the vendor’s database  JDBC commands converted to database engine’s native protocol directly  Advantage: no additional translation or middleware layer  Improves performance Application Space Java Application Type 4 JDBC Driver Database SQL Command Using Proprietary Protocol Result Set Using Proprietary Protocol
  • 19. Step-1 : Import JAVA SQl statement. o import.java.sql.*; Creating Database Step-2 : Load and Register JDBC driver. o Syntax : Class.forName (“Driver Name”); Step-3 : Establish Connection with Database. o Syntax : Connection conn= DriverManager.getConnection (“URL”, “Username”, ”Password”); Step-4 : Create Statement. o Statement stmt = conn.createstatement();
  • 20. Step-5 : Execute Query. o ResultSet rs= stmt.executeQuery("SELECT * FROM STUDENT"); o stmt.executeUpdate("INSERT INTO STUDENT VALUES(7,'abc','Chennai')”); Step-6 : Retrieve Results (applied for select query) o while(rs.next()) { int id = rs.getInt("enroll"); String name= rs.getString("name"); String city= rs.getString("city"); System.out.println(id+"tt"); System.out.println(name+"tt"); System.out.println(city+"tt"); } Step-7 : Closing Connection and Statement. o conn.close(); ostmt.close(); Continued…..
  • 21. // Step-1 : Import java.sql package import java.sql.*; public class database { public static void main(String args[]) { Connection conn= null; Statement stmt= null; try { //Step-2: Load and register the JDBC driver Class.forName("com.mysql.jdbc.Driver"); //Step-3 : Establish connection with Database. System.out.println("Trying to connect with Database"); conn= DriverManager.getConnection("jdbc:mysql://localhost/","root",""); System.out.println("Connection Established Successfully"); //Step-4 : Create Statement. System.out.println("Trying to create Database");
  • 22. //Step-5 : Execute Query. stmt=conn.createStatement(); String sql= "CREATE DATABASE jdemo"; stmt.executeUpdate(sql); System.out.println("Database created successfully"); //Step-6: Close Connection. conn.close(); stmt.close(); } catch(SQLException se) { se.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }
  • 23. import java.sql.*; public class dbpreparestmt { public static void main(String args[]) { Connection conn = null; Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("Trying to connect with Database"); conn=DriverManager.getConnection("jdbc:mysql://localhost/jdemo","root",""); System.out.println("Connection Established Successfully"); System.out.println("Trying to insert data in table"); stmt = conn.createStatement(); PreparedStatement pst=conn.prepareStatement("INSERT INTO dhyey VALUES(?,?,?)"); Insertion Using PrepareStatement