JDBC: Conexiones con bases de datos desde Java



Documentos relacionados
JDBC: Conexiones con bases de datos desde Java

JDBC: Conexiones con bases de datos desde Java JDBC. Luis Fernando Llana Díaz. 27 de abril de 2010

Acceso a bases de datos desde Java: JDBC

JDBC. Curso 04/05. Tema 8 JDBC. Departament d Informàtica. Universitat de València. 1. Introducción Arquitecturas típicas...

Acceso a bases de datos desde Java: JDBC

Programación Orientada a Objetos II. La Plataforma JDBC

Desarrollo de Software con

Acceso a BDs en Java: JDBC

MySQL con Java en MS Windows

Práctica 2 ACCESO A BASES DE DATOS CON JDBC. 1. Creación de una base de datos en MySQL...1

JDBC. Una mini-introducci. introducción

JDBC Julio Introducción JDBC Arquitecturas típicas con JDBC... 3

Acceso a BD desde Java. JDBC

JDBC. Aplicaciones Distribuidas

Java RMI. las RPC de Java. Parte I. Luis Fernando Llana Díaz. Departamento de Sistemas Informáticos y ProgramaciónUniversidad Complutense de Madrid

JDBC: Java DataBase Conectivity

Guía práctica de introducción básica a JDBC

Bases de Datos usando Java : JDBC. Ing. César Julio Bustacara Medina

Java y MySQL. Ciclo de ejecución

NORMAS PARA LA ENTREGA DE LOS LABORATORIOS

INTRODUCCIÓN. El objetivo de este manual es la identificación de los procesos técnicos más

10. JDBC. 10. JDBC Introducción. java.sql

Prácticas Ingeniería del Software 3º

JDBC. Francisco Javier Solans Benedí Software Developper

UNIVERSIDAD DON BOSCO FACULTAD DE ESTUDIOS TECNOLÓGICOS

Bases de Datos Introducción a la Conectividad de

Practica 11: Conexión de Java con Bases de datos Access

Bases de datos y JDBC

Universidad Don Bosco. Materia: Programación Orientada a Objetos Contenido: Modificadores de Acceso y JDBC

USANDO SERVLETS EN UN SERVIDOR WEB RESIN

Centro Asociado Palma de Mallorca. Antonio Rivero Cuesta

Práctica sobre compartición de instancias remotas.

Qué es JDBC? Cuatro tipos de drivers JDBC Uso de JDBC

Base de Datos JDBC. Unidad: 1 Laboratorio de Programación. Universidad Nacional de la Patagonia Austral Unidad Académica Río Gallegos

Módulo 15. Introducción a JDBC, Java Database Connectivity. Programación Orientada a Objetos Página 16.0 de 25

PRÁCTICA Nº 16 CONEXIÓN A UNA BASE DE DATOS, INGRESO Y VISUALIZACIÓN DE REGISTROS

Diseño de la capa de datos. Acceso a datos con JDBC

Establecer una conexión con una base de datos o acceder a cualquier fuente de datos tabular Enviar enunciados SQL. Procesar los resultados

2.1 La interfaz de JDBC para el programador de aplicaciones La interfaz JDBC para los controladores JDBC... 5

CORBA desde Java. Diego Sevilla Ruiz Sistemas Distribuidos. 1. Introducción

Programación Orientada a Objetos Analista Programador Universitario Plan 2008 Año 2010

Acceso a bases de datos con JDBC

Conexión de Mysql con NetBeans

PROGRAMACIÓN CON ACCESO A DATOS

Creación de proyectos usando Java/MySQL

JDBC: EL PUENTE ENTRE JAVA Y LAS BASES DE DATOS. En este articulo se expone la API JDBC (Java DataBase Connetivity), la cual permite el

Java Servlets. Luis Fernando Llana Díaz. 17 de abril de Departamento de Sistemas Informáticos y ProgramaciónUniversidad Complutense de Madrid

Laboratorios de BASES DE DATOS. (I.T. Informática)

Arquitectura J2EE para aplicaciones web. Aplicaciones web con JSP. Arquitectura J2EE: Capa de Acceso Web. Arquitectura J2EE: Capa Cliente

