SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
JDBC	
  

            Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
JDBC	
  Intro	
  
•  Java	
  Database	
  Connec@vity	
  in	
  Java	
  since	
  1.1	
  
•  API	
  in	
  Java	
  for	
  accessing	
  Databases	
  
•  PlaDorm	
  independent,	
  Database	
  independent	
  
Drivers	
  
•  To	
  access	
  a	
  database,	
  you	
  need	
  a	
  driver	
  
•  To	
  code	
  in	
  Java	
  is	
  always	
  the	
  same,	
  changing	
  
   the	
  driver	
  changes	
  the	
  connec@on	
  to	
  different	
  
   database.	
  
•  Lot’s	
  of	
  drivers	
  for	
  different	
  databases:	
  
   MySQL,	
  Text,	
  MS	
  SQL	
  Server..	
  
Driver	
  Types	
  
•  JDBC	
  Drivers	
  are	
  divided	
  into	
  four	
  categories	
  
     –  Type	
  1	
  that	
  calls	
  na@ve	
  code	
  of	
  the	
  locally	
  available	
  ODBC	
  
        driver.	
  
     –  Type	
  2	
  that	
  calls	
  database	
  vendor	
  na.ve	
  library	
  on	
  a	
  client	
  side.	
  	
  
     –  Type	
  3,	
  the	
  pure-­‐java	
  driver	
  that	
  talks	
  with	
  the	
  server-­‐side	
  
        middleware	
  that	
  then	
  talks	
  to	
  database.	
  
     –  Type	
  4,	
  the	
  pure-­‐java	
  driver	
  that	
  uses	
  database	
  na@ve	
  protocol.	
  	
  
•  In	
  most	
  cases	
  you	
  can	
  access	
  the	
  same	
  database	
  with	
  four	
  
   different	
  type	
  of	
  drivers	
  
•  List	
  of	
  drivers:	
  
     –  hQp://developers.sun.com/product/jdbc/drivers	
  
To	
  use	
  JDBC	
  
1.    Register	
  driver	
  
2.    Access	
  database	
  
3.    Do	
  some	
  SQL	
  magic	
  
4.    Handle	
  result	
  
5.    Close	
  the	
  connec@on	
  
1.	
  Register	
  the	
  Driver	
  
