SlideShare une entreprise Scribd logo
1  sur  40
JDBC – J ava  D ata b ase  C onnectivity Modified slides  from Dr. Yehoshua Sagiv
Introduction to JDBC ,[object Object],[object Object],[object Object],[object Object]
JDBC Architecture Java Application JDBC Oracle DB2 MySQL Oracle  Driver DB2 Driver MySQL  Driver Network We will  use this one…
JDBC Architecture (cont.) ,[object Object],[object Object],[object Object],[object Object],[object Object],Application JDBC Driver
JDBC Driver for Oracle ,[object Object],[object Object]
Seven Steps ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Loading the Driver ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
An Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],imaginary1 imaginary2 Registered Drivers MySQL
Connecting to the Database ,[object Object],[object Object],[object Object]
Connecting to the Database ,[object Object],[object Object],imaginary1 imaginary2 Registered Drivers Oracle    acceptsURL( "jdbc:imaginaryDB1" )? ,[object Object]
Interaction with the Database ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Querying with Statement ,[object Object],[object Object],String  queryStr  =  "SELECT * FROM employee "   + "WHERE lname = ‘Wong'" ; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery( queryStr );
Changing DB with Statement String  deleteStr  =  "DELETE FROM employee "   + "WHERE lname = ‘Wong'" ; Statement stmt = con.createStatement(); int delnum = stmt.executeUpdate( deleteStr ); ,[object Object],[object Object]
About Prepared Statements ,[object Object],[object Object],[object Object],[object Object],[object Object]
Querying with  PreparedStatement String  queryStr  =  "SELECT * FROM employee "   + "WHERE superssn=  ?  and salary >  ? " ; PreparedStatement pstmt =  con.prepareStatement( queryStr ); pstmt.setString( 1 ,   "333445555" ); pstmt.setInt( 2 , 26000); ResultSet rs = pstmt.executeQuery();
Updating with  PreparedStatement String deleteStr =  “ DELETE FROM employee "   + "WHERE superssn = ? and salary > ?" ;   PreparedStatement pstmt =  con.prepareStatement(deleteStr); pstmt.setString(1,  "333445555" ); pstmt.setDouble(2, 26000); int delnum = pstmt.executeUpdate();
Statements vs. PreparedStatements: Be Careful! ,[object Object],String  val  =   "abc" ; PreparedStatement pstmt =  con.prepareStatement( "select * from R where A=?" ); pstmt.setString(1,  val ); ResultSet rs =  pstmt.executeQuery(); String val =   "abc" ; Statement stmt =  con.createStatement( ); ResultSet rs =  stmt.executeQuery( "select * from R where A="  + val);
Statements vs. PreparedStatements: Be Careful! ,[object Object],[object Object],PreparedStatement pstmt =  con.prepareStatement( "select * from ?" ); pstmt.setString(1, myFavoriteTableString);
Timeout ,[object Object],[object Object],[object Object]
ResultSet ,[object Object],[object Object],[object Object],[object Object],[object Object]
ResultSet Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
ResultSet Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
ResultSet Methods ,[object Object]
ResultSet Example Statement stmt = con.createStatement(); ResultSet  rs  = stmt.  executeQuery( "select lname,salary from employees" );      // Print the result while ( rs .next()) {  System.out.print(rs.getString(1) +  ":" );  System.out.println(rs.getDouble( “salary" )); }
Mapping Java Types to SQL Types ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Null Values ,[object Object],[object Object],[object Object],[object Object],[object Object]
Null Values ,[object Object],[object Object],[object Object]
ResultSet Meta-Data  ResultSetMetaData  rsmd  = rs.getMetaData(); int  numcols  =  rsmd .getColumnCount(); for (int i = 1 ; i <=  numcols ; i++) { System.out.print( rsmd .getColumnLabel(i)+ &quot; &quot; ); } A  ResultSetMetaData  is an object that can be used to get information about the properties of the columns in a  ResultSet  object An example: write the columns of the result set
Database Time ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Cleaning Up After Yourself ,[object Object],con.close(); stmt.close(); pstmt.close(); rs.close()
Dealing With Exceptions ,[object Object],catch (SQLException e) { while (e != null) { System.out.println(e.getSQLState()); System.out.println(e.getMessage()); System.out.println(e.getErrorCode()); e = e.getNextException(); } }
Transactions and JDBC ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example ,[object Object],PreparedStatement pstmt =  con.prepareStatement( &quot;update BankAccount    set amount = amount + ?   where accountId = ?&quot; ); pstmt.setInt(1,-100);  pstmt.setInt(2, 13); pstmt.executeUpdate(); pstmt.setInt(1, 100);  pstmt.setInt(2, 72); pstmt.executeUpdate(); What happens if this update fails?
Transaction Management ,[object Object],[object Object],[object Object],[object Object],[object Object]
AutoCommit ,[object Object],[object Object],[object Object],setAutoCommit( boolean val )
Scrollable ResultSet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Scrollable ResultSet (cont’d) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Scrollable ResultSet (cont’d) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Scrollable ResultSet (cont’d) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JDBC Usage in Industry ,[object Object],[object Object],[object Object],[object Object],[object Object]

Contenu connexe

Tendances

Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 

Tendances (20)

Wrapper class
Wrapper classWrapper class
Wrapper class
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Java Streams
Java StreamsJava Streams
Java Streams
 

En vedette

Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
backdoor
 
Java JDBC Communication and Connection Manager
Java JDBC Communication and Connection ManagerJava JDBC Communication and Connection Manager
Java JDBC Communication and Connection Manager
aashish
 

En vedette (20)

Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Jdbc
JdbcJdbc
Jdbc
 
Database Connectivity with JDBC
Database Connectivity with JDBCDatabase Connectivity with JDBC
Database Connectivity with JDBC
 
