Foros del Web » Programación para mayores de 30 ;) » Java »

ayuda con jdbc y base de datos ayuda novata

Estas en el tema de ayuda con jdbc y base de datos ayuda novata en el foro de Java en Foros del Web. aqui esta el codigo de mi programa lo que tiene que hacer es relacionar dos tablas y navegar en ellas con los botones primero ultimo ...
  #1 (permalink)  
Antiguo 29/11/2005, 11:31
 
Fecha de Ingreso: septiembre-2004
Mensajes: 212
Antigüedad: 19 años, 7 meses
Puntos: 0
ayuda con jdbc y base de datos ayuda novata

aqui esta el codigo de mi programa lo que tiene que hacer es
relacionar dos tablas
y navegar en ellas con los botones primero ultimo siguiente y anterior eso lo hace sin ningun problema
borrar un registro tambien lo hace
agregar tambien agrega
pero el buscar me busca solo en una tabla y nose como hacer para que busque en las dos me pueden explicar eso como hacerlo porfavor
y el modificar tampoco se como se hace desde ya muchas gracias



Código PHP:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class 
AccessBD extends Frame implements ActionListener //
{
    
// Creando los Objetos
    
Button btnSigbtnAntbtnPrimbtnUltbtnBorrarbtnActualibtnBorbtnAcep;
    
Button btnLimpbtnGrabbtnBusc;
    
TextField txtRuttxtNomtxtApelltxtDirtxtBusca;
    
TextField txtNota1txtNota2txtNota3;


    
Label lblError = new Label("");

    
Connection cCon;
    
ResultSet rsAlumno;
    
ResultSet rsNotas;
    
Statement stStat,stStat1;
    
String strSQLstrSQL1;
;

    public 
AccessBD()

    {
        
//inicio ventana
        
setLayout(null);
        
setLocation(200,200);
        
setSize(600,450);
        
setTitle("NAVEGACION DE BD");




        
// Instanciando los Objetos
        
btnSig = new Button("Siguiente");
        
btnAnt = new Button("Anterior");
        
btnPrim = new Button("Primero");
        
btnUlt = new Button("Ultimo");
        
btnBor = new Button("BORRAR");
        
btnLimp = new Button("Agre.Alumno");
        
btnGrab = new Button("Grabar Alum.");
        
btnBusc = new Button("Buscar");
        
btnAcep = new Button("Aceptarr");

        
txtRut = new TextField ("");
        
txtNom = new TextField ("");
        
txtApell = new TextField ("");
        
txtDir = new TextField ("");
        
txtBusca = new TextField ("");
        
txtNota1= new TextField ("");
        
txtNota2= new TextField ("");
        
txtNota3= new TextField ("");
        
//lblEroor= new Label("");

        //Llamado de objetos

        
txtRut.setBounds(150,50,100,30);
        
txtNom.setBounds(150,100,100,30);
        
txtApell.setBounds(150,150,100,30);
        
txtDir.setBounds(150,200,100,30);
        
btnBusc.setBounds(350,300,80,30);
        
btnAcep.setBounds(350,350,80,30);
        
txtNota1.setBounds(350,100,100,30);
        
txtNota2.setBounds(350,150,100,30);
        
txtNota3.setBounds(350,200,100,30);


        
btnPrim.setBounds(50,250,80,30);
        
btnSig.setBounds(250,250,80,30);
        
btnAnt.setBounds(150,250,80,30);
        
btnUlt.setBounds(350,250,80,30);
        
btnBor.setBounds(50,300,80,30);
        
btnLimp.setBounds(150,300,80,30);
        
btnGrab.setBounds(250,300,80,30);
        
txtBusca.setBounds(450,300,100,30);

        
lblError.setBounds(10,10,100,30);
        
lblError.setText("Hola");

        
// Adisiona Objetos

        
add(txtRut);
        
add(txtNom);
        
add(txtApell);
        
add(txtDir);
        
add(btnSig);
        
add(btnAnt);
        
add(btnPrim);
        
add(btnUlt);
        
add(btnBor);
        
add(txtNota1);
        
add(txtNota2);
        
add(txtNota3);
        
add(btnLimp);
        
add(btnGrab);
        
add(btnBusc);
        
add(lblError);
        
add(txtBusca);
        
add(btnAcep);

    
btnPrim.addActionListener(this);
    
btnSig.addActionListener(this);
    
btnAnt.addActionListener(this);
    
btnUlt.addActionListener(this);
    
btnBor.addActionListener(this);
    
btnLimp.addActionListener(this);
    
btnGrab.addActionListener(this);
    
btnBusc.addActionListener(this);
    
btnAcep.addActionListener(this);
    
//inicio ocultado



    //coneccion base de datos

    
String sDriver ="sun.jdbc.odbc.JdbcOdbcDriver";
    
String sUrl "jdbc:odbc:AccessBD" ;
    try {
        Class.
forName(sDriver);
            }
    catch(
ClassNotFoundException ex)
    {
    }
    try{

        
cCon DriverManager.getConnection(sUrl"""");     //  "", "" es user y pasword
        
stStat cCon.createStatement(10051008); // 1005 = cursor sensitive(mas conveniente); 1004 = insensitive ;1003 = forward_only (defecto)
                                            //1007 read_only     1008 cur_omly
        
stStat1 cCon.createStatement(10051008);
    
String sSql2 "Select * from alumno";

        
rsAlumno stStat.executeQuery(sSql2);
        
rsAlumno.next();
        
mostrar();




            }

        catch(
Exception ex){ }



    
btnAnt.setEnabled(false);
    
btnPrim.setEnabled(false);
    
btnAcep.setEnabled(false);

    }

    public 
void actionPerformed(ActionEvent ev)
    {
        try{

            if (
ev.getSource()== btnPrim)
        {
            
rsAlumno.first();
            
mostrar();
            
btnSig.setEnabled(true);
            
btnAnt.setEnabled(false);
            
btnUlt.setEnabled(true);
        }
            if (
ev.getSource()== btnSig)
        {

            
rsAlumno.next();

            if (
rsAlumno.isLast())
            {
                
btnSig.setEnabled(false);
                
btnUlt.setEnabled(false);
            }
            
mostrar();
            
btnAnt.setEnabled(true);
            
btnPrim.setEnabled(true);
        }



            if (
ev.getSource()== btnAnt)
        {
            
rsAlumno.previous();
            if (
rsAlumno.isFirst())
            {
                
btnAnt.setEnabled(false);
                
btnPrim.setEnabled(false);
            }

            
mostrar();
            
btnSig.setEnabled(true);

            
btnUlt.setEnabled(true);
        }
            if (
ev.getSource()== btnUlt)
        {
            
rsAlumno.last();
            
mostrar();
                
btnAnt.setEnabled(true);
                
btnSig.setEnabled(false);
                
btnPrim.setEnabled(true);
                
btnUlt.setEnabled(false);
        }

        if(
ev.getSource() == btnBor)
        {

            try
            {
            
String sSql1 "Delete from alumno Where rut='" txtRut.getText() + "'";
            
stStat.execute(sSql1);
            
sSql1 "Select * from alumno";
            
rsAlumno =     stStat.executeQuery(sSql1);
            
rsAlumno.next();
            
mostrar();


            } catch(
SQLException Se){System.out.println("error");            }

            
btnAnt.setEnabled(false);
            
btnSig.setEnabled(true);
            
btnPrim.setEnabled(false);
        }





        if (
ev.getSource()== btnLimp)
        {
            
btnGrab.setEnabled(true);
        
txtRut.setText("");
        
txtNom.setText("");
        
txtApell.setText("");
        
txtDir.setText("");
        
txtNota1.setText("");
        
txtNota2.setText("");
        
txtNota3.setText("");

        
btnLimp.setEnabled(false);
        
btnBor.setEnabled(false);
        
btnUlt.setEnabled(false);
        
btnAnt.setEnabled(false);
        
btnPrim.setEnabled(false);
        
btnSig.setEnabled(false);
        
btnBusc.setEnabled(false);

        }

        if (
ev.getSource()== btnGrab)
        {
            try
            {
            
stStat.execute("Insert Into alumno (rut,nombre,apellido,direccion)" +    "values ('" txtRut.getText()+ "','" txtNom.getText()+"','"+txtApell.getText()+"','"+txtDir.getText()+"')");
            
stStat1.execute("Insert Into nota (rut,nota1 ,nota2 ,nota3)" +    "values ('" txtRut.getText()+ "','" txtNota1.getText()+ "','" txtNota2.getText()+"','"+txtNota3.getText()+"')");
                
//mostrar();

            
String sSql1 "Select * from alumno";
            
rsAlumno =     stStat.executeQuery(sSql1);
            
rsAlumno.next();
            
mostrar();

            }
            catch(
SQLException se){    System.out.print("no grabo");        }

            
//rsAlumno.last();
            //mostrar();


            
btnLimp.setEnabled(true);
            
btnGrab.setEnabled(false);

            
btnBor.setEnabled(true);
            
btnUlt.setEnabled(true);
            
btnAnt.setEnabled(true);
            
btnPrim.setEnabled(true);
            
btnSig.setEnabled(true);
            
btnBusc.setEnabled(true);

        }


        
//fin grabar

        
if (ev.getSource()== btnBusc)
        {

            
String p,n="",c=""s="";
            
String a=""b=""e=""d="";
            
String q=txtBusca.getText();
            
int i=0;
            try
            {

                
stStat.close();

                
stStat=cCon.createStatement();
                
stStat1=cCon.createStatement();
                
strSQL="Select * from alumno";
                
strSQL1="Select * from nota";

                
rsAlumno=stStat.executeQuery(strSQL);
                
rsNotas=stStat1.executeQuery(strSQL1);
                while((
rsAlumno.next()) && (i==0))
                {

                    
p=rsAlumno.getString("rut");


                    if(
p.equals(q))
                    {

                        
n=rsAlumno.getString("nombre");
                        
c=rsAlumno.getString("apellido");
                        
s=rsAlumno.getString("direccion");
                        
a=rsNotas.getString("nota1");
                        
b=rsNotas.getString("nota2");
                               
e=rsNotas.getString("nota3");
                        
i=1;
                    }
                }
                if(
i==1)
                {
                    
txtRut.setText(q);
                    
txtNom.setText(n);
                    
txtApell.setText(c);
                    
txtDir.setText(s);


                }
                else
                {
                
txtRut.setText("Usuario no existe");
                
txtNom.setText("Usuario no existe");
                
txtApell.setText("Usuario no existe");
                
txtDir.setText("Usuario no existe");
                
txtBusca.setText("Usuario no existe");
                }

            }
            catch(
SQLException se){System.out.print("error al buscar");        }

        
btnAcep.setEnabled(true);

        }

        if (
ev.getSource()== btnAcep)
        {

            
String sSql1 "Select * from alumno";
            
rsAlumno =     stStat.executeQuery(sSql1);
            
rsAlumno.next();
            
mostrar();

            
btnAcep.setEnabled(false);
            
btnAnt.setEnabled(false);
            
btnPrim.setEnabled(false);
            
btnSig.setEnabled(true);
            
btnUlt.setEnabled(true);
        }



        }




        catch(
Exception ex){ setTitle("ERROR 2");    }

    }


    public static 
void main(String args[])
    {
        
// Visualizando la Clase
        
new AccessBD().setVisible(true);

    }


    
//prosedimiento para mostrar

    
public void mostrar()
    {

        try
        {

        
txtRut.setText(rsAlumno.getString("rut"));
        
txtNom.setText(rsAlumno.getString("nombre"));
        
txtApell.setText(rsAlumno.getString("apellido"));
        
txtDir.setText(rsAlumno.getString("direccion"));

        
String sSql3 "Select * from nota Where rut='" txtRut.getText() + "'";
        
rsNotas stStat1.executeQuery(sSql3);
        if (
rsNotas.next())
                
mostrarNota();

         else{
             
txtNota1.setText("");
            
txtNota2.setText("");
            
txtNota3.setText("");


         }




        }
        catch(
Exception ex){setTitle("ERROR 1");        }


    }

        public 
void mostrarNota()
    {
        try
        {



        
txtNota1.setText(rsNotas.getString("nota1"));
        
txtNota2.setText(rsNotas.getString("nota2"));
        
txtNota3.setText(rsNotas.getString("nota3"));

        }
        catch(
Exception ex){setTitle("ERROR 5555");        }


    }


__________________
http://trucosinformatica.blogspot.com/
http://rickblogblog.blogspot.com/ el peor problema de facebook
  #2 (permalink)  
Antiguo 29/11/2005, 14:26
Avatar de goncafa  
Fecha de Ingreso: julio-2002
Ubicación: Santiago
Mensajes: 1.211
Antigüedad: 21 años, 10 meses
Puntos: 10
Tu problema no tiene nada que ver con Java ni con JDBC, es de SQL.
Para eso esta el foro de Base de datos, ahi debes preguntar como hacer el JOIN entre 2 o mas tablas.

Pero basicamente una consulta a dos tablas se hace asi

SELECT * FROM TABLA1, TABLA2 WHERE TABLA1.ID = TABLA2.ID;

Esa seria una consulta simple que arroja todos los datos de TABLA1 y TABLA2 donde el ID de TABLA1 dea igual al ID de TABLA2

Saludos
__________________
se despide hasta la proxima
Gonzalo Castillo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:10.