Aplicacion Movil Con Base Datos Mysql Tienda

15
UNIVERSIDAD CAPITÁN GENERAL GERARDO BARRIOS FACULTAD DE CIENCIA Y TECNOLOGÍA GUIA PRÁCTICA SOBRE MANEJO DE BASES DE DATOS (MYSQL) CON NETBEANS ME, PARA DISPOSITIVOS MOVILES (Aplicado a su Proyecto móvil) MATERIA: PROGRAMACION III DOCENTE: LIC.MS. WILLIAM ALEXANDER FLORES CARDONA FECHA DE ENTREGA: MARTES 13 DE NOVIEMBRE 2012

Transcript of Aplicacion Movil Con Base Datos Mysql Tienda

Page 1: Aplicacion Movil Con Base Datos Mysql Tienda

UNIVERSIDAD CAPITÁN GENERAL GERARDO BARRIOS

FACULTAD DE CIENCIA Y TECNOLOGÍA

GUIA PRÁCTICA SOBRE MANEJO DE BASES DE DATOS (MYSQL) CON

NETBEANS ME, PARA DISPOSITIVOS MOVILES

(Aplicado a su Proyecto móvil)

MATERIA: PROGRAMACION III

DOCENTE: LIC.MS. WILLIAM ALEXANDER FLORES CARDONA

FECHA DE ENTREGA: MARTES 13 DE NOVIEMBRE 2012

Page 2: Aplicacion Movil Con Base Datos Mysql Tienda

DIAGRAMA DE LA APLICACIÓN MOVIL:

- MIDLET (Estándar)

- MENU (List)

- CAPTURA (Form)

- ALERTA (alert)

- BUSCAR PRODUCTO (Form)

- LISTADO (Form)

Page 3: Aplicacion Movil Con Base Datos Mysql Tienda

PANTALLA: MENU (LISTA)

- Guardar Registros

- Buscar Productos

- Listado de Productos

- Salir

Librerías y variables globales a utilizar en la aplicación:

- url (variable que establece la ruta del archivo php donde están las consultas)

- petición=”” (variable donde se envía el dato de Netbeans, solicitado por php)

Page 4: Aplicacion Movil Con Base Datos Mysql Tienda

PANTALLA: CAPTURA

- Nombre del Producto (TextField)

- Descripción (TextField)

- Cantidad (TextField)

- Precio (TextField)

- Total (StringItem)

Código del Botón OK

Page 5: Aplicacion Movil Con Base Datos Mysql Tienda

// write post-action user code here

url="http://localhost/mitienda/recibir.php";

String nombre_enviar = nombre.getString();

String articulo_enviar = articulo.getString();

String cantidad_enviar = cantidad.getString();

String precio_enviar = precio.getString();

double totales_enviar;

total.setText((Double.parseDouble(cantidad_enviar)* Double.parseDouble(precio_enviar))+"");

totales_enviar=Double.parseDouble(cantidad_enviar)* Double.parseDouble(precio_enviar);

peticion = url + "?nombre=" + nombre_enviar + "&articulo=" + articulo_enviar + "&cantidad=" +

cantidad_enviar + "&precio=" + precio_enviar + "&totales=" + totales_enviar;

System.out.println(peticion);

Thread hilo = new Thread() {

public void run() {

try {

alert.setString(conexion( peticion ));

}

catch( IOException e ) {

System.out.print(e.getMessage());

}

}

};

hilo.start();

Page 6: Aplicacion Movil Con Base Datos Mysql Tienda

PANTALLA: BUSCAR PRODUCTO

- Nombre del Producto a buscar (TextField)

- Búsqueda (StringItem)

Código del Botón OK

// write post-action user code here

url="http://localhost/mitienda/mostrar.php";

String nombre_enviar = buscar.getString();

peticion = url + "?nombre=" + nombre_enviar;

System.out.println(peticion);

Thread hilo = new Thread() {

Page 7: Aplicacion Movil Con Base Datos Mysql Tienda

public void run() {

try {

resultado.setText(conexion( peticion));

}

catch( IOException e ) {

System.out.print(e.getMessage());

}

}

};

hilo.start();

PANTALLA: LISTADO DE PRODUCTOS

- Listado de Productos (StringItem)

Page 8: Aplicacion Movil Con Base Datos Mysql Tienda

Código del Botón OK

// write post-action user code here

url="http://localhost/mitienda/listado.php";

Thread hilo = new Thread() {

public void run() {

try {

//Etiqueta donde se mostrarn el listado de productos

lista.setText(conexion(url));

}

catch( IOException e ) {

System.out.print(e.getMessage());

}

}

};

