Presentation is loading. Please wait.

Presentation is loading. Please wait.

IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 1 Embedded SQL i Java nikos dimitrakas 08-162099 rum 6626 Connolly/Begg(3rd edition) Kapitel 21.

Similar presentations


Presentation on theme: "IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 1 Embedded SQL i Java nikos dimitrakas 08-162099 rum 6626 Connolly/Begg(3rd edition) Kapitel 21."— Presentation transcript:

1 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 1 Embedded SQL i Java nikos dimitrakas rum 6626 Connolly/Begg(3rd edition) Kapitel (4th edition) Kapitel Appendix E (5th edition) Kapitel , Appendix I

2 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 3 Varför embedded SQL? Avancerad logik Gränssnitt mot databasen / Applikation Jobba med flera databaser

4 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 4 Java embedded SQL Javaprogram JDBC-driver (JDBC-ODBC bridge + ODBC-driver) Databas

5 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 5 Java embedded SQL Arkitektur Java Application JDBC driver manager DBMS JDBC/ODBC bridge ODBC Driver JDBC Driver (DBMS Specific)

6 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 7 Exempeldatabas

8 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 9 1. Importera Paket //Import packages import java.sql.*; //JDBC packages //other packages import java.util.*; … java.sql package specification:

10 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS Öppna en Connection till databasen // 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);

12 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS Skapa en Statement (eller PreparedStatement) // Create a statement associated to the connection con. // The new statement is placed in the variable stmt. Statement stmt; stmt = con.createStatement();

13 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS Skapa en Statement (eller PreparedStatement) // 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);

17 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS Skapa en Statement (eller PreparedStatement) 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);

21 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 22 // Close the variable stmt and release all resources // bound to it. stmt.close(); 7. Stäng Statement

23 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 23 // Close the connection con.close(); 8. Stäng Connection

24 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 25 Mer information JDBC (SUN):

26 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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 IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 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! 27


Download ppt "IV1351 ht2010 nikos dimitrakas KTH/ICT/SCS 1 Embedded SQL i Java nikos dimitrakas 08-162099 rum 6626 Connolly/Begg(3rd edition) Kapitel 21."

Similar presentations


Ads by Google