Codigo de Sistema de Base de Datos

Codigo de Sistema de Base de Datos

Clase Coneccion DB package sistemaSida; import java.sql.*; import javax.swing.*; public class coneBD { public static PreparedStatement prest=null; public static CallableStatement cllst=null; public static Connection conec=null; public static Statement st=null; public static ResultSet rt=null; public static String usu; public static String contraseña; public static String host; public static String db; public void Conectar(){ try{usu="root";contraseña="";host="localhost"; db="sida"; String url="jdbc:mysql://"+host+"/"+db; Class.forName("com.mysql.jdbc.Driver").newInstance(); conec=DriverManager.getConnection(url, usu, contraseña); } catch(Exception e){ e.printStackTrace(); } }

public void ConectarDinamyc(String us,String clv,String hs,String bs){ try{ usu=us;contraseña=clv;host=hs; db=bs; String url="jdbc:mysql://"+host+"/"+db; Class.forName("com.mysql.jdbc.Driver").newInstance(); conec=DriverManager.getConnection(url, usu, contraseña); } catch(Exception e){ e.printStackTrace(); } }

} Clase Controlador package sistemaSida; import java.awt.event.*;import javax.swing.*;import javax.swing.table.*; public class Clscontrolador { static String[] Usuario=new String[1];static String sq=""; static coneBD cn=new coneBD(); static ClsCodificador Codigo=new ClsCodificador(); static ClsVisualizador Vsz=new ClsVisualizador(); static Mimodelo mdtb=new Mimodelo(); public Clscontrolador(){ try{

} catch(Exception e){

e.printStackTrace(); } } public void Solonumeros(KeyEvent e){ if((e.getKeyChar()<48)&&(e.getKeyChar()>57)){ e.consume(); } } public void NoExcribe(KeyEvent E){ E.consume(); } public void NoNuevo(JButton b,JButton b1, String cd, String cd1, KeyEvent E){ if((b.getText().compareTo(cd)==0)&&(b1.getText().compareTo(cd1)==0)){ E.consume(); } } public void NoNuevo1(JButton b,String cd,KeyEvent E){ if((b.getText().compareTo(cd)==0)) E.consume(); } public void Mayusculas(JTextField tx){ tx.setText(tx.getText().toUpperCase()); } public void MarcaTxt(JTextField txt){ txt.setSelectionStart(0);txt.setSelectionEnd(txt.getText().length());txt.requestFocus(); } public void MarcaTxt(JPasswordField ps){

ps.setSelectionStart(0);ps.setSelectionEnd(ps.getText().length());ps.requestFocus(); } public void Enter(KeyEvent e, JButton btn){ if(e.getKeyChar()==10){ btn.doClick(); } } public void Mayus(JTextField txt){ txt.setText(txt.getText().toUpperCase()); } public void SinEspcBlanco(KeyEvent e,JTextField t){ if(e.getKeyChar()==32){ if(t.getCaretPosition()==0){ e.consume(); } else{ if(t.getCaretPosition()<t.getText().length()){ if((t.getText().charAt(t.getCaretPosition()-1)==' ')||(t.getText().charAt(t.getCaretPosition())==' ')) e.consume(); } else{ if(t.getText().charAt(t.getCaretPosition()-1)==' ') e.consume(); } } } }

public void Volvercbo(JComboBox Cbo){ if(Cbo.getSelectedIndex()==-1){ Cbo.requestFocus(); } } public void LlenaCbs(JComboBox cbu, String consulta){ try{cn.Conectar(); cn.st=cn.conec.createStatement(); cn.rt=cn.st.executeQuery(consulta); while(cn.rt.next()) cbu.addItem(cn.rt.getString(1)); cbu.setSelectedIndex(-1); } catch(Exception E){ E.printStackTrace(); } } public void LlenaCbsConprocalmac(JComboBox cbu, String consulta){ try{cn.Conectar();String sq="{call "+consulta+"()}"; cn.st=cn.conec.createStatement();cn.rt=cn.st.executeQuery(sq); while(cn.rt.next()) cbu.addItem(cn.rt.getString(1).toString()); cbu.setSelectedIndex(-1); } catch(Exception E) { E.printStackTrace();

} } public boolean VerifClv(String usu,String psw){ boolean b=false;cn.Conectar(); try{String sq="select* from usuarios where idusuarios='"+usu+"' and clave='"+psw+"';"; cn.st=cn.conec.createStatement(); cn.rt=cn.st.executeQuery(sq); b=cn.rt.next(); } catch(Exception e){ System.out.println(e.getMessage()); } return b; } public boolean BscrExiste(String tba,String cmpo, String vlr){ boolean b=false; sq="select * from "+tba+" where "+cmpo+"='"+vlr+"'"; return b; } public void LimpiTabla(DefaultTableModel md){ while(md.getRowCount()>0){ md.removeRow(0); } } public boolean Insertar(String dato,String Tba,String cmpcod,String cmpnom){ boolean b=true; try{

if(Codigo.VerificaNorepite(Tba, cmpnom, dato)==false){ String cod=Codigo.CodigoValido(Tba, cmpcod,dato); sq="insert into "+Tba+" values('"+cod+"','"+dato+"');"; cn.st= cn.conec.createStatement(); int p=cn.st.executeUpdate(sq); } else{ Vsz.Dlg.showMessageDialog(null,dato+" Ya existe");b=false; } } catch(Exception e){ e.getStackTrace(); } return b; } public boolean InsertarProcAlmc(String dato,String dato3,String dato4,int dato5,String dato6,String Tba,String cmpcod,String cmpnom){ boolean b=true; try{ if(Codigo.VerificaNorepite(Tba, cmpnom, dato)==false){ String cod=Codigo.CodigoValido(Tba, cmpcod,dato); try{cn.prest=cn.conec.prepareCall("{call insertarper(?,?,?,?,?,?)}"); cn.prest.setString(1, cod); cn.prest.setString(2, dato); cn.prest.setString(3, dato3); cn.prest.setString(4, dato4); cn.prest.setInt(5, dato5); cn.prest.setString(6, dato6);

int p=cn.prest.executeUpdate(); } catch(Exception e){ System.out.println(e.getMessage()); } } else{ Vsz.Dlg.showMessageDialog(null,dato+" Ya existe");b=false; } } catch(Exception e){ e.getStackTrace(); } return b; } public boolean ActualizarTbla(String tb, String DtAc,String cmpact,String cdc,String cmpcond){ boolean b=false; try{ sq="select * from "+tb+" where "+cmpact+"='"+DtAc+"' and "+cmpcond+"<>'"+cdc+"'"; cn.st=cn.conec.createStatement(); cn.rt=cn.st.executeQuery(sq); if(cn.rt.next()==false){ sq="update "+tb+" set "+cmpact+"='"+DtAc+"' where "+cmpcond+"='"+cdc+"';"; cn.st=cn.conec.createStatement(); int r=cn.st.executeUpdate(sq); b=true; } else

b=false; } catch(Exception e){ e.getMessage(); } return b; } public boolean ActualizarTblaConProCategoria(String cod,String dato,String dato3,String dato4,int dato5,String dato6){ boolean b=false; try{cn.prest=cn.conec.prepareCall("{call ActzCategoria(?,?,?,?,?,?)}"); cn.prest.setString(1, cod);cn.prest.setString(2, dato); cn.prest.setString(3, dato3); cn.prest.setString(4, dato4); cn.prest.setInt(5, dato5); cn.prest.setString(6, dato6); int p=cn.prest.executeUpdate(); } catch(Exception e){ System.out.println(e.getMessage()); } return b; } public void Eliminar(String tbl, String cmpvld,String vlr){ try{sq="delete from "+tbl+" where "+cmpvld+"='"+vlr+"';"; cn.st=cn.conec.createStatement(); int r=cn.st.executeUpdate(sq); }

catch(Exception e){ e.printStackTrace(); } } public void EliminaCtgcnpro(String cd){ try{cn.prest=cn.conec.prepareCall("{call ElimPersona(?)}"); cn.prest.setString(1, cd); int p=cn.prest.executeUpdate(); cn.rt= cn.prest.executeQuery(); while(cn.rt.next()){ String g=cn.rt.getString(1); } } catch(Exception e){ System.out.println(e.getMessage()); } } } Clase Acceso package sistemaSida; import javax.swing.*; import java.sql.*; import java.awt.event.*; public class FrmAcceso extends javax.swing.JFrame { coneBD cn=new coneBD(); Clscontrolador ctrl=new Clscontrolador(); JOptionPane msg=new JOptionPane();

ClsVisualizador Vslz=new ClsVisualizador(); FrmRegistro registro=new FrmRegistro(); int c=0; public FrmAcceso() { initComponents(); ctrl.LlenaCbs(CbUsu,"select * from usuarios;"); } public void Acceso(){ if(pw.getText().length()==0) pw.requestFocus(); else Vslz.Acceso(registro, this, CbUsu, pw,6); } private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) { Acceso(); }