Formatos para prácticas de laboratorio

MultiBase. Notas a la versión 3.4. BASE 100, S.A.

Excepciones y E/S Java y Servicios Web I Master en Ingeniería Matemática

LENGUAJE ESTRUCTURADO DE CONSULTAS SQL (2)

Aplicaciones Web (Curso 2014/2015)

Utilización de Oracle Java Procedures

Computación Web (Curso 2015/2016)

Ingeniería del Software II. Curso 2003/2004. Enunciado de la práctica del Segundo parcial.

Tutorial de incorporación del driver JDBC a Eclipse. Programación en Internet

Apéndice C. Clases del sistema.

APUNTES: APLICACIONES WEB BASADAS EN PHP Y MYSQL Página 1 de 5

/) 0! () 5!2 /)( 1! "!"# 2 ()/ $% $!" !! " #!!!$!!% &!!' ' ' "! ()()/ & $!" ()()(' (!(

Número de Práctica: 1 Nombre de la Práctica: Comunicación Cliente Servidor Vía Sockets Java con Sistema Manejador de Bases de Datos

APÉNDICE A: ACCESO A UNA BASE DE DATOS CON JDBC

JAVA 5. Conexión con Bases de Datos

Federico Peinado

%& %)& '$!%*+ $, %%%&$ %%

Base de Datos JDBC. Unidad: 1 Laboratorio de Programación. Universidad Nacional de la Patagonia Austral Unidad Académica Río Gallegos

MSSQL Server Java

Práctica 3 Manejo de Base de Datos(Java-MySQL) desde consola

TEMA 1 ACCESO A BASE DE DATOS 1. QUE ES UN JDBC ODBC?

Tópicos Avanzados de Programación (TAP3501)

Java con Bases de Datos

Tutorial de Unity 3D Tema 52 Bases de Datos. Tutorial de Unity 3D. Bases de Datos

Programación Concurrente en Java

2.1 Tutorial de JDBC

Ficheros de acceso aleatorio

Curso de MySQL y Java

El primer paso a realizar es crear la referencia JNDI para el origen de datos en el servidor local TomCat.

Taller de Programación II J2EE

Luego lleve el cliente a otra máquina y ejecute desde ahí usando el nombre de la máquina del servidor.

Entrada y Salida con Java

James Gosling, creador de Java

Formato para prácticas de laboratorio

Acceso a bases de datos MySQL con PHP

- Compilar y ejecutar programas en Java - Estructura básica de una clase - El comando javac - El comando java - Introducción al IDE de desarrollo

Base de Datos Oracle: desarrollo de aplicaciones

Practica 7 Conexión a su base de datos. Optativa II

Manual de JSP con MySQL

Programación y Acceso a BD con SQL

Java y Access. 4. En nuestra pantalla aparecerá ahora la pestaña DSN usuario seleccionada. Para crear un nuevo perfil haremos click en Agregar...

BASE DE DATOS QUÉ ES UNA BASE DE DATOS?

BASE DE DATOS. Luis Emilio Cabrera Crot Mayo 2015

