/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CapaLogica;
/**
*
* @author User
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
/**
*
* @author User
*/
public class CEntidad extends CConexion {
// <editor-fold defaultstate="collapsed" desc="Atributos">
String NombreTabla;
String[] Columnas_Nombres;
String[] Columnas_Valores;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Propiedades">
public String[] getColumnas_Nombres() {
return Columnas_Nombres;
}
public void setColumnas_Nombres(String[] Columnas_Nombres) {
this.Columnas_Nombres = Columnas_Nombres;
}
public String[] getColumnas_Valores() {
return Columnas_Valores;
}
public void setColumnas_Valores(String[] Columnas_Valores) {
this.Columnas_Valores = Columnas_Valores;
}
public String getNombreTabla() {
return NombreTabla;
}
public void setNombreTabla(String NombreTabla) {
this.NombreTabla = NombreTabla;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Metodos">
public CEntidad(String Ruta, String NombreBD, String User, String Password, String NombreTabla) {
super(Ruta, NombreBD, User, Password);
try {
this.NombreTabla = NombreTabla;
if (NombreBD != null) {
AbrirConexion();
//Columnas_Nombres = this.GenerarColumnas_Nombres();
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void Columnas_Nombres() throws SQLException {
Columnas_Nombres = this.GenerarColumnas_Nombres();
}
public ResultSet SeleccionSimple() throws SQLException {
return this.Seleccionar("select * from " + this.NombreTabla);
}
public DefaultTableModel Insert(String Valores) throws SQLException{
return EjecutarConsulta("insert into " + this.NombreTabla + " values (" + Valores + ")");
}
public boolean Insert() {
String valores = "";
for (int i = 0; i < this.Columnas_Valores.length; i++) {
valores += ",'" + this.Columnas_Valores[i]+"' ";
}
return this.Actualizar("insert into " + this.NombreTabla + " values (" + valores.substring(1) + ")");
}
public boolean Delete(int col) {
return this.Actualizar("delete from " + this.NombreTabla + " where "
+ this.Columnas_Nombres[col] + "='" + this.Columnas_Valores[col] + "'");
}
public boolean Delete(String Condicion) {
return this.Actualizar("delete from " + this.NombreTabla + " where "
+Condicion);
}
public boolean Update() {
String valores = "";
for (int i = 0; i < this.Columnas_Valores.length; i++) {
valores += ", " + this.Columnas_Nombres[i] + "='" + this.Columnas_Valores[i] + "' ";
}
return this.Actualizar("update " + this.NombreTabla +" set "+ valores.substring(1)
+ "where " + this.Columnas_Nombres[0] + "='" + this.Columnas_Valores[0] + "'");
}
public String[] GenerarColumnas_Nombres() {
try {
DefaultTableModel modelo;
ResultSet rs = ConsultaSQL.executeQuery("select * from " + this.NombreTabla);
ResultSetMetaData metadatos = rs.getMetaData();
int nrocol = metadatos.getColumnCount();
String[] etiquetas = new String[nrocol];
for (int i = 0; i < nrocol; i++) {
etiquetas[i] = (metadatos.getColumnLabel(i + 1)).toString();
}
return etiquetas;
} catch (Exception ex) {
return null;
}
}
public boolean ExecQuery(String Consulta){
try{ConsultaSQL.execute(Consulta);
return true;
}catch(Exception ex){
System.out.println(ex.getMessage());
return false;
}
}
public DefaultTableModel EjecutarConsulta(String Consulta) throws SQLException {
ConsultaSQL.execute(Consulta);
DefaultTableModel modelo;
ResultSet rs = ConsultaSQL.getResultSet();
ResultSetMetaData metadatos = rs.getMetaData();
int nrocol = metadatos.getColumnCount();
Object[] etiquetas = new Object[nrocol];
for (int i = 0; i < nrocol; i++) {
etiquetas[i] = metadatos.getColumnLabel(i + 1);
}
modelo = new DefaultTableModel();
modelo.setColumnIdentifiers(etiquetas);
Object[] fila = new Object[nrocol];
if (rs != null) {
while (rs.next()) {
for (int i = 0; i < nrocol; i++) {
fila[i] = rs.getObject(i + 1);
}
modelo.addRow(fila);
}
}
return modelo;
}
// </editor-fold>
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CapaLogica;
/**
*
* @author User
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
/**
*
* @author User
*/
public class CConexion {
// <editor-fold defaultstate="collapsed" desc="Atributos">
String Ruta;
String NombreBD;
String User;
String Password;
Connection Conexion;
Statement ConsultaSQL;
boolean Conexion_Abierta;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Propiedades">
public String getNombreBD() {
return NombreBD;
}
public void setNombreBD(String NombreBD) {
this.NombreBD = NombreBD;
}
public String getPassword() {
return Password;
}
public void setPassword(String Password) {
this.Password = Password;
}
public String getRuta() {
return Ruta;
}
public void setRuta(String Ruta) {
this.Ruta = Ruta;
}
public String getUser() {
return User;
}
public void setUser(String User) {
this.User = User;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Metodos">
public CConexion(String Ruta, String NombreBD, String User, String Password) {
this.Ruta = Ruta;
this.NombreBD = NombreBD;
this.User = User;
this.Password = Password;
}
public void AbrirConexion() {
try {
if (Conexion_Abierta) {
CerrarConexion();
}
Class.forName("com.mysql.jdbc.Driver");
Conexion = DriverManager.getConnection("jdbc:mysql://localhost/" + NombreBD, "root", Password);
ConsultaSQL = Conexion.createStatement();
Conexion_Abierta = true;
} catch (Exception ex) {
}
}
public void CerrarConexion() throws SQLException {
if (!Conexion_Abierta) {
Conexion.close();
}
}
public ResultSet Seleccionar(String Consulta) throws SQLException {
ConsultaSQL.execute(Consulta);
return ConsultaSQL.getResultSet();
}
public boolean Actualizar(String Consulta) {
try {
ConsultaSQL.execute(Consulta);
return true;
} catch (SQLException ex) {
return false;
}
}
public Vector<String> MostrarTablas() throws SQLException {
Vector<String> Tablas = new Vector<String>();
try {
ConsultaSQL.execute("show tables;");
ResultSet rs = ConsultaSQL.getResultSet();
String ntablas = "";
if (rs != null) {
while (rs.next()) {
Tablas.add(rs.getString(1));
}
}
return Tablas;
} catch (Exception ex) {
return Tablas;
}
}
public Vector<String> MostrarBD() throws SQLException {
Vector<String> Tablas = new Vector<String>();
try {
ConsultaSQL.execute("show databases;");
ResultSet rs = ConsultaSQL.getResultSet();
String ntablas = "";
if (rs != null) {
while (rs.next()) {
Tablas.add(rs.getString(1));
}
}
return Tablas;
} catch (Exception ex) {
return Tablas;
}
}
// </editor-fold>
}
No hay comentarios:
Publicar un comentario