hilo.start();

Page 9: Aplicacion Movil Con Base Datos Mysql Tienda

METODO PARA LA CONEXIÓN EN NETBEANS

public String conexion(String url ) throws IOException {

HttpConnection con = null;

InputStream is = null;

OutputStream os = null;

StringBuffer sb = new StringBuffer();

try {

con = (HttpConnection)Connector.open( url );

con.setRequestMethod( HttpConnection.GET);

con.setRequestProperty( "IF-Modified-Since","05 Nov 2002 07:17:19 GMT" );

con.setRequestProperty( "User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");

con.setRequestProperty( "Content-Language","es-ES" );

con.setRequestProperty( "Content-Type","application/x-www-form-urlencoded" );

os = con.openOutputStream();

is=con.openDataInputStream();

//os.write( ("nombre="+nombre).getBytes() );

int ch;

while((ch=is.read())!=-1){

sb.append((char)ch);

}

return sb.toString();

Page 10: Aplicacion Movil Con Base Datos Mysql Tienda

} finally {

if( is!= null ) is.close();

if( os != null ) os.close();

if( con != null ) con.close();

}

}

Page 11: Aplicacion Movil Con Base Datos Mysql Tienda

CREANDO LA BASE DE DATOS EN MYSQL

(UTILIZANDO LA HERRAMIENTA XAMPP)

- Se ejecuta el XAMPP.

- Digitar en el navegador, la siguiente dirección para entrar al entorno de base de datos de

Mysql.

http://localhost/phpmyadmin/

- Crear la base de datos con el nombre de tienda

Page 12: Aplicacion Movil Con Base Datos Mysql Tienda

- Crear una tabla llamada productos, con 5 campos: nombre, articulo, cantidad, precio,

total. Así mismo definir el tipo y la longitud de cada campo.

UTILIZANDO EL PROGRAMA PSPAD, SE CREARAN 3 ARCHIVOS SCRIP DE PHP, QUE SERVIRAN

PARA ESTABLECER LAS CONSULTAS QUE NETBEANS ESPERA RECIBIR

Archivo 1:

recibir.php

<?php

$conexion=mysql_connect("localhost","root","");

mysql_select_db("tienda");

$nombre=$_GET['nombre'];

$articulo=$_GET['articulo'];

$cantidad=$_GET['cantidad'];

$precio=$_GET['precio'];

$totales=$_GET['totales'];

Page 13: Aplicacion Movil Con Base Datos Mysql Tienda

$resultado=mysql_query("INSERT INTO productos

VALUES('".$nombre."','".$articulo."','".$cantidad."','".$precio."','".$totales."')") or die (mysql_error());

if ($resultado) {

echo "Registro Guardado exitosamente...";

}

else

{

echo "Error al guardar el producto...";

}

?>

mostrar.php

<?php

$conexion=mysql_connect("localhost","root","");

mysql_select_db("tienda");

$nombre=$_GET['nombre'];

$resultado=mysql_query("select * from productos where nombre like '%$nombre%'") or die (mysql_error());

if ($resultado && mysql_num_rows($resultado)>0) {

$row=mysql_fetch_array($resultado);

$nombreproducto=$row['nombre'];

$articulo=$row['articulo'];

$cantidad=$row['cantidad'];

$precio=$row['precio'];

$total=$row['total'];

Page 14: Aplicacion Movil Con Base Datos Mysql Tienda

echo "\n Nombre: ".$nombre. "\n Articulo: ".$articulo. "\n Cantidad: ".$cantidad. "\n Precio: ".$precio. "\n

Total: ".$total;

}

else

{

echo "Registro no encontrado...";

}

?>

listado.php

<?php

$conexion=mysql_connect("localhost","root","");

mysql_select_db("tienda");

$resultado=mysql_query("select * from productos") or die (mysql_error());

if ($resultado && mysql_num_rows($resultado)>0) {

while ($row=mysql_fetch_array($resultado)){

$nombreproducto=$row['nombre'];

$articulo=$row['articulo'];

$cantidad=$row['cantidad'];

$precio=$row['precio'];

$total=$row['total'];

echo "\n \n Nombre: ".$nombreproducto. " Articulo: ".$articulo. " Cantidad: ".$cantidad. " Precio: ".$precio.

" Total: ".$total;

Page 15: Aplicacion Movil Con Base Datos Mysql Tienda

} //fin del while

} //fin del if

else

{

echo "Registros no encontrados en la base de datos...";

}

?>