Bienvenidos sean otra vez a este post, lo que hoy se vendrá a explicar sera la manera de como conectar, enlazar, ligar un programa escrito en Java, lógicamente desde el código fuente, a una base de datos MySQL 5.6.X
La versión del MySQL es la versión que viene en la instalación de XAMPP 5.6.11.
Primeramente tendrán que instalar el MySQL, cualquiera de estos videomanuales te pueden servir, uno es para Linux Mint 17.2 Rafaela y otro es para la versión de Windows 10.
Instalación Windows 10
[youtube https://www.youtube.com/watch?v=7oFXfcPP9CE]
Instalación Linux Mint 17.2
[youtube https://www.youtube.com/watch?v=8yuUw0V7jz0]
Una vez instalado el servidor SQL procederemos a levantar la base de datos
[code languaje=”sql”]
— phpMyAdmin SQL Dump
— version 4.4.12
— http://www.phpmyadmin.net
—
— Servidor: localhost
— Tiempo de generación: 12-10-2015 a las 05:06:49
— Versión del servidor: 5.6.25
— Versión de PHP: 5.6.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
—
— Base de datos: `INVEN`
—
CREATE DATABASE IF NOT EXISTS `INVEN` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `INVEN`;
DELIMITER $$
—
— Procedimientos
—
DROP PROCEDURE IF EXISTS `sp_InsertaCliente`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_InsertaCliente`(in Nombres VARCHAR(200)
, Apellidos VARCHAR(200),Direccion varchar(200),Ciudad varchar(200)
,Telefono varchar(200),RFC VARCHAR(10),FechaNacimiento VARCHAR(200),Estado VARCHAR(200)
,Municipio varchar(200),CodigoPostal VARCHAR(200))
BEGIN
INSERT INTO `Clientes`
(
`Nombres`,
`Apellidos`,
`Direccion`,
`Ciudad`,
`Telefono`,
`RFC`,
`FechaNacimiento`,
`Estado`,
`Municipio`,
`CodigoPostal`
)
VALUES
(
Nombres,
Apellidos,
Direccion,
ciudad,
Telefono,
RFC,
FechaNacimiento,
Estado,
Municipio,
CodigoPostal
);
END$$
DELIMITER ;
— ——————————————————–
—
— Estructura de tabla para la tabla `Clientes`
—
DROP TABLE IF EXISTS `Clientes`;
CREATE TABLE IF NOT EXISTS `Clientes` (
`idCliente` bigint(20) NOT NULL,
`Nombres` varchar(100) DEFAULT NULL,
`Apellidos` varchar(45) DEFAULT NULL,
`Direccion` varchar(45) DEFAULT NULL,
`Ciudad` varchar(45) DEFAULT NULL,
`Telefono` varchar(45) DEFAULT NULL,
`RFC` varchar(45) DEFAULT NULL,
`FechaNacimiento` datetime(6) DEFAULT NULL,
`Estado` varchar(45) DEFAULT NULL,
`Municipio` varchar(45) DEFAULT NULL,
`CodigoPostal` varchar(45) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=latin1;
—
— Volcado de datos para la tabla `Clientes`
—
[/code]
Ya levantada la base de datos tendremos que agregar la siguiente librería para conectar MySQL mysql-connector-java-5.1.6-bin.jar, la agregamos de igual modo que hemos estado agregando librerías en los post anteriores.
Bien ya agregada la librería creamos la siguiente clase
[code languaje=”Java”]
/*
Clase conexión, recibe la información de la conexión y se conecta
*/
package herramientas;
import java.sql.*;
import javax.swing.JOptionPane;
public class conexion {
public static String strServidor= new String();
public static String strBaseDeDatos=new String();
public static String strUsuarioSQL=new String();
public static String strPSWSQL=new String();
Connection conexion= null;
public Connection conectado(){
try{
Class.forName("com.mysql.jdbc.Driver");
conexion = DriverManager.getConnection("jdbc:mysql://"+ this.strServidor +"/" +this.strBaseDeDatos + "?user="+this.strUsuarioSQL + "&password="+ this.strPSWSQL);
Statement stm = conexion.createStatement();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, "ERROR DE CONEXIÓN"+e);
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null, e);
}
return conexion;
}
public void desconectar(){
conexion = null;
System.out.println("conexion terminada");
}
}
[/code]
Luego al usar la clase conexión, lo haríamos de la siguiente forma
[code languaje=”Java”]
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package interfaces;
import clases.classUsuarios;
//AQUI ES DONDE TENEMOS LA CLASE CONEXION
import herramientas.conexion;
import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.jar.Attributes;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.*;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author julio
*/
public class frmLogueo extends javax.swing.JFrame {
/**
* Creates new form frmLogueo
*/
public frmLogueo() {
initComponents();
leerConfiguracion();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
lblServidor = new javax.swing.JLabel();
txtServer = new javax.swing.JTextField();
lblUsuario = new javax.swing.JLabel();
txtUsuario = new javax.swing.JTextField();
lblContraseña = new javax.swing.JLabel();
cmdAccesar = new javax.swing.JButton();
pswContraseña = new javax.swing.JPasswordField();
lblBase = new javax.swing.JLabel();
txtBase = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setLocationByPlatform(true);
setResizable(false);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/padlock.jpg"))); // NOI18N
lblServidor.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
lblServidor.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblServidor.setText("SERVIDOR");
lblServidor.setToolTipText("");
lblUsuario.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
lblUsuario.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblUsuario.setText("USUARIO");
lblContraseña.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
lblContraseña.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblContraseña.setText("CONTRASEÑA");
cmdAccesar.setText("ACCESO");
cmdAccesar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdAccesarActionPerformed(evt);
}
});
lblBase.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
lblBase.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblBase.setText("BASE DE DATOS");
lblBase.setToolTipText("");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(129, 129, 129)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(lblServidor, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addComponent(lblUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addComponent(pswContraseña)
.addComponent(txtServer, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addComponent(lblContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblBase, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addComponent(txtBase)))
.addGroup(layout.createSequentialGroup()
.addGap(114, 114, 114)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(123, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(140, 140, 140)
.addComponent(cmdAccesar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(156, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addGap(30, 30, 30)
.addComponent(lblServidor)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblBase)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(lblUsuario)
.addGap(12, 12, 12)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblContraseña)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(pswContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(93, 93, 93))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(459, Short.MAX_VALUE)
.addComponent(cmdAccesar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void cmdAccesarActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
// File archivoConexion = new File("Conexion.xml");
//
// if (archivoConexion.exists())
// System.out.println("El fichero");
// else
crearXML2();
//AQUI CREAMOS LA INSTANCIA A LA CLASE
conexion con = new conexion();
//AQUI LLENAMOS CON LOS DATOS NECESARIOS DE CONEXIÓN
con.strServidor=this.txtServer.getText();
con.strPSWSQL="";
con.strUsuarioSQL="root";
con.strBaseDeDatos=this.txtBase.getText();
//AQUI REALIZAMOS LA CONEXIÓN
con.conectado();
leerConfiguracion();
frmPrincipal principal= new frmPrincipal();
classUsuarios usuarios = new classUsuarios();
//ESTO YA ES OTRA HISTORIA, AQUI VERIFICA SI EL USUARIO Y CONTRASEÑA COINCIDEN
if (usuarios.blnChecaUsuarioContra("SELECT * FROM Usuarios where Usuario=’"+this.txtUsuario.getText() +"’ and Contra=’"+this.pswContraseña.getText()+"’")==true){
this.setVisible(false);
principal.setVisible(true);
}
else
JOptionPane.showMessageDialog(null, "USUARIO O CONTRASEÑA INCORRECTO"+this.pswContraseña.getText());
System.out.println("SELECT * FROM Usuarios where Usuario=’"+this.txtUsuario.getText() +"’ and Contra=’"+this.pswContraseña.getText()+"’");
} catch (ParserConfigurationException ex) {
Logger.getLogger(frmLogueo.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(frmLogueo.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
// public static void main(String args[]) {
// /* Set the Nimbus look and feel */
// //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
// /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
// * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
// */
// try {
// for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
// if ("Nimbus".equals(info.getName())) {
// javax.swing.UIManager.setLookAndFeel(info.getClassName());
// break;
// }
// }
// } catch (ClassNotFoundException ex) {
// java.util.logging.Logger.getLogger(frmLogueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (InstantiationException ex) {
// java.util.logging.Logger.getLogger(frmLogueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (IllegalAccessException ex) {
// java.util.logging.Logger.getLogger(frmLogueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (javax.swing.UnsupportedLookAndFeelException ex) {
// java.util.logging.Logger.getLogger(frmLogueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// }
// //</editor-fold>
//
// /* Create and display the form */
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// new frmLogueo().setVisible(true);
// }
// });
// }
// Variables declaration – do not modify
private javax.swing.JButton cmdAccesar;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel lblBase;
private javax.swing.JLabel lblContraseña;
private javax.swing.JLabel lblServidor;
private javax.swing.JLabel lblUsuario;
private javax.swing.JPasswordField pswContraseña;
private javax.swing.JTextField txtBase;
private javax.swing.JTextField txtServer;
private javax.swing.JTextField txtUsuario;
// End of variables declaration
public void crearXML2() throws ParserConfigurationException, TransformerConfigurationException, TransformerException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation implementation = builder.getDOMImplementation();
Document document = implementation.createDocument(null, "Configuracion", null);
document.setXmlVersion("1.0");
Element raiz = document.getDocumentElement();
Element nodoServidor = document.createElement("Servidor"); //creamos un nuevo elemento
Text nodoValorServidor = document.createTextNode(this.txtServer.getText()); //Ingresamos la info
nodoServidor.appendChild(nodoValorServidor);
raiz.appendChild(nodoServidor); //pegamos el elemento a la raiz "Documento"
Element nododb = document.createElement("DB"); //creamos un nuevo elemento
Text nodoValorDB = document.createTextNode(this.txtBase.getText()); //Ingresamos la info
nododb.appendChild(nodoValorDB);
raiz.appendChild(nododb); //pegamos el elemento a la raiz "Documento"
Element nodoUsuario = document.createElement("Usuario"); //creamos un nuevo elemento
Text nodoValorUsuario = document.createTextNode(this.txtUsuario.getText()); //Ingresamos la info
nodoUsuario.appendChild(nodoValorUsuario);
raiz.appendChild(nodoUsuario); //pegamos el elemento a la raiz "Documento"
Source source = new DOMSource(document);
StreamResult result = new StreamResult(new java.io.File("Conexion.xml"));//nombre del archivo
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
}
public void leerConfiguracion(){
try{
File fXmlFile = new File("Conexion.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this – http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Configuracion");
System.out.println("—————————-");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
this.txtServer.setText( eElement.getElementsByTagName("Servidor").item(0).getTextContent());
this.txtBase.setText(eElement.getElementsByTagName("DB").item(0).getTextContent());
this.txtUsuario.setText(eElement.getElementsByTagName("Usuario").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
[/code]
Por si llegaran a necesitar la clase usuarios
[code languaje=”Java”]
/*
CREADO POR JULIO
*/
package clases;
import java.sql.SQLException;
import herramientas.conexion.*;
import com.sun.crypto.provider.RSACipher;
import herramientas.conexion;
import java.awt.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author julio
*/
public class classUsuarios {
private conexion con;
PreparedStatement ps;
ResultSet res;
public classUsuarios() {
con = new conexion();
}
public boolean blnChecaUsuarioContra( String sql){
boolean data =false;
String idUsuarios="IdUsuarios";
try{
ps= con.conectado().prepareStatement(sql);
res = ps.executeQuery();
while(res.next()){
//System.out.println(res.getNString(idUsuarios));
data=true;
System.out.println(res.getWarnings());
}
res.close();
}catch(SQLException e){
System.out.println(e);
}
return data;
}
}
[/code]