SlideShare a Scribd company logo
Java Database Connectivity 
Nangarhar University Computer Science 
faculty 
Fall semester 2014 
Waheedullah Sulaimankhail
Overview 
1. JDBC Introduction 
2. JDBC Configuration 
3. Practical Examples with JDBC
1. JDBC introduction 
 JDBC stands for Java Database 
Connectivity, which is a standard 
Java API for database-independent 
connectivity between the Java 
programming language and a wide 
range of databases. 
 The JDBC library includes APIs for each 
of the tasks commonly associated with 
database usage
How JDBC works 
1. Registering JDBC Driver 
2. Making a connection to a database 
3. Creating SQL or MySQL statements 
4. Executing that SQL or MySQL queries 
in the database 
5. Viewing & Modifying the resulting 
records
2. JDBC Configuration 
 We will use eclipse and Microsoft Sql Server 
 So please install Eclipse and Ms Sql Server
Configuration . 
 Add Sql Server Path to environment variable.
Configuration .. 
 Download JDBC driver for Ms Sqlserver 
 Download the sqljdbc_<version>_enu.exe to a 
temporary directory. 
 Run sqljdbc_<version>_enu.exe (unzip it)
Configuration … 
 Copy sqljdbc_auth.dll 
 From this folder : …Microsoft JDBC Driver 4.0 
for SQL Serversqljdbc_4.0enuauthx64 
 To C: windows / System32 folder
Add JDBC Driver for Ms Sql Server to Project 
properties in Eclipse
3. Practical Examples with JDBC 
 Setting Up JDBC Examples 
 3 important steps to set up JDBC 
1. Write the import statements 
 import java.sql.*; 
