Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structura bazei de date MS Access

Similar presentations


Presentation on theme: "Structura bazei de date MS Access"— Presentation transcript:

1 Structura bazei de date MS Access

2 Ce face o aplicatie JDBC?
Incarca dinamic driverelele de care este nevoie; Initiaza conexiunea cu sistemul de gestiunea a bazelor de date; Defineste contextul tranzactiilor (BEGIN,…, {COMMIT | ROLLBACK}), Verifica lock-urile; Genereaza si trimite instructiunele SQL; Receptioneaza date; Prelucreaza datele; Genereaza rezultate si mesajele de eroare; Decide daca face commit sau roll-back pe tranzactie; Se deconecteaza de la baza de date (termina sesiunea).

3 Managerul de driver JDBC
Incarca driver-ul necesar pentru comunicatei; Transfera apelurile de functii JDBC de la apliactia catre driverul corect Trateaza initializarea JDBC din aplicatie si realizeazaza cateva verificari de eororu rudimentare.

4 SGBD-ul si driverul JDBC
trebuie sa exista un driver specific pentru accesul la SGBDR care: stabileste conexiunea cu BD transfera cererile de manipulare a datelor; accepta rezultatele intoarse de cerere; transforma tipurile datelor specifice SGBDR in tipuri de date Java si mesajele de eroare. SGBDR (sistemul de gestiune al bazelor de date relationale): prelucreaza comenzile de manipulare a datelor si; intoarce datele.

5 Configurare JDBC in Windows 7/ 64

6 Aplicatia 1 Aplicatia utilizeaza JDBC pentru accesul din Java la o baza de date MS Access. Structura bazei de date este prezentata in slide-ul anterior impreuna cu modalitatea de configurarea a lucrului cu JDBC din W7/64.

7 Aplicatia 1 / 1 import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class cuJDBC { private static final String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; private static final String accessDBURLSuffix = ";DriverID=22;READONLY=false}"; //aceasta cale trebuie "adaptata" la locul in care este baza de date MS Access private static final String filename = "D:\\work\\Curs Master Java\\App\\l13-Database\\PrDb\\test.mdb"; public static Connection getAccessDBConnection(String filename) throws SQLException { filename = filename.trim(); String databaseURL = accessDBURLPrefix + filename + accessDBURLSuffix; return DriverManager.getConnection(databaseURL, "", ""); } public static void afisare(ResultSet resultSet) throws SQLException { String out=""; for(int i=1;i<=resultSet.getMetaData().getColumnCount();++i) out+=resultSet.getMetaData().getColumnName(i) + " "; System.out.println(out); while(resultSet.next()) { out=""; out+=resultSet.getString(i) + " ";

8 Aplicatia 1 / 2 public static void main(String[] args) { try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection =getAccessDBConnection(filename); DatabaseMetaData dbMetaData = connection.getMetaData(); String productName = dbMetaData.getDatabaseProductName(); System.out.println("Baza de date: " + productName); String productVersion = dbMetaData.getDatabaseProductVersion(); System.out.println("Versiune: " + productVersion); Statement statement = connection.createStatement(); System.out.println("\nSelectie * instructiuni"); String query = "SELECT * FROM Table1"; ResultSet resultSet = statement.executeQuery(query); afisare(resultSet); java.sql.Date dataazi =new java.sql.Date(new java.util.Date().getTime()); String ins ="INSERT INTO Table1" + "([program],[instructiune],[parametri],[data],[real]) VALUES('program2','linie','x1=1;x2=2',#"+ dataazi+"#, 1211)"; System.out.println(ins); statement.executeUpdate(ins); System.out.println("\nSelectie numai instructiuni program2"); query = "SELECT * FROM Table1 WHERE [program]='program2'"; resultSet = statement.executeQuery(query); System.out.println("\nSterge linii din Table1"); query = "DELETE * FROM Table1 WHERE [real]=1211"; int rowsEffected= statement.executeUpdate(query); System.out.println(rowsEffected + " rows effected");

9 Aplicatia 1 / 3 System.out.println("\nActualizare linii din Table1");
query = "UPDATE Table1 SET [real] = WHERE [program] = 'program1'"; rowsEffected = statement.executeUpdate(query); System.out.println(rowsEffected + " rows effected"); statement.close(); connection.close(); } catch(ClassNotFoundException e) { System.err.println("Eroare la incarcarea de driver: " + e); } catch (SQLException e) { System.err.println("Exceptie SQL: " + e); }

10 Aplicatia 1 / 4 Baza de date: ACCESS Versiune: 2.0
Selectie * instructiuni ID program instructiune parametri data real 1 program1 linie x1=10,y1=10;x2=20,y2= :00: 2 program1 arc x1=10,y1=20;r= :00: 3 program1 grip open :00: 4 program2 linie x1=1;x2= :00: 5 program2 linie x1=1;x2= :00: 6 program2 linie x1=1;x2= :00: 7 program2 linie x1=1;x2= :00: 8 program2 linie x1=1;x2= :00: 9 program2 linie x1=1;x2= :00: 10 program2 linie x1=1;x2= :00: 11 program2 linie x1=1;x2= :00: INSERT INTO Table1([program],[instructiune],[parametri],[data],[real]) VALUES('program2','linie','x1=1;x2=2',# #, 1211) Selectie numai instructiuni program2 32 program2 linie x1=1;x2= :00: Sterge linii din Table1 1 rows effected Actualizare linii din Table1 3 rows effected

11 Bibliografie > Java: course, IDE (JDeveloper), JDK and JRE, JDeveloper labs. > Getting Started With the JDeveloper IDE


Download ppt "Structura bazei de date MS Access"

Similar presentations


Ads by Google