private void pwKeyPressed(java.awt.event.KeyEvent evt) { ctrl.Enter(evt, BtnAceptar); }

private void CbUsuItemStateChanged(java.awt.event.ItemEvent evt) { pw.requestFocus(); }

private void pwFocusGained(java.awt.event.FocusEvent evt) { ctrl.Volvercbo(CbUsu);

}

private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) { Vslz.Salir(this); } Clase Visualizador package sistemaSida; import javax.swing.*; import javax.swing.table.*; public class ClsVisualizador { JOptionPane Dlg=new JOptionPane(); Clscontrolador Ctld=new Clscontrolador(); int intentos=0; public void VerenTabla(DefaultTableModel mdltb,String[] dts,String cnslt){ try{Ctld.LimpiTabla(mdltb); Ctld.cn.Conectar(); Ctld.cn.st=Ctld.cn.conec.createStatement(); Ctld.cn.rt=Ctld.cn.st.executeQuery(cnslt); while(Ctld.cn.rt.next()){ dts[0]=Ctld.cn.rt.getString(1); dts[1]=Ctld.cn.rt.getString(2); dts[2]=Ctld.cn.rt.getString(3); dts[3]=Ctld.cn.rt.getString(4); dts[4]=String.valueOf(Ctld.cn.rt.getDate(8)); dts[5]=Ctld.cn.rt.getString(5); mdltb.addRow(dts); }

} catch(Exception E){ Dlg.showMessageDialog(null, E.getMessage());

} } public void Salir(JFrame frm){ if(Dlg.showConfirmDialog(null,"Deseas Salir","Confirmar",0)==0) frm.dispose(); } public void Mostrar(JFrame frm){ frm.setVisible(true); } public void Acceso(JFrame frmacc,JFrame frm,JComboBox Cbo, JPasswordField psw,int cint){ intentos++; if(Ctld.VerifClv(Cbo.getSelectedItem().toString(), psw.getText())){ frm.dispose();frmacc.setVisible(true); } else{ if(intentos==1) Dlg.showMessageDialog(null, "Lleva un intento"); else{ if(intentos==cint){ Dlg.showMessageDialog(null, "Ya van"+cint+" Intentos se cerrara el Sistema"); frm.dispose(); } else{ Dlg.showMessageDialog(null, "Llevas "+intentos+" intentos"); } }

Ctld.MarcaTxt(psw); } } public void LlamaProgramas(String Prg){ try{ Runtime rt=Runtime.getRuntime(); Process p=rt.exec(Prg); p.waitFor(); } catch(Exception e){ e.printStackTrace(); } }

public String CargarDts(DefaultTableModel mdtb, JTable tb,JTextField tx,JTextField tx3,JComboBox cbo,JTextField txF,JTextField txG){ int p;String cd="",se="",fe="",cad=""; if(mdtb.getRowCount()>0){ p=tb.getSelectedRow();cd=mdtb.getValueAt(p, 0).toString(); tx.setText(mdtb.getValueAt(p, 1).toString()); tx3.setText(mdtb.getValueAt(p, 2).toString()); se=mdtb.getValueAt(p, 3).toString(); if (se.compareTo("Masculino")==0){ cbo.setSelectedIndex(0); } else{ cbo.setSelectedIndex(1); }

fe=mdtb.getValueAt(p, 4).toString();

cad=fe.substring(8, 10)+"/"+fe.substring(5, 7)+"/"+fe.substring(0, 4); txF.setText(cad); txG.setText(mdtb.getValueAt(p, 5).toString()); } else{ Dlg.showMessageDialog(null,"No hay nada que mostrar","Aviso",2); } return cd; } } Clase Registro package sistemaSida; import javax.swing.*; public class FrmRegistro extends javax.swing.JFrame { Clscontrolador Ctrl=new Clscontrolador();ClsVisualizador Ver=new ClsVisualizador(); String[] Cabecera={"Codigo","Nombres","Apellidos","sexo"};String[] DtsCategoria=new String[6];String CdCtg=""; /** Creates new form FrmRegistro */ public void Iniciar(){ initComponents();Tbl.setModel(Ctrl.mdtb);Ctrl.mdtb.addColumn("Codigo");

Ctrl.mdtb.addColumn("Nombres");Ctrl.mdtb.addColumn("Apellidos");Ctrl.mdtb.addColumn("S exo"); Ctrl.mdtb.addColumn("Fecha");Ctrl.mdtb.addColumn("Grupo_Sanguineo"); Tbl.getColumnModel().getColumn(0).setPreferredWidth(20); Tbl.getColumnModel().getColumn(1).setPreferredWidth(150); Tbl.getColumnModel().getColumn(2).setPreferredWidth(150);

MostrarCategorias(Txbsc);cboSexo.setSelectedIndex(-1);

} public FrmRegistro() { Iniciar(); } public void MostrarCategorias(JTextField T){ /*Ctrl.sq="select * from categoria where (codctg like'"+Txbsc.getText()+"%' or " +"nomctg like'"+Txbsc.getText()+"%')";Ver.VerenTabla(Ctrl.mdtb, DtsCategoria, Ctrl.sq);*/ Ctrl.sq="select * from personas where (idpersonas like'"+T.getText()+"%' or " +"nombres like'"+T.getText()+"%') order by nombres";Ver.VerenTabla(Ctrl.mdtb, DtsCategoria, Ctrl.sq); } public void LimpiarTextos(){ Txbsc.setText(null) ; Txper.setText(null);TxtApe.setText(null);cboSexo.setSelectedIndex(1);TxtFecha.setText(null); TxtGrupo.setText(null); } public int DFecha(){ String dia,mes,year,cad; dia= TxtFecha.getText().substring(0, 2); mes=TxtFecha.getText().substring(3, 5); year=TxtFecha.getText().substring(6,10 ); cad=year+mes+dia; return Integer.parseInt(cad); } public void Nuevo(){ if(BN.getText().compareTo("Nuevo")==0){

Txper.requestFocus();BN.setText("Grabar"); } else{ if(Txper.getText().trim().length()==0){ Ver.Dlg.showMessageDialog(this,"Faltan Datos");Txper.requestFocus(); } else{

if(Ctrl.InsertarProcAlmc(Txper.getText(),TxtApe.getText(),cboSexo.getSelectedItem().toString(), DFecha(),TxtGrupo.getText(),"personas","idpersonas","nombres")){ MostrarCategorias(Txbsc);BN.setText("Nuevo"); } } } LimpiarTextos(); } public void Editar(){ BN.setText("Nuevo"); if(Tbl.getSelectedRow()<0){ Ver.Dlg.showMessageDialog(this,"Primero seleccione una fila","Aviso",2); return; } if(Ctrl.mdtb.getRowCount()==0){ Ver.Dlg.showMessageDialog(this,"No hay nada para editar","Aviso",2); return; } if(BD.getText().compareTo("Editar")==0){

BD.setText("Grabar");CdCtg=Ver.CargarDts(Ctrl.mdtb, Tbl, Txper,TxtApe,cboSexo,TxtFecha,TxtGrupo); //Cargar(); Txper.requestFocus(); } else{ //Ctrl.ActualizarTbla("Categoria",Txmrc.getText().toUpperCase(),"nomctg",CdCtg,"codctg"); Ctrl.ActualizarTblaConProCategoria(CdCtg, Txper.getText(),TxtApe.getText(),cboSexo.getSelectedItem().toString(),DFecha(),TxtGrupo.getT ext()); MostrarCategorias(Txbsc);CdCtg=null;BD.setText("Editar"); } } public void Eliminar(){ if(Ctrl.mdtb.getRowCount()==0) Ctrl.Vsz.Dlg.showMessageDialog(this,"No hay registros para eliminar","Aviso",3); else{ if(Ctrl.Vsz.Dlg.showConfirmDialog(this,"Seguro quieres eliminar el valor","Confirmar",0)==0){ Ctrl.EliminaCtgcnpro(Ctrl.mdtb.getValueAt(Tbl.getSelectedRow(), 0).toString()); MostrarCategorias(Txbsc); } } } /*String result; Connection con; CallableStatement mf=con.prepareCall( {?call sf_login_check(?)} ); mf.registerOutParameter(1,TypesVARCHAR); mf.setString(2, test );

mf.execute(); result=mf.getString(1);

*/

public void Cancelar(){ BN.setText("Nuevo");BD.setText("Editar");LimpiarTextos(); } private void TxperKeyReleased(java.awt.event.KeyEvent evt) { Ctrl.Mayusculas(Txper); }

private void TxperKeyTyped(java.awt.event.KeyEvent evt) { Ctrl.NoNuevo(BN,BD,"Nuevo","Editar",evt); }

private void BNActionPerformed(java.awt.event.ActionEvent evt) { Nuevo(); }

private void BDActionPerformed(java.awt.event.ActionEvent evt) { Editar(); }

private void BEActionPerformed(java.awt.event.ActionEvent evt) { Eliminar(); }

private void BCActionPerformed(java.awt.event.ActionEvent evt) { Cancelar(); }

private void BSActionPerformed(java.awt.event.ActionEvent evt) { Ver.Salir(this); }

private void TxbscKeyReleased(java.awt.event.KeyEvent evt) { MostrarCategorias(Txbsc); }

private void TxtApeKeyReleased(java.awt.event.KeyEvent evt) { Ctrl.Mayusculas(TxtApe); }

private void TxtApeKeyTyped(java.awt.event.KeyEvent evt) { Ctrl.NoNuevo(BN,BD,"Nuevo","Editar",evt); } Clase Mimodelo package sistemaSida; import javax.swing.*; import javax.swing.table.*; public class Mimodelo extends DefaultTableModel{ public boolean isCellEditable (int row, int column) { if (column == 2)

return true; return false; } }

Sign up to vote on this title
UsefulNot useful