2. Register JDBC Driver 
 Class.forName("com.microsoft.sqlserver.jdbc.SQL 
ServerDriver"); 
3. Creating connection and opening it 
 Connection con = 
DriverManager.getConnection("jdbc:sqlserver://loc 
alhost:1433;databaseName=db_name;integrated 
Security=true","User","Pw");
Executing queries with JDBC 
 After jdbc is set up we need to write queries and 
execute them. 
 In sql server we create query and execute them. 
 However in JDBC we need to create a statement 
and initialize the statement with object of 
connection class. 
 And execute the query with an object of 
statement class.
Create statement and execute create 
table query 
 Statement st = con.createStatement(); 
 String query = "CREATE TABLE student " + 
"(id INTEGER not NULL, " + " name 
VARCHAR(255), " + 
" fname VARCHAR(255), " + " grade 
Varchar(255), " + 
" PRIMARY KEY ( id ))" ; 
 St.executeUpdate(query); 
 System.out.println(“New Table with name student 
is created ”);
Creating Select Query and executing 
 Statement st= con.createStatement(); 
 String query= "SELECT id, first, last, age FROM 
Employee"; 
 ….. = st.executeQuery(query); 
 executeQuery method returns a dataset so we 
need to store the outcome in a dataset 
 We will rewrite the executeQuery method as 
above 
 ResultSet rs = st.executeQuery(query); 
 Now we need to fetch the data from our result set
Fetching data from data set 
 Rs.next() // will fetch the next record (next row) 
 To get each record we will run a while loop till the 
last record is reached 
 While(rs.next()) { 
 int id = rs.getInt("id"); // this will fetch the column 
named “id” 
 // the same for all other columns which we want 
to fetch from this record 
 System.out.println(“ID :”+id); // show the record 
 }
Clearing the Environment 
 Rs.close(); 
 St.close(); 
 Con.close();
Thanks for attention 
Download these slides from : 
https://app.box.com/s/62nb671lj6vsz23ylw7v

More Related Content

PPTX
PPT
3 database-jdbc(1)
PPT
Jdbc connectivity
PDF
Jdbc connectivity in java
PPTX
1. java database connectivity (jdbc)
PPSX
JDBC: java DataBase connectivity
PPTX
Java database connectivity with MySql
3 database-jdbc(1)
Jdbc connectivity
Jdbc connectivity in java
1. java database connectivity (jdbc)
JDBC: java DataBase connectivity
Java database connectivity with MySql

What's hot (20)

PPTX
JDBC ppt
PPTX
DataBase Connectivity
PPTX
Java DataBase Connectivity API (JDBC API)
PPT
Java database connectivity
PPT
JDBC Tutorial
PPTX
Jdbc
PPT
PPT
Java Database Connectivity
PPT
PPT
PPTX
Jdbc
PPT
java jdbc connection
PDF
Overview Of JDBC
PPT
Java database connectivity with MYSQL
PPTX
Java- JDBC- Mazenet Solution
PDF
Database and Java Database Connectivity
PPTX
Jdbc in servlets
PPTX
JDBC ppt
DataBase Connectivity
Java DataBase Connectivity API (JDBC API)
Java database connectivity
JDBC Tutorial
Jdbc
Java Database Connectivity
Jdbc
java jdbc connection
Overview Of JDBC
Java database connectivity with MYSQL
Java- JDBC- Mazenet Solution
Database and Java Database Connectivity
Jdbc in servlets
Ad

Viewers also liked (20)

PDF
Tarjumaye jadwali-quran-karim-pdf
 
PPTX
Introduction to graph databases in term of neo4j
PPTX
How to take control of your l ife
PPTX
Introduction to sql_02
PPTX
E learning ict4-d_presentation
PPTX
Web design - Working with forms in HTML
PDF
Bitcharities Cause Presentation
PPTX
How to speak in public
PDF
PPTX
Simple past
PPTX
Computational Advertising
PDF
Web app development_cookies_sessions_14
PDF
Web Application Security and Awareness
PDF
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
PDF
Google Search Console
PPT
Enterprise Resource Planning using Odoo/OpenERP in Afghanistan
PDF
Architecting for failure - Why are distributed systems hard?
PPT
Mite Nroc Beebe Aeqa
PDF
Bootstrap day3
PPTX
Monitoring Virtualized Environments
Tarjumaye jadwali-quran-karim-pdf
 
Introduction to graph databases in term of neo4j
How to take control of your l ife
Introduction to sql_02
E learning ict4-d_presentation
Web design - Working with forms in HTML
Bitcharities Cause Presentation
How to speak in public
Simple past
Computational Advertising
Web app development_cookies_sessions_14
Web Application Security and Awareness
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
Google Search Console
Enterprise Resource Planning using Odoo/OpenERP in Afghanistan
Architecting for failure - Why are distributed systems hard?
Mite Nroc Beebe Aeqa
Bootstrap day3
Monitoring Virtualized Environments
Ad

Similar to Lecture 1. java database connectivity (20)

PDF
PPT
jdbc_presentation.ppt
PPT
Jdbc
PDF
Lecture17
PPTX
Jdbc
PPTX
Jdbc ppt
PPT
Java jdbc
PPT
Jdbc (database in java)
PPTX
Amr Mohamed Abd Elhamid_JAVA_JDBCData.pptx
PDF
Jdbc tutorial
PPS
Jdbc api
PDF
PPT
JDBC Connecticity.ppt
PDF
22jdbc
PPT
Data Access with JDBC
PPT
PDF
Jdbc Complete Notes by Java Training Center (Som Sir)
PPT
Jdbc sasidhar
PDF
Jdbc[1]
PDF
JDBC programming
jdbc_presentation.ppt
Jdbc
Lecture17
Jdbc
Jdbc ppt
Java jdbc
Jdbc (database in java)
Amr Mohamed Abd Elhamid_JAVA_JDBCData.pptx
Jdbc tutorial
Jdbc api
JDBC Connecticity.ppt
22jdbc
Data Access with JDBC
Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc sasidhar
Jdbc[1]
JDBC programming

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Reach Out and Touch Someone: Haptics and Empathic Computing
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Mobile App Security Testing_ A Comprehensive Guide.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Digital-Transformation-Roadmap-for-Companies.pptx
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Bridging biosciences and deep learning for revolutionary discoveries: a compr...

Lecture 1. java database connectivity

  • 1. Java Database Connectivity Nangarhar University Computer Science faculty Fall semester 2014 Waheedullah Sulaimankhail
  • 2. Overview 1. JDBC Introduction 2. JDBC Configuration 3. Practical Examples with JDBC
  • 3. 1. JDBC introduction  JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.  The JDBC library includes APIs for each of the tasks commonly associated with database usage
  • 4. How JDBC works 1. Registering JDBC Driver 2. Making a connection to a database 3. Creating SQL or MySQL statements 4. Executing that SQL or MySQL queries in the database 5. Viewing & Modifying the resulting records
  • 5. 2. JDBC Configuration  We will use eclipse and Microsoft Sql Server  So please install Eclipse and Ms Sql Server
  • 6. Configuration .  Add Sql Server Path to environment variable.
  • 7. Configuration ..  Download JDBC driver for Ms Sqlserver  Download the sqljdbc_<version>_enu.exe to a temporary directory.  Run sqljdbc_<version>_enu.exe (unzip it)
  • 8. Configuration …  Copy sqljdbc_auth.dll  From this folder : …Microsoft JDBC Driver 4.0 for SQL Serversqljdbc_4.0enuauthx64  To C: windows / System32 folder
  • 9. Add JDBC Driver for Ms Sql Server to Project properties in Eclipse
  • 10. 3. Practical Examples with JDBC  Setting Up JDBC Examples  3 important steps to set up JDBC 1. Write the import statements  import java.sql.*; 2. Register JDBC Driver  Class.forName("com.microsoft.sqlserver.jdbc.SQL ServerDriver"); 3. Creating connection and opening it  Connection con = DriverManager.getConnection("jdbc:sqlserver://loc alhost:1433;databaseName=db_name;integrated Security=true","User","Pw");
  • 11. Executing queries with JDBC  After jdbc is set up we need to write queries and execute them.  In sql server we create query and execute them.  However in JDBC we need to create a statement and initialize the statement with object of connection class.  And execute the query with an object of statement class.
  • 12. Create statement and execute create table query  Statement st = con.createStatement();  String query = "CREATE TABLE student " + "(id INTEGER not NULL, " + " name VARCHAR(255), " + " fname VARCHAR(255), " + " grade Varchar(255), " + " PRIMARY KEY ( id ))" ;  St.executeUpdate(query);  System.out.println(“New Table with name student is created ”);
  • 13. Creating Select Query and executing  Statement st= con.createStatement();  String query= "SELECT id, first, last, age FROM Employee";  ….. = st.executeQuery(query);  executeQuery method returns a dataset so we need to store the outcome in a dataset  We will rewrite the executeQuery method as above  ResultSet rs = st.executeQuery(query);  Now we need to fetch the data from our result set
  • 14. Fetching data from data set  Rs.next() // will fetch the next record (next row)  To get each record we will run a while loop till the last record is reached  While(rs.next()) {  int id = rs.getInt("id"); // this will fetch the column named “id”  // the same for all other columns which we want to fetch from this record  System.out.println(“ID :”+id); // show the record  }
  • 15. Clearing the Environment  Rs.close();  St.close();  Con.close();
  • 16. Thanks for attention Download these slides from : https://app.box.com/s/62nb671lj6vsz23ylw7v