try {
    // The driver string here is given you by the
    // driver documentation.
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e) {
    System.out.println(”Can’t find the driver!");
}
2.	
  Connect	
  to	
  Database	
  
try {
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection(
     "jdbc:odbc:mydatabasename", ”userlogin", ”password");
}
catch(SQLException e) {

}
catch(ClassNotFoundException e) {

}
3.	
  Some	
  SQL	
  Magic	
  
try {
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection(
      "jdbc:odbc:mydatabasename", ”userlogin", ”password");

    Statement statement = conn.createStatement();
    statement.executeUpdate("DELETE FROM employees WHERE id<20");

}
catch(SQLException e) { }
catch(ClassNotFoundException e) { }
5.	
  Close	
  connec@on	
  
try {
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection(
      "jdbc:odbc:mydatabasename", ”userlogin", ”password");

    Statement statement = conn.createStatement();
    statement.executeUpdate("DELETE FROM employees WHERE id<20");

    statement.close();
    conn.close();
}
catch(SQLException e) { }
catch(ClassNotFoundException e) { }
Handle	
  Result?	
  
•  In	
  previous	
  example,	
  the	
  result	
  of	
  the	
  SQL	
  was	
  
   boolean	
  value	
  (we	
  did	
  not	
  handle	
  it).	
  
•  If	
  SQL	
  is	
  select,	
  then	
  you	
  can	
  retrieve	
  the	
  
   results	
  
•  Use	
  ResultSet	
  object.	
  ResultSet	
  holds	
  the	
  
   result	
  of	
  your	
  SQL	
  query	
  
•  You	
  can	
  navigate	
  in	
  result	
  set	
  
4.	
  Handling	
  Results	
  
try {
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection(
      "jdbc:odbc:mydatabasename", ”userlogin", ”password");

     Statement statement = conn.createStatement();
     ResultSet rs = statement.executeQuery("SELECT * FROM employees");

     while(rs.next()) {
        System.out.println(rs.getString("someColumn"));
     }

    statement.close();
    conn.close();
}
catch(SQLException e) {
}
catch(ClassNotFoundException e) {

}
ResultSet	
  
rs.next();	
  
rs.next();	
  
rs.next();	
  
//	
  Prints	
  “Williams”	
  
System.out.println(rs.getString(“Last	
  Name”));	
  
ResultSetMetaData	
  
•  Don’t	
  know	
  column	
  names?	
  Or	
  amount?	
  
•  You	
  can	
  check	
  these	
  using	
  ResultSetMetaData	
  
•  Example	
  usage:	
  	
  
	
  
     ResultSetMetaData rsmd = rs.getMetaData();
     int numCols = rsmd.getColumnCount ();

     while (rs.next()) {
         for (int i=1; i<=numCols; i++) {
             System.out.println(rs.getString(i));
         }
     }
Transac@ons	
  
•  If	
  you	
  want	
  to	
  commit	
  several	
  sql	
  –	
  commands	
  
   into	
  one:	
  
    –  conn.setAutoCommit(false);
    –  // do some sql
    –  conn.commit();
    –  conn.rollback();
Prepared	
  Statements	
  
•  Prepared	
  statement	
  or	
  parameterized	
  
   statement	
  is	
  a	
  feature	
  used	
  to	
  execute	
  the	
  same	
  
   or	
  similar	
  database	
  statements	
  repeatedly	
  with	
  
   high	
  efficiency.	
  
•  Example	
  
    Connection con = connect();
    String sql = "UPDATE table1 set one = ?, two = ?";
    PreparedStatement preStmt = con.prepareStatement(sql);
    preStmt.setInt(1, 123);
    preStmt.setString(2, "myNewValue2");
    preStmt.executeUpdate();
Scrollable	
  Resultset	
  
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

ResultSet srs = stmt.executeQuery(SQLQuery);
Values	
  
•  TYPE_FORWARD_ONLY	
  (default)	
  
•  TYPE_SCROLL_INSENSITIVE	
  
•  TYPE_SCROLL_SENSITIVE	
  –	
  all	
  updates	
  
   happens	
  immediately	
  
•  CONCUR_READONLY	
  
•  CONCUR_UPDATABLE	
  
Methods	
  for	
  Scrollable	
  ResultSet	
  
•    next()	
  
•    previous()	
  
•    beforeFirst()	
  
•    aierLast()	
  
•    absolute(int	
  x)	
  
Update	
  Result	
  
•  It’s	
  possible	
  to	
  update	
  the	
  result	
  using	
  
   methods	
  
    –  updateString()	
  
    –  updateInt()	
  
    –  updateFloat()..	
  
•  Once	
  updated,	
  call	
  updateRow(),	
  which	
  will	
  
   move	
  the	
  result	
  to	
  database	
  
Update	
  
while(rs.next()){
       System.out.println(rs.getString("Firstname"));
}

rs.previous();
rs.updateString("Firstname", args[0]);
rs.updateRow();
Adding	
  a	
  Row	
  
rs.moveToInsertRow();
rs.updateString("firstname", "Jack");
rs.updateString("lastname", "Smith");
rs.updateInt("idnumber", 20);
rs.insertRow();
Dele@ng	
  a	
  Row	
  
•  rs.last();
•  rs.deleteRow();
	
  

Contenu connexe

Tendances

Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)Craig Dickson
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database ConnectivityRanjan Kumar
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQLAdil Mehmoood
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySqlDhyey Dattani
 
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 MySqlkamal kotecha
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 

