<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vive-libre.com &#187; CODIGO</title>
	<atom:link href="http://vive-libre.com/blog/tag/codigo/feed/" rel="self" type="application/rss+xml" />
	<link>http://vive-libre.com/blog</link>
	<description>Un espacio, para la libertad.</description>
	<lastBuildDate>Thu, 24 Dec 2009 18:25:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mini How-to Ubuntu, Java y MySQL Parte 4</title>
		<link>http://vive-libre.com/blog/2008/04/06/mini-how-to-ubuntu-java-y-mysql-parte-4/</link>
		<comments>http://vive-libre.com/blog/2008/04/06/mini-how-to-ubuntu-java-y-mysql-parte-4/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 05:58:12 +0000</pubDate>
		<dc:creator>yovany</dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[LINUX]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[UBUNTU]]></category>
		<category><![CDATA[CODIGO]]></category>

		<guid isPermaLink="false">http://vive-libre.com/blog/2008/04/06/mini-how-to-ubuntu-java-y-mysql-parte-4/</guid>
		<description><![CDATA[Que tal pues seguimos con la saga, en esta parte veremos como hacer consultas desde la interfas grafica, empecemos.
Consultas a MySQL desde interfas grafica con NETBEANS.
Para empesar hay que crear un archivo nuevo,  Formulario GUI JAVA &#62; Formulario JFrame &#62;

Despues le asignamos un nombre que en este caso sera CONSULTAS y le damos terminar.

Ya [...]]]></description>
			<content:encoded><![CDATA[<p>Que tal pues seguimos con la saga, en esta parte veremos como hacer consultas desde la interfas grafica, empecemos.</p>
<p>Consultas a MySQL desde interfas grafica con NETBEANS.</p>
<p>Para empesar hay que crear un archivo nuevo,  Formulario GUI JAVA &gt; Formulario JFrame &gt;</p>
<p><img src="http://lh5.google.com/blue.star.boy/R-xRqwrZ52I/AAAAAAAAA1g/m1rq2JOLwn8/s800/Pantallazo-17.png" /></p>
<p>Despues le asignamos un nombre que en este caso sera CONSULTAS y le damos terminar.</p>
<p><img src="http://lh4.google.com/blue.star.boy/R-xRqgrZ51I/AAAAAAAAA1Y/VbHQtSD6os4/s800/Pantallazo-18.png" /></p>
<p>Ya que tenemos nuestro JFrame vamos a garegar una caja de texto un boton y una tabla que es donde vamos a visualizar los resultados de la consulta, al final obtendremos algo asi.</p>
<p><img src="http://lh6.google.com/blue.star.boy/R-xUBArZ53I/AAAAAAAAA1o/4P1KJKM2fek/s800/Pantallazo-20.png" /></p>
<p>Ahora, recordando la estructura de la base de datos que hemos estado usando, los campos que tenemos disponibles son los siguientes:</p>
<p>CREATE TABLE DATOS<br />
<span style="color: #006600; font-weight: bold">(</span><br />
NOMBRE VARCHAR<span style="color: #006600; font-weight: bold">(</span><span style="color: #800000">25</span><span style="color: #006600; font-weight: bold">)</span>,<br />
TELEFONO VARCHAR<span style="color: #006600; font-weight: bold">(</span><span style="color: #800000">15</span><span style="color: #006600; font-weight: bold">)</span>,<br />
CORREO VARCHAR<span style="color: #006600; font-weight: bold">(</span><span style="color: #800000">25</span><span style="color: #006600; font-weight: bold">)</span>,<br />
TWITTER VARCHAR<span style="color: #006600; font-weight: bold">(</span><span style="color: #800000">30</span><span style="color: #006600; font-weight: bold">)</span><br />
<span style="color: #006600; font-weight: bold">)</span>;</p>
<p>Los resultados que vamos a obtener de la consulta, los vamos a recibir en la tabla, por lo mismo la tabla debe de tener las mismas columnas, estas tablas no son tan dinamicas, como los arreglos en C o demas lenguajes, (o al menos no le he encontrado la forma : ) si saben se aceptan sugerencias ). Para modificar las propiedades de la tabla como columnas, filas, y tipos de datos que albergaran las columnas hay que seleccionar el objeto tabla, y dirigirse a la columna derecha, donde se encuantra la seccion de propiedades, ahi buscaremos el atributo de "MODEL", este atributo no tiene ningun valor, pero tiene un boton al final de campo con tres puntos [...], al dar click en el boton nos mostrara el menu, de la propiedades de la tabla.</p>
<p><img src="http://lh6.google.com/blue.star.boy/R-xazArZ56I/AAAAAAAAA10/FzUbeLa5hTs/s800/Pantallazo-21.png" /></p>
<p>En esta ventana modificaremos el nombre de las columnas que tiene la tabla segun sea el caso, y el tipo de datos que contendra la columna, lo podemos dejar como Object, pero lo cambiaremos a String. En la pestana de Valores predeterminados podremos ver una vista previa de la tabla, ademas de poder agregar columnas y filas con los botones que se encuentran en la parte inferior izquierda. Al termniar obtendremos esto:</p>
<p><img src="http://lh4.google.com/blue.star.boy/R-xccgrZ57I/AAAAAAAAA18/h9z0v_Mx9hQ/s800/Pantallazo-22.png" /></p>
<p>Teniedo el formulario listo, empezaremos con la programacion de la consulta a mysql. Cambiaremos el nombre de la caja de texto para manejarlo mas facil, para nuestro caso se llamara txtNombre.</p>
<p>Agregaremos un evento al boton de tipo mouseReleased para hacer la consulta y ahi debe ir el siguiente codigo:</p>
<div class="igBar"><span id="lcode-2"><a href="#" onclick="javascript:showPlainTxt('code-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-2">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">int j=<span style="color:#800000;color:#800000;">0</span>,i=<span style="color:#800000;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Esta es la cadena a ejecutar en el servido de mysql se le pasa como parametro txtNombre, que es la caja de texto //donde se introduce el nombre a buscar, con el metodo getTex(), para obtener el texto introducido, y toStrin() para //convertir a String la cadena de la caja del texto.</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">String cadena=<span style="color:#CC0000;">"SELECT * FROM DATOS WHERE NOMBRE LIKE '%"</span>+txtNobre.<span style="">getText</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">toString</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>+<span style="color:#CC0000;">"%';"</span>;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//No olvidar que las conexiones siempre se manejan dentro de try, para el manejo de excepciones de lo contrario siempre //les manejara un error aun que el codigo este bien</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">try</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Le indicamos el&nbsp; driver a usar</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Class.<span style="">forName</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"com.mysql.jdbc.Driver"</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">newInstance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Le indicamos los datos correspondientes en la cadena de conexion, ip del servido, Base de datos usuario y pass</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">conexion=DriverManager.<span style="">getConnection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"jdbc:mysql://127.0.0.1:3306/AGENDA"</span>,<span style="color:#CC0000;">"user"</span>,<span style="color:#CC0000;">"passwd"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sentencia=conexion.<span style="">createStatement</span><span style="color:#006600; font-weight:bold;">&#40;</span>ResultSet.<span style="">TYPE_SCROLL_INSENSITIVE</span>,ResultSet.<span style="">CONCUR_READ_ONLY</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Aqui ejecutamos la sentencia de la consulta</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">resultado=sentencia.<span style="">executeQuery</span><span style="color:#006600; font-weight:bold;">&#40;</span>cadena<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Mientras la consulta arroje resultados va entrar al while</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">while<span style="color:#006600; font-weight:bold;">&#40;</span>resultado.<span style="">next</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//El for nos servira para ir recorriendo los datos que recojimos de la consulta, el contador comienza en uno (no en cero //como los areglos en C) el indice i nos servira para ir pasando de columna en columna, tanto en la tabla como en los //resultados de la consulta,el indice j sera para ir saltando de fila en fila despues de cada recorrido de columnas</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">for<span style="color:#006600; font-weight:bold;">&#40;</span>i=<span style="color:#800000;color:#800000;">1</span>;i&amp;lt;=<span style="color:#800000;color:#800000;">4</span>;i++<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Tabla.<span style="">setValueAt</span><span style="color:#006600; font-weight:bold;">&#40;</span>resultado.<span style="">getString</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>,j,i-<span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">j=j+<span style="color:#800000;color:#800000;">1</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Por ultimo el catch para manejar las excepciones si entra al catch mandaremos a imprimir la variable e que es donde se //guardara el mensaje de la excepcion</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">catch<span style="color:#006600; font-weight:bold;">&#40;</span>Exception e<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">JOptionPane.<span style="">showMessageDialog</span><span style="color:#006600; font-weight:bold;">&#40;</span>null,<span style="color:#CC0000;">"error: "</span>+e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#498BFE;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Y aqui esta la prueba (Las etiquetas estan al reves pero nada de importancia <img src='http://vive-libre.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ).</p>
<p><img src="http://lh4.google.com/blue.star.boy/R_lJkQrZ58I/AAAAAAAAA2g/IGdGUGIzpzM/s800/Pantallazo-23.png" /><br />
Y listo espero les sirva de ayuda, comentarios, quejas o sujerencias ya saben a donde <img src='http://vive-libre.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Saludos</p>
]]></content:encoded>
			<wfw:commentRss>http://vive-libre.com/blog/2008/04/06/mini-how-to-ubuntu-java-y-mysql-parte-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