java Jdbc
java Jdbc java Jdbc
java Jdbc
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Jdbc Dao it-slideshares.blogspot.com
Jdbc Dao it-slideshares.blogspot.comJdbc Dao it-slideshares.blogspot.com
Jdbc Dao it-slideshares.blogspot.com
 
Jdbc in java
Jdbc in javaJdbc in java
Jdbc in java
 
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)
 
Java Swing
Java SwingJava Swing
Java Swing
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
Java lezione 10
Java lezione 10Java lezione 10
Java lezione 10
 
Java JDBC Communication and Connection Manager
Java JDBC Communication and Connection ManagerJava JDBC Communication and Connection Manager
Java JDBC Communication and Connection Manager
 

Similaire à JDBC – Java Database Connectivity

Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
phanleson
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
leminhvuong
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
phanleson
 

Similaire à JDBC – Java Database Connectivity (20)

Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
JDBC for CSQL Database
JDBC for CSQL DatabaseJDBC for CSQL Database
JDBC for CSQL Database
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.ppt
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
JDBC in Servlets
JDBC in ServletsJDBC in Servlets
JDBC in Servlets
 
Lecture17
Lecture17Lecture17
Lecture17
 
jdbc
jdbcjdbc
jdbc
 

Plus de Information Technology (20)

Web303
Web303Web303
Web303
 
Sql Server Security Best Practices
Sql Server Security Best PracticesSql Server Security Best Practices
Sql Server Security Best Practices
 
SAN
SANSAN
SAN
 
SAN Review
SAN ReviewSAN Review
SAN Review
 
SQL 2005 Disk IO Performance
SQL 2005 Disk IO PerformanceSQL 2005 Disk IO Performance
SQL 2005 Disk IO Performance
 
RAID Review
RAID ReviewRAID Review
RAID Review
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Sql 2005 high availability
Sql 2005 high availabilitySql 2005 high availability
Sql 2005 high availability
 
IIS 7: The Administrator’s Guide
IIS 7: The Administrator’s GuideIIS 7: The Administrator’s Guide
IIS 7: The Administrator’s Guide
 
MOSS 2007 Deployment Fundamentals -Part2
MOSS 2007 Deployment Fundamentals -Part2MOSS 2007 Deployment Fundamentals -Part2
MOSS 2007 Deployment Fundamentals -Part2
 
MOSS 2007 Deployment Fundamentals -Part1
MOSS 2007 Deployment Fundamentals -Part1MOSS 2007 Deployment Fundamentals -Part1
MOSS 2007 Deployment Fundamentals -Part1
 
Clustering and High Availability
Clustering and High Availability Clustering and High Availability
Clustering and High Availability
 
F5 beyond load balancer (nov 2009)
F5 beyond load balancer (nov 2009)F5 beyond load balancer (nov 2009)
F5 beyond load balancer (nov 2009)
 
WSS 3.0 & SharePoint 2007
WSS 3.0 & SharePoint 2007WSS 3.0 & SharePoint 2007
WSS 3.0 & SharePoint 2007
 
SharePoint Topology
SharePoint Topology SharePoint Topology
SharePoint Topology
 
Sharepoint Deployments
Sharepoint DeploymentsSharepoint Deployments
Sharepoint Deployments
 
Microsoft Clustering
Microsoft ClusteringMicrosoft Clustering
Microsoft Clustering
 
Scalable Internet Servers and Load Balancing
Scalable Internet Servers and Load BalancingScalable Internet Servers and Load Balancing
Scalable Internet Servers and Load Balancing
 
Web Hacking
Web HackingWeb Hacking
Web Hacking
 
Migration from ASP to ASP.NET
Migration from ASP to ASP.NETMigration from ASP to ASP.NET
Migration from ASP to ASP.NET
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 

JDBC – Java Database Connectivity

  • 1. JDBC – J ava D ata b ase C onnectivity Modified slides from Dr. Yehoshua Sagiv
  • 2.
  • 3. JDBC Architecture Java Application JDBC Oracle DB2 MySQL Oracle Driver DB2 Driver MySQL Driver Network We will use this one…
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. Querying with PreparedStatement String queryStr = &quot;SELECT * FROM employee &quot; + &quot;WHERE superssn= ? and salary > ? &quot; ; PreparedStatement pstmt = con.prepareStatement( queryStr ); pstmt.setString( 1 , &quot;333445555&quot; ); pstmt.setInt( 2 , 26000); ResultSet rs = pstmt.executeQuery();
  • 16. Updating with PreparedStatement String deleteStr = “ DELETE FROM employee &quot; + &quot;WHERE superssn = ? and salary > ?&quot; ; PreparedStatement pstmt = con.prepareStatement(deleteStr); pstmt.setString(1, &quot;333445555&quot; ); pstmt.setDouble(2, 26000); int delnum = pstmt.executeUpdate();
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. ResultSet Example Statement stmt = con.createStatement(); ResultSet  rs  = stmt. executeQuery( &quot;select lname,salary from employees&quot; );      // Print the result while ( rs .next()) {  System.out.print(rs.getString(1) +  &quot;:&quot; );  System.out.println(rs.getDouble( “salary&quot; )); }
  • 25.
  • 26.
  • 27.
  • 28. ResultSet Meta-Data ResultSetMetaData rsmd = rs.getMetaData(); int numcols = rsmd .getColumnCount(); for (int i = 1 ; i <= numcols ; i++) { System.out.print( rsmd .getColumnLabel(i)+ &quot; &quot; ); } A ResultSetMetaData is an object that can be used to get information about the properties of the columns in a ResultSet object An example: write the columns of the result set
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

Notes de l'éditeur

  1. Lobs and DDL