Tendances (20)

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 (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
JDBC
JDBCJDBC
JDBC
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
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
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
JDBC
JDBCJDBC
JDBC
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 

En vedette (16)

Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
02 software process_models
02 software process_models02 software process_models
02 software process_models
 
SDLC and Software Process Models
SDLC and Software Process ModelsSDLC and Software Process Models
SDLC and Software Process Models
 
Java Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet ProgramsJava Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet Programs
 
Types of Software testing
Types of  Software testingTypes of  Software testing
Types of Software testing
 
jdbc
jdbcjdbc
jdbc
 
JDBC Driver Types
JDBC Driver TypesJDBC Driver Types
JDBC Driver Types
 
Jdbc in java
Jdbc in javaJdbc in java
Jdbc in java
 
Java applets
Java appletsJava applets
Java applets
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Different type of_software_testing - copy
Different type of_software_testing - copyDifferent type of_software_testing - copy
Different type of_software_testing - copy
 
Testing concepts ppt
Testing concepts pptTesting concepts ppt
Testing concepts ppt
 

Similaire à Jdbc

JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.pptSwapnil Kale
 
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 applicationsFulvio Corno
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracleyazidds2
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programmingchhaichivon
 
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 JDBCOUM SAOKOSAL
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBCWebStackAcademy
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaPawanMM
 
Database connect
Database connectDatabase connect
Database connectYoga Raja
 

Similaire à Jdbc (20)

Jdbc
JdbcJdbc
Jdbc
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.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 oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
22jdbc
22jdbc22jdbc
22jdbc
 
JDBC in Servlets
JDBC in ServletsJDBC in Servlets
JDBC in Servlets
 
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
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBC
 
Lecture17
Lecture17Lecture17
Lecture17
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
Database connect
Database connectDatabase connect
Database connect
 
Jdbc day-1
Jdbc day-1Jdbc day-1
Jdbc day-1
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
Jdbc
JdbcJdbc
Jdbc
 

Plus de Jussi Pohjolainen

libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferencesJussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDXJussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript DevelopmentJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDXJussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 

Plus de Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 

Dernier

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
 
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 FresherRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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.pdfsudhanshuwaghmare1
 
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 Scriptwesley chun
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
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
 

Dernier (20)

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...
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
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
 
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...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 

Jdbc

  • 1. JDBC   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2. JDBC  Intro   •  Java  Database  Connec@vity  in  Java  since  1.1   •  API  in  Java  for  accessing  Databases   •  PlaDorm  independent,  Database  independent  
  • 3. Drivers   •  To  access  a  database,  you  need  a  driver   •  To  code  in  Java  is  always  the  same,  changing   the  driver  changes  the  connec@on  to  different   database.   •  Lot’s  of  drivers  for  different  databases:   MySQL,  Text,  MS  SQL  Server..  
  • 4. Driver  Types   •  JDBC  Drivers  are  divided  into  four  categories   –  Type  1  that  calls  na@ve  code  of  the  locally  available  ODBC   driver.   –  Type  2  that  calls  database  vendor  na.ve  library  on  a  client  side.     –  Type  3,  the  pure-­‐java  driver  that  talks  with  the  server-­‐side   middleware  that  then  talks  to  database.   –  Type  4,  the  pure-­‐java  driver  that  uses  database  na@ve  protocol.     •  In  most  cases  you  can  access  the  same  database  with  four   different  type  of  drivers   •  List  of  drivers:   –  hQp://developers.sun.com/product/jdbc/drivers  
  • 5. To  use  JDBC   1.  Register  driver   2.  Access  database   3.  Do  some  SQL  magic   4.  Handle  result   5.  Close  the  connec@on  
  • 6. 1.  Register  the  Driver   try { // The driver string here is given you by the // driver documentation. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e) { System.out.println(”Can’t find the driver!"); }
  • 7. 2.  Connect  to  Database   try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection( "jdbc:odbc:mydatabasename", ”userlogin", ”password"); } catch(SQLException e) { } catch(ClassNotFoundException e) { }
  • 8. 3.  Some  SQL  Magic   try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection( "jdbc:odbc:mydatabasename", ”userlogin", ”password"); Statement statement = conn.createStatement(); statement.executeUpdate("DELETE FROM employees WHERE id<20"); } catch(SQLException e) { } catch(ClassNotFoundException e) { }
  • 9. 5.  Close  connec@on   try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection( "jdbc:odbc:mydatabasename", ”userlogin", ”password"); Statement statement = conn.createStatement(); statement.executeUpdate("DELETE FROM employees WHERE id<20"); statement.close(); conn.close(); } catch(SQLException e) { } catch(ClassNotFoundException e) { }
  • 10. Handle  Result?   •  In  previous  example,  the  result  of  the  SQL  was   boolean  value  (we  did  not  handle  it).   •  If  SQL  is  select,  then  you  can  retrieve  the   results   •  Use  ResultSet  object.  ResultSet  holds  the   result  of  your  SQL  query   •  You  can  navigate  in  result  set  
  • 11. 4.  Handling  Results   try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection( "jdbc:odbc:mydatabasename", ”userlogin", ”password"); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM employees"); while(rs.next()) { System.out.println(rs.getString("someColumn")); } statement.close(); conn.close(); } catch(SQLException e) { } catch(ClassNotFoundException e) { }
  • 12. ResultSet   rs.next();   rs.next();   rs.next();   //  Prints  “Williams”   System.out.println(rs.getString(“Last  Name”));  
  • 13. ResultSetMetaData   •  Don’t  know  column  names?  Or  amount?   •  You  can  check  these  using  ResultSetMetaData   •  Example  usage:       ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount (); while (rs.next()) { for (int i=1; i<=numCols; i++) { System.out.println(rs.getString(i)); } }
  • 14. Transac@ons   •  If  you  want  to  commit  several  sql  –  commands   into  one:   –  conn.setAutoCommit(false); –  // do some sql –  conn.commit(); –  conn.rollback();
  • 15. Prepared  Statements   •  Prepared  statement  or  parameterized   statement  is  a  feature  used  to  execute  the  same   or  similar  database  statements  repeatedly  with   high  efficiency.   •  Example   Connection con = connect(); String sql = "UPDATE table1 set one = ?, two = ?"; PreparedStatement preStmt = con.prepareStatement(sql); preStmt.setInt(1, 123); preStmt.setString(2, "myNewValue2"); preStmt.executeUpdate();
  • 16. Scrollable  Resultset   Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet srs = stmt.executeQuery(SQLQuery);
  • 17. Values   •  TYPE_FORWARD_ONLY  (default)   •  TYPE_SCROLL_INSENSITIVE   •  TYPE_SCROLL_SENSITIVE  –  all  updates   happens  immediately   •  CONCUR_READONLY   •  CONCUR_UPDATABLE  
  • 18. Methods  for  Scrollable  ResultSet   •  next()   •  previous()   •  beforeFirst()   •  aierLast()   •  absolute(int  x)  
  • 19. Update  Result   •  It’s  possible  to  update  the  result  using   methods   –  updateString()   –  updateInt()   –  updateFloat()..   •  Once  updated,  call  updateRow(),  which  will   move  the  result  to  database  
  • 20. Update   while(rs.next()){ System.out.println(rs.getString("Firstname")); } rs.previous(); rs.updateString("Firstname", args[0]); rs.updateRow();
  • 21. Adding  a  Row   rs.moveToInsertRow(); rs.updateString("firstname", "Jack"); rs.updateString("lastname", "Smith"); rs.updateInt("idnumber", 20); rs.insertRow();
  • 22. Dele@ng  a  Row   •  rs.last(); •  rs.deleteRow();