This is the first example seen in class using the Java Persistence API to start create the MySQL database, the script is as follows; CREATE DATABASE
school USE school
;
CREATE TABLE `students` ( `key` int (11) NOT NULL default '0 ', `name` varchar (20) default '0', `claveCarrera` varchar (45) NOT NULL, `father` varchar (45) NOT NULL, `mother` varchar (45) NOT NULL, PRIMARY KEY (`key`), KEY `claveCarrera` (`claveCarrera`), CONSTRAINT `claveCarrera` FOREIGN KEY (`claveCarrera`) REFERENCES `race` (`claveCarrera`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = InnoDB DEFAULT CHARSET = latin1;
CREATE TABLE `race` (`claveCarrera` varchar (10) NOT NULL default '0 ', `nombreCarrera` varchar (20) default '0', PRIMARY KEY (`claveCarrera`)) ENGINE = InnoDB DEFAULT CHARSET = latin1;
Since we created the database you are going to persist our data, we must now create our project in Netbeans, so we continue the following steps:
* Create a new Java Application
We will open a wizard where you specify a connection to the base data. In the section "Database connection" selected - New Database Connection - then we will see a window where you select the tab "Basic Setting"
fields take the following information:
name: (select ) - MySQL (Connector / J driver) -
Driver: com.mysql.jdbc.Driver
Database URL: jdbc: mysql: / / 127.0.0.1:3306 / school
User name: root
Password: root
and check the "Remember Password" We
"OK" and test the connection and if everything is correct add tables d enuestra database including related tables. We
and click on the button "Create Persistence Unit"
This will create the java classes representing the tables in the database on which we work.
Note that this class has the annotation: @ entity
@ table @ @ Entity Query
belonging to the Java Persistence API. Well to better understand these classes and their annotations, we had to have read the API documentation persistence:
http://java.sun.com/javaee/5/docs/tutorial/doc/bnbpy.html
Following example will create a new package on "Source Packages" and within this a new class called "Pruebap"
right click on "Source Packages", new java class and he indicated they will be in the package "test" already created the class add the following code, (See comments).
package test;
import
ejemplop1.Carrera;
import java.util.List;
import
javax.persistence.EntityManager;
import
javax.persistence.EntityManagerFactory;
javax.persistence.Persistenceimport, import
javax.persistence.Query;
public class
Pruebap {public static
void main (String arg []) {v
EntityManagerFactory emf = Persistence.createEntityManagerFactory
(JavaApplication2PU ");
emf.createEntityManager EntityManager em = ();
Carrera Carrera c = new ();
c.setClaveCarrera ("isc");
c.setNombreCarrera ("Systems Engineer");
em.getTransaction().begin();
em.persist(c);
em.getTransaction().commit();
//actualizr
String x="isc";
c=(Carrera)em.find(Carrera.class,x);
c.setNombreCarrera("actualizada");
em.getTransaction().begin();
em.merge(c);
em.getTransaction().commit();
//eliminar
c=(Carrera)em.find(Carrera.class,x);
em.getTransaction().begin();
em.remove (c);
em.getTransaction (). Commit ();
/ / search
c = (Carrera) em.find (Carrera.class, x);
System.out.println ("Password Race" c.getClaveCarrera + ());
/ / show tdo
em.createQuery sql = Query ("select c from c Race");
sql.getResultList List result = ();
for (int i = 0; and lower resultado.size (); i + +) {c =
(Carrera) resultado.get (i);
System. out.println ("Key" + c.getClaveCarrera ());
System.out.println("Clave"+c.getNombreCarrera());
}
}
}
Ya creados estos metodos podemos ver como es que las consultas que realizamos se hacen contra al objeto de la clase Carrera que representa a la tabla carrera dela base de datos en MySql.
Ybueno hasta aqui hemos llegado en la clase que tuvimos le dia de hoy, posteriormente estare pasando los pauntes de la siguiente clase asi como ideas de como darle utilidad a esto con una interfaz (jsp, CRUD con JPA usando SWING ..etc)
Pueden descargar la carpeta del proyecto created in NetBeans 6.1 on the next page:
Remember that there may also download the work of the semester as well as some interesting things out there .....
0 comments:
Post a Comment