API de java. ( Guía de alumno Laboratorio 9. Recursos disponibles en moodle para este día.

10. Servlets con acceso a bases de datos

Conexión SQL Server y C# (Consola)

JDBC. Cada celda de la tabla debe tener solamente un valor (nunca un conjunto de valores). (1NF)

Java Servlets. Qué es un programa? Luis Fernando Llana Díaz. 22 de abril de input Programa output. En pascal:

Introducción a Java LSUB. 15 de enero de 2015 GSYC

JSP. MSc. Daniel Alejandro Yucra Sotomayor Pag Web Services. Laboratorio Nro. 11. Web Services con JAX-WS con MySQL. I. Competencia General:

Transcripción:

JDBC: Conexiones con bases de datos desde Java Luis Fernando Llana Díaz Departamento de Sistemas Informáticos y Computación Universidad Complutense de Madrid 0 de mayo de 007

JDBC Conexión con una fuentes de datos. Realizar peticiones y actualizaciones. Manejar los resultados de las consultas Aplicacion Java JDBC DBMS MySQL DBMS Oracle DBMS PostgresSQL

Drivers JDBC Es necesario un driver dependiente del proveedor de la Base de datos. MySQL MySQL java connector

Creando la base de datos Fichero crea.sql create database valores ; use valores ; create table valores ( id int unsigned auto_increment primary key, nombre varchar (), ibex_id varchar (00), url varchar (), tipo enum (" acciones "," fondos ") ); create table datos ( id int unsigned auto_increment primary key, empresa int unsigned, fecha date, valor double ); grant all on valores.* to luis identified by ' patata '; 7 8 9 0 7 8 9 ~/ sql$ mysql -u root < crea.sql

Interfaz java.sql.connection Establece conexión con la base de datos. No tiene constructor. Es un interfaz (está implementado en el driver). Constuye instrucciones SQL precompiladas (método preparestatement).

Realizando conexión con la base de datos package valores ; import java.sql. Connection ; import java.sql. DriverManager ; import java.sql. SQLException ; public class Conexion { protected static Connection getmysqlconnection ( String url, String db, String user, String passwd ) throws ClassNotFoundException, InstantiationException, SQLException, IllegalAccessException { Class. forname ("com. mysql.jdbc. Driver " ). newinstance (); return DriverManager. getconnection ("jdbc : mysql :// "+url+"/"+db+ "?user ="+ user +"& password ="+ passwd ); protected static Connection getmysqlconnection () throws ClassNotFoundException, InstantiationException, SQLException, IllegalAccessException { return getmysqlconnection (" localhost ", " valores ", "luis ", " patata " ); protected static String getfich ( String fich ) { java.net.url url = ClassLoader. getsystemresource (fich ); return url. getpath (); 7 8 9 0 7 8 9 0 7 8

Ficheros de datos Fichero de valores: valores.csv BSCH BSCH acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_.htm Endesa ENDESA acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_.htm Repsol - YPF REPSOL YPF acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_ Acciona ACCIONA acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_.htm Inditex INDITEX acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_.htm Teléfonica TELEFONICA acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_ Banco Popular BA. POPULAR acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind 7 Ebro - Puleva EBRO PULEVA acciones http :// www. bolsamadrid.es /esp / mercados / acciones / igbm 8 Mittal - Steel ARCELOR MIT. acciones http :// www. bolsamadrid.es /esp / mercados / acciones / 9igbm_ Indra INDRA A acciones http :// www. bolsamadrid.es /esp / mercados / acciones / igbm.htm0 EADS EADS acciones http :// www. bolsamadrid.es /esp / mercados / acciones / igbm.htm Unipapel UNIPAPEL acciones http :// www. bolsamadrid.es /esp / mercados / acciones / igbm.htm Madrid Bolsa MADRID BOLSA fondos http :// www. bolsamadrid.es /esp / mercados / fondos / htmfondos Plus Madrid PLUSMADRID fondos http :// www. bolsamadrid.es /esp / mercados / fondos / htmfondos /00 FonCaixa FONCAIXA BOLSA INDICE ESPA &#; A fondos http :// www. bolsamadrid.es /esp / me

Cargar datos package valores ; import java.io. BufferedReader ; import java.io. FileReader ; import java.io. IOException ; import java.sql. Connection ; import java.sql. PreparedStatement ; import java.sql. SQLException ; 7 import java.util. Scanner ; 8 public class LeeValores { 9 private static void insertadatos ( Connection con, BufferedReader in ) 0 throws IOException, SQLException {... public static void main ( String [] args ) throws Exception { Connection con = null ; BufferedReader in = null ; try { 7 con = Conexion. getmysqlconnection (); 8 in = new BufferedReader (new FileReader ( Conexion. getfich ("sql / valores.csv 9" ))) insertadatos (con, in ); 0 finally { if (con!= null ) con. close (); if (in!= null ) in. close ();

¾Dónde está el chero sql/valores.csv?. -- -- sql -- borra. sql -- crea. sql -- datos. csv -- usuarios. csv `-- valores. csv -- src -- valores -- CVS -- Entries -- Repository `-- Root -- Conexion. java -- ConsigueDatos. java -- ConsigueDatos. java.~..~ -- LeeDatos. java `-- LeeValores. java -- classes `-- valores -- Conexion. class -- ConsigueDatos. class -- LeeDatos. class `-- LeeEmpresas. class 7 8 9 0 7 8 9 0

Accediendo sql/valores.csv Los objetos de clase ClassLoader permitend acceder a los recursos Java. java.net.url url = ClassLoader. getsystemresource (fich ); fich debe estar dentro del CLASSPATH en la ejecución. ~/ Java$ export CLASSPATH =.:./ classes ~/ Java$ $JAVA_HOME /bin /java valores. LeeValores

Cargar datos private static final String sqlins = " insert into valores (nombre,ibex_id,url,tipo ) "+ " values (?,?,?,?) "; private final static int NOMBRE =; private final static int IBEX_ID =; private final static int URL_BOLSA =; private final static int TIPO =; private static void insertadatos ( Connection con, BufferedReader in ) throws IOException, SQLException { String linea = in. readline (); PreparedStatement pstmt = con. preparestatement ( sqlins ); while ( linea!= null ) { Scanner scan = new Scanner ( linea ); scan. usedelimiter (" " ); String nombre = scan. next (); String ibex_id = scan. next (); String tipo = scan. next (); String url = scan. next (); System.out. println ( nombre +":"+ ibex_id +":"+ tipo +":"+ url ); pstmt. setstring (NOMBRE, nombre ); pstmt. setstring (IBEX_ID, ibex_id ); pstmt. setstring (TIPO,tipo ); pstmt. setstring (URL_BOLSA,url ); int n = pstmt. executeupdate (); linea = in. readline (); 7 8 9 0 7 8 9 0 7 8

Ficheros de datos Fichero de valores: valores.csv Fecha BSCH ENDESA REPSOL - YPF ACCIONA INDITEX Telefónica Banco Popular Ebro - Puleva Mittal 8/0/0 9. 7.0.. 7..9 0/0/0 9. 7.0.. 7..9 7/08/0 8..98 0. 9.8.8 7.0. 0/08/0 7.99.9 7.0 0.0 9.08.78.89 7.. //0.0.7 7.7 7.9 9.9.0.7 8.. 8..08. 87. 8 9//0.98..9 8. 0.78..7 9.7. 8..0. 9.7 8 0//0.08..9 9.9 0.9.8.8 9.8.8 8... 0.7 8 //0.0.. 0.9 0... 9..8 8..79..9 8. 7//0..7. 0.0..9 9.9.9 9.0.07... 8//0....0 0.89..8 9.. 8.7.00.0.0 9//0..8.. 0.8..7 9. 8..00.0.. 8 0/0/07..8.. 0.8..7 9. 8..00.0.8. 0/0/07.9.7..0 0.79.9.97 9..87 8.88.9.8 7.8

Cargar datos private static void insertadatos ( Connection con, BufferedReader in ) throws IOException, SQLException, ParseException { int [] id_valores = valores ( con ); String linea = in. readline (); linea = in. readline (); String sql = " insert into datos (fecha,valor, precio ) values (?,?,? )"; PreparedStatement pstmt = con. preparestatement (sql ); 7 while ( linea!= null ) { 8 Scanner scan = new Scanner ( linea ); 9 scan. usedelimiter (" " ); 0 String strfecha = scan. next (); if (! strfecha. equals ("" )) { DateFormat df = new SimpleDateFormat (" dd / MM / yy " ); Date fecha = df. parse ( strfecha ); System.out. println (df. format ( fecha )); for (int i = 0; i < id_valores. length ; i ++) { String strvalor = scan. next (); 7 if (! strvalor. equals ("" )) { 8 double precio = Double. parsedouble ( strvalor ); 9 pstmt. setdate (, new java.sql.date ( fecha. gettime ())); 0 pstmt. setint (, id_valores [i ]); pstmt. setdouble (, precio ); int n = pstmt. executeupdate (); 7 linea = in. readline (); 8 9 0

Cargar datos private static String [] valores = { " BSCH "," ENDESA "," REPSOL YPF ", " ACCIONA "," INDITEX "," TELEFONICA ", "BA. POPULAR ","EBRO PULEVA "," ARCELOR MIT.", " INDRA A"," EADS "," UNIPAPEL "," MADRID BOLSA ", " PLUSMADRID "," FONCAIXA BOLSA INDICE ESPA &#; A" ; private static int [] valores ( Connection con ) throws SQLException { int [] ids = new int [valores. length ]; String sql = " select id from valores where ibex_id like?"; PreparedStatement pstmt = con. preparestatement (sql ); for (int i = 0; i < ids. length ; i ++) { pstmt. setstring (, valores [i ]); ResultSet rs = pstmt. executequery (); rs. next (); ids [i ]=rs. getint (); return ids ; 7 8 9 0 7 8 9

Realizar actualizaciones Instrucciones: INSERT, UPDATE, DELETE public static void pr ( Connection con ) throws SQLException { String sql = " update datos set precio = precio + "; PreparedStatement pstmt = con. preparestatement (sql ); int n = pstmt. executeupdate (); System.out. println (n+" filas actualizadas " ); SQLWarning warning = pstmt. getwarnings (); if ( warning!= null ) { System. out. println (" AVISOS... " ); while ( warning!= null ) { System.out. println (" Message : " + warning. getmessage ()); warning = warning. getnextwarning (); else { System.out. println ("No hay avisos " ); 7 8 9 0

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores ResultSet rs = pstmt.executequery(); rs id nombre tipo

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores rs.next(); id nombre tipo rs BSCH acciones

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores rs.next(); id nombre tipo BSCH acciones rs Endesa acciones

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores rs.next(); id nombre tipo BSCH acciones Endesa acciones rs Repsol-YPF acciones

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores rs.next(); id nombre tipo BSCH acciones Endesa acciones Repsol-YPF acciones rs Acciona acciones

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores rs.next(); id nombre tipo BSCH acciones Endesa acciones Repsol-YPF acciones Acciona acciones rs Inditex acciones

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores rs.next(); id nombre tipo BSCH acciones Endesa acciones Repsol-YPF acciones Acciona acciones Inditex acciones rs Telefónica acciones

Realizar Consultas Instrucciones Select select id, nombre, tipo from valores id nombre tipo BSCH acciones Endesa acciones Repsol-YPF acciones Acciona acciones Inditex acciones Telefónica acciones

Ejemplos de consultas I public static void pr ( Connection con ) throws SQLException { String sql = " select * from valores "; PreparedStatement pstmt = con. preparestatement (sql ); ResultSet rs = pstmt. executequery (); while ( rs. next ()) { int id = rs. getint ("id " ); String nombre = rs. getstring (" nombre " ); System.out. println (id +":"+ nombre ); 7 8 9 0

Ejemplos de consultas II public static void pr ( Connection con ) throws SQLException { String sql = " select valores.nombre, datos.fecha, datos. precio "+ " from datos left join valores on valores.id = datos. valor "+ " where datos. fecha >= '007-0 -0 ' "+ " order by valores.tipo, valores.nombre, datos. fecha "; PreparedStatement pstmt = con. preparestatement (sql ); ResultSet rs = pstmt. executequery (); 7 while ( rs. next ()) { 8 String nombre = rs. getstring (" valores. nombre " ); 9 Date fecha = rs. getdate (" datos. fecha " ); 0 double precio = rs. getdouble (" datos. precio " ); DateFormat df = new SimpleDateFormat (" dd 'de ' MMMM 'de ' yyy " ); NumberFormat nf = new DecimalFormat (" #,000.00 'e'" ); System.out. println (df. format ( fecha )+ "\t"+ nombre +"\t"+ nf. format ( precio ));

Ejemplos de consultas III public static int getid ( String ibex_id, Connection con ) throws SQLException { String sql = " select id from valores where ibex_id like '"+ibex_id +" '"; PreparedStatement pstmt = con. preparestatement (sql ); ResultSet rs = pstmt. executequery (); if ( rs. next ()) { return rs. getint (); else { return -; 7 8 9 0

Consiguiendo datos desde www.bolsamadrid.es Conseguir el chero HTML. Analizar el chero. Añadir datos en la base de datos.

Conseguir el chero HTML Fichero de valores: valores.csv BSCH BSCH acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_.htm Endesa ENDESA acciones http :// www. bolsamadrid.es /esp / mercados / acciones / accind_.htm private static String cargapagina ( java. net. URL url ) throws IOException { InputStream in = url. openstream (); StringBuffer sb = new StringBuffer (); int c = in. read (); while (c!= -) { sb. append (( char )c ); c = in. read (); String res = sb. tostring (); return res ; 7 8 9 0

Analizar el chero HTML Fichero de valores: http://www.bolsamadrid.es/esp/mercados/acciones/accind_.ht...... <IMG SRC="/ images /arr -up9.gif " BORDER=0> BSCH</A></TD><TD>,0</TD>......... <IMG SRC="/ images /arr -dw9.gif " BORDER=0> ENDESA</A></TD><TD>0,9</TD>... private static double getvalor ( String ibex_id, String datos, String tipo ) throws ParseException { String regexp = null ; if (tipo. equals (" acciones " )) { regexp=ibex_id+" *</A></TD><TD >([0-9,]+) </TD>"; else if (tipo. equals (" fondos " )) { regexp=ibex_id+" *</a></td >.*? <TD *[^>]* >([0-9,]+) </TD>"; 7 else { 8 throw new RuntimeException (" Tipo \'"+ tipo +"\' desconocido " ); 9 0 Pattern p = Pattern. compile ( regexp ); Matcher m = p. matcher ( datos ); if (m. find ()) { // Double. parsedouble no vale, no analiza,0 NumberFormat nf = NumberFormat. getinstance (); return nf. parse (m. group ()). doublevalue (); else { 7 throw new RuntimeException (" Acciones \'"+ ibex_id +"\' no encontrada " ); 8 9 0

Insertar los datos en la Base de datos private static final String sqlins = " insert into datos ( fecha, valor, precio ) values (?,?,?) "; private static final int FECHA =; private static final int VALOR =; private static final int PRECIO =; private static void insertadatos (java.sql.date fecha, Connection con ) throws SQLException, IOException, ParseException { HashMap <String, String > paginas = new HashMap <String, String >(); String sql = " select * from valores "; PreparedStatement pstmtvalores = con. preparestatement (sql ); ResultSet rs = pstmtvalores. executequery (); PreparedStatement pstmtins = con. preparestatement ( sqlins ); while ( rs. next ()) { insertadatos ( pstmtins, fecha, rs, paginas ); 7 8 9 0 7 8 9

Insertar los datos en la Base de datos private static void insertadatos ( PreparedStatement pstmtins, java. sql. Date fecha, ResultSet rs, HashMap <String, String > paginas ) throws SQLException, IOException, ParseException { String url = rs. getstring ("url " ); String datos = paginas. get ( url ); if ( datos == null ) { datos = cargapagina ( paginas, url )); paginas.put (url, datos ); String tipo = rs. getstring ("tipo " ); String ibex_id = rs. getstring (" ibex_id " ); double valor = getvalor ( ibex_id, datos, tipo ); int id = rs. getint ("id " ); String nombre = rs. getstring (" nombre " ); pstmtins. setdate (FECHA, fecha ); pstmtins. setint (VALOR,id ); pstmtins. setdouble (PRECIO, valor ); int n = pstmtins. executeupdate (); 7 8 9 0 7 8 9 0

Consiguiendo datos automáticamente #!/ bin / sh # $Id : transparencias. tex,v.8 007-0 -0 :7: luis Exp $ JAVA_HOME =/ opt /java /jdk. CLASSPATH =/ home /casa / ahorros / classes $JAVA_HOME /bin /java -cp $CLASSPATH valores. ConsigueDatos $* $ crontab -l SHELL =/ bin /bash MAILTO = luis # Directorios donde buscar programas PATH =/ bin :/ usr /bin :/ usr / XR /bin :/ usr / local /bin :/ home /luis /bin * *,,,, /home /casa / ahorros / scripts / consiguedatos.sh > /dev /null 7 >& 8