SlideShare a Scribd company logo
1 of 16
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

What's hot (20)

JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
DataBase Connectivity
DataBase ConnectivityDataBase Connectivity
DataBase Connectivity
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
Jdbc   Jdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
java jdbc connection
java jdbc connectionjava jdbc connection
java jdbc connection
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Jdbc
JdbcJdbc
Jdbc
 
Database and Java Database Connectivity
Database and Java Database ConnectivityDatabase and Java Database Connectivity
Database and Java Database Connectivity
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Jdbc
JdbcJdbc
Jdbc
 

Viewers also liked

Tarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfTarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfBU
 
Introduction to graph databases in term of neo4j
Introduction to graph databases in term of neo4jIntroduction to graph databases in term of neo4j
Introduction to graph databases in term of neo4jAbdullah Hamidi
 
How to take control of your l ife
How to take control of your l ifeHow to take control of your l ife
How to take control of your l ifeMashooq Jami
 
Bitcharities Cause Presentation
Bitcharities Cause PresentationBitcharities Cause Presentation
Bitcharities Cause PresentationFrancesco Rulli
 
How to speak in public
How to speak in publicHow to speak in public
How to speak in publicJawid Qaumi
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Hassen Poreya
 
Web Application Security and Awareness
Web Application Security and AwarenessWeb Application Security and Awareness
Web Application Security and AwarenessAbdul Rahman Sherzad
 
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostraFondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostraAlessandro Califano, PhD
 
Enterprise Resource Planning using Odoo/OpenERP in Afghanistan
Enterprise Resource Planning using Odoo/OpenERP in AfghanistanEnterprise Resource Planning using Odoo/OpenERP in Afghanistan
Enterprise Resource Planning using Odoo/OpenERP in AfghanistanFarshid Ghyasi
 
Architecting for failure - Why are distributed systems hard?
Architecting for failure - Why are distributed systems hard?Architecting for failure - Why are distributed systems hard?
Architecting for failure - Why are distributed systems hard?Markus Eisele
 
Monitoring Virtualized Environments
Monitoring Virtualized EnvironmentsMonitoring Virtualized Environments
Monitoring Virtualized EnvironmentsAhmad Khalid Nasrat
 

Viewers also liked (20)

Tarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfTarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdf
 
Introduction to graph databases in term of neo4j
Introduction to graph databases in term of neo4jIntroduction to graph databases in term of neo4j
Introduction to graph databases in term of neo4j
 
How to take control of your l ife
How to take control of your l ifeHow to take control of your l ife
How to take control of your l ife
 
Introduction to sql_02
Introduction to sql_02Introduction to sql_02
Introduction to sql_02
 
E learning ict4-d_presentation
E learning ict4-d_presentationE learning ict4-d_presentation
E learning ict4-d_presentation
 
Web design - Working with forms in HTML
Web design - Working with forms in HTMLWeb design - Working with forms in HTML
Web design - Working with forms in HTML
 
Bitcharities Cause Presentation
Bitcharities Cause PresentationBitcharities Cause Presentation
Bitcharities Cause Presentation
 
How to speak in public
How to speak in publicHow to speak in public
How to speak in public
 
MyCV
MyCVMyCV
MyCV
 
Simple past
Simple pastSimple past
Simple past
 
Computational Advertising
Computational AdvertisingComputational Advertising
Computational Advertising
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14
 
Web Application Security and Awareness
Web Application Security and AwarenessWeb Application Security and Awareness
Web Application Security and Awareness
 
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostraFondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
Fondo gallerie storiche romane - Destinazione Donna - catalogo della mostra
 
Google Search Console
Google Search ConsoleGoogle Search Console
Google Search Console
 
Enterprise Resource Planning using Odoo/OpenERP in Afghanistan
Enterprise Resource Planning using Odoo/OpenERP in AfghanistanEnterprise Resource Planning using Odoo/OpenERP in Afghanistan
Enterprise Resource Planning using Odoo/OpenERP in Afghanistan
 
Architecting for failure - Why are distributed systems hard?
Architecting for failure - Why are distributed systems hard?Architecting for failure - Why are distributed systems hard?
Architecting for failure - Why are distributed systems hard?
 
Mite Nroc Beebe Aeqa
Mite Nroc Beebe AeqaMite Nroc Beebe Aeqa
Mite Nroc Beebe Aeqa
 
Bootstrap day3
Bootstrap day3Bootstrap day3
Bootstrap day3
 
Monitoring Virtualized Environments
Monitoring Virtualized EnvironmentsMonitoring Virtualized Environments
Monitoring Virtualized Environments
 

Similar to Lecture 1. java database connectivity

Similar to Lecture 1. java database connectivity (20)

Jdbc
JdbcJdbc
Jdbc
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Lecture17
Lecture17Lecture17
Lecture17
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Jdbc tutorial
Jdbc tutorialJdbc tutorial
Jdbc tutorial
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.ppt
 
22jdbc
22jdbc22jdbc
22jdbc
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Jdbc day-1
Jdbc day-1Jdbc day-1
Jdbc day-1
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

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