1 / 27

Embedded SQL i Java

Embedded SQL i Java. nikos dimitrakas nikosd@kth.se 08-162099 rum 6626. Connolly/Begg (3rd edition) Kapitel 21 + 28.8 (4th edition) Kapitel 29.7 + Appendix E (5th edition) Kapitel 30.7.1, Appendix I. Vad är embedded SQL?. Värdspråk / Host Language Valfritt programmingsspråk Databas

cosima
Download Presentation

Embedded SQL i Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Embedded SQL i Java nikos dimitrakas nikosd@kth.se 08-162099 rum 6626 Connolly/Begg (3rd edition) Kapitel 21 + 28.8 (4th edition) Kapitel 29.7 + Appendix E (5th edition) Kapitel 30.7.1, Appendix I

  2. Vad är embedded SQL? Värdspråk / Host Language Valfritt programmingsspråk Databas En SQL-databas (relationsdatabas) Embedded SQL Hjälper värdspråket kommunicera med databasen.

  3. Varför embedded SQL? Avancerad logik Gränssnitt mot databasen / Applikation Jobba med flera databaser

  4. Java embedded SQL Javaprogram JDBC-driver (JDBC-ODBC bridge + ODBC-driver) Databas

  5. Java embedded SQLArkitektur Java Application JDBC driver manager JDBC/ODBC bridge JDBC Driver (DBMS Specific) ODBC Driver DBMS

  6. JDBC-programsekvens 1. Importerta paket 2. Registrera JDBC-Driver 3. Öppna en Connection till databasen 4. Skapa en Statement (eller PreparedStatement) 5. Exekvera en SQL-sats och ta emot resultatet (ett ResultSet) om resultat finns 6. Jobba med resultatet (om det finns) (tillbaka till punkt 4) 7. Stäng ResultSet och Statement (tillbaka till punkt 4) 8. Stäng Connection

  7. Exempeldatabas

  8. En URL (”databasadress”) består av tre delar: jdbc:databastyp/ODBC:databasnamn/ODBC-alias jdbc:mysql:///labb jdbc:odbc:labb ODBC-alias kan skapas i ODBC Manager (”Data Sources (ODBC)” i Administrative Tools i Control Panel eller genom att köra programmet odbcad32.exe som finns i windows\system32): Lämpligt för databaser som inte har en Java-driver, t ex MS Access Connection URL / ODBC-alias

  9. 1. Importera Paket //Import packages import java.sql.*; //JDBC packages //other packages import java.util.*; … java.sql package specification: http://java.sun.com/javase/6/docs/api/

  10. //Load MySQL driver String driver = "com.mysql.jdbc.Driver"; Class.forName (driver); //Load JDBC-ODBC bridge driver Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); 2. Registrera JDBC-Driver

  11. // DB access variables String URL = "jdbc:mysql:///labb"; String userID = "root"; String password = "secretpassword"; //create a connection to the database Connection con; con = DriverManager.getConnection(URL, userID, password); 3. Öppna en Connection till databasen

  12. // Create a statement associated to the connection con. // The new statement is placed in the variable stmt. Statement stmt; stmt = con.createStatement(); 4. Skapa en Statement (eller PreparedStatement)

  13. String query; ResultSet rs; // Set the SQL statement into the query variable query = "SELECT stad, COUNT(*) AS antal FROM person GROUP BY stad"; // Execute the SQL statement that is stored in the variable query // and store the result in the variable rs. rs = stmt.executeQuery(query); 5. Exekvera en SQL-sats och ta emot resultatet

  14. // Loop through the result set and print the results. // The method next() returns false when there are no more rows. while (rs.next()) { System.out.print("Stad: " + rs.getString("stad")); System.out.println(" Antal personer: " + rs.getInt("antal")); } 6. Jobba med resultatet

  15. // Close the variable stmt and release all resources // bound to it. // Any ResultSet associated to the Statement will be // automatically closed too. stmt.close(); 7. Stäng ResultSet och Statement

  16. // Set the SQL statment into the query variable String query; query = "SELECT fnamn, enamn, stad FROM person WHERE id IN (SELECT agare FROM bil WHERE marke = ?)"; // Create a statement associated to the connection and the query. // The new statement is placed in the variable stmt. PreparedStatement stmt; stmt = con.prepareStatement(query); 4. Skapa en Statement (eller PreparedStatement)

  17. String markeparam = “FORD”; // Provide the value for the first ? in the SQL statement. // The value of the variable markeparam will be sent to the // database manager through the variables stmt and con. stmt.setString(1, markeparam); // Execute the SQL statement that is prepared in the // variable stmt and store the result in the variable rs. ResultSet rs; rs = stmt.executeQuery(); 5. Exekvera en SQL-sats och ta emot resultatet

  18. // Loop through the result set and print the results. // The method next() returns false when there are no more rows. while (rs.next()) { System.out.println(rs.getString("fnamn")+" "+rs.getString("enamn")+" "+rs.getString("stad")); } 6. Jobba med resultatet

  19. // Close the variable stmt and release all resources // bound to it. // Any ResultSet associated to the Statement will be // automatically closed too. stmt.close(); 7. Stäng ResultSet och Statement

  20. String query; // Set the SQL statement into the query variable query = "INSERT INTO person (fnamn, enamn, stad) VALUES (?, ?, ?)"; // Create a statement associated to the connection and the query. // The new statement is placed in the variable stmt. PreparedStatement stmt; stmt = con.prepareStatement(query); 4. Skapa en Statement (eller PreparedStatement)

  21. String fnamnparam; String enamnparam; String stadparam; // Provide the values for the ?'s in the SQL statement. // The value of the variable fnamnparam is first, // enamnparam is second and stadparam is third. stmt.setString(1, fnamnparam); stmt.setString(2, enamnparam); stmt.setString(3, stadparam); // Execute the SQL statement that is prepared in the variable stmt stmt.executeUpdate(); 5. Exekvera en SQL-sats

  22. // Close the variable stmt and release all resources // bound to it. stmt.close(); 7. Stäng Statement

  23. // Close the connection con.close(); 8. Stäng Connection

  24. Datatyper Datatyper i databasen måste kunna mappas till datatyper/klasser i Java: SQL integer  Java int SQL number, real, etc  Java float/double SQL varchar, char, string, etc  Java String SQL date, time, timestamp, etc  Java java.sql.Date, java.sql.Time, java.sql.Timestamp Läs mer i API för java.sql.ResultSet

  25. Mer information JDBC (SUN): http://java.sun.com/products/jdbc/overview.html

  26. Labb QUIZ 4 bör göras först. MySQL Hämta databasskriptet! Kör databasskriptet för att skapa databasen! Access Hämta databasen! Skapa ett ODBC-alias! Hämta exempelprogrammet! Kompilera och testa exempelprogrammet! Implementera 3 nya frågor! Kompilera det nya programmet! Kör programmet och visa koden för en av handledarna!

  27. Demo Hämta MySQL-databasskriptet! Kör databasskriptet för att skapa databasen! Hämta exempelprogrammet (MySQL-versionen)! Kompilera och testa exempelprogrammet! Hämta Access-versionen av databasen Skapa ett ODBC-alias till databasen Hämta exempelprogrammet (ODBC-versionen)! Kompilera och testa exempelprogrammet! Testa MySQL via ODBC!

More Related