Anda di halaman 1dari 15

UNIVERSIDAD CAPITN GENERAL GERARDO BARRIOS FACULTAD DE CIENCIA Y TECNOLOGA

GUIA PRCTICA SOBRE MANEJO DE BASES DE DATOS (MYSQL) CON NETBEANS ME, PARA DISPOSITIVOS MOVILES (Aplicado a su Proyecto mvil)

MATERIA: PROGRAMACION III

DOCENTE: LIC.MS. WILLIAM ALEXANDER FLORES CARDONA

FECHA DE ENTREGA: MARTES 13 DE NOVIEMBRE 2012

DIAGRAMA DE LA APLICACIN MOVIL: MIDLET (Estndar) MENU (List) CAPTURA (Form) ALERTA (alert) BUSCAR PRODUCTO (Form) LISTADO (Form)

PANTALLA: MENU (LISTA) Guardar Registros Buscar Productos Listado de Productos Salir

Libreras y variables globales a utilizar en la aplicacin: url (variable que establece la ruta del archivo php donde estn las consultas) peticin= (variable donde se enva el dato de Netbeans, solicitado por php)

PANTALLA: CAPTURA Nombre del Producto (TextField) Descripcin (TextField) Cantidad (TextField) Precio (TextField) Total (StringItem)

Cdigo del Botn OK

// write post-action user code here


url="http://localhost/mitienda/recibir.php"; String nombre_enviar = nombre.getString(); String String String articulo_enviar = articulo.getString(); cantidad_enviar = cantidad.getString(); 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();

PANTALLA: BUSCAR PRODUCTO Nombre del Producto a buscar (TextField) Bsqueda (StringItem)

Cdigo del Botn 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() {

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)

Cdigo del Botn 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();

METODO PARA LA CONEXIN 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();

} finally { if( is!= null ) is.close(); if( os != null ) os.close(); if( con != null ) con.close(); }

CREANDO LA BASE DE DATOS EN MYSQL (UTILIZANDO LA HERRAMIENTA XAMPP) Se ejecuta el XAMPP.

Digitar en el navegador, la siguiente direccin para entrar al entorno de base de datos de Mysql. http://localhost/phpmyadmin/

Crear la base de datos con el nombre de 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'];

$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'];

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;

} //fin del while } //fin del if else { echo "Registros no encontrados en la base de datos..."; } ?>

Anda mungkin juga menyukai