• Miércoles 8 de Mayo de 2024, 22:15

Autor Tema:  Conexion Acces Y Java .. Insertar Registros..  (Leído 3874 veces)

Blizknight

  • Miembro activo
  • **
  • Mensajes: 41
    • Ver Perfil
Conexion Acces Y Java .. Insertar Registros..
« en: Sábado 2 de Septiembre de 2006, 07:38 »
0
Hola a todos... pues despues de luchar mucho por conectar la dbase lo logre.... pero ahora tengo el problema de como insertar filas ya que en los libros utilizan algo de ObInserccion Obregistro... Obconsulta y varios asi.... pero un amigo me dio un codigo diferente... aunque me dijo que habia un error deberia de funcionar.... y me preguntaba si alguien me puede dar una mano ....

pues lo que basicamente quiero es insertar datos con la presion del boton...
y bueno luego queria tb aprender como consutlar y listar ....
pero primero poco a poco  :(


aqui les dejo la dbase y espero poder resolver mi duda  :(


Código: Text
  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.sql.*;
  5.  
  6.  
  7. public class Inscripcion extends JFrame
  8. {
  9.    //declaracion de variables
  10.    JFrame main, main2;
  11.    JLabel lblname;
  12.    JTextField name;
  13.    static private Statement sql;
  14.    private ResultSet resultInsertar;
  15.    static JButton btn_ok;
  16.  
  17.    
  18.    //crea la interfaz grafica de usuario GUI (ves como le cambie el nombre)
  19.    public Inscripcion()
  20.    {
  21.        setSize(500,300);  //Tamanio del formulario
  22.        setTitle("OTM"); //TItulo del formulario
  23.        setResizable(false); //Para que no sea redimensionable
  24.        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Para salir al cerrar
  25.  
  26.        getContentPane().setLayout(null);
  27.        
  28.        lblname = new JLabel("Nombre");
  29.        lblname.setBounds(50,100,100,23);
  30.        getContentPane().add(lblname);
  31.        
  32.        name = new JTextField();
  33.        name.setBounds(155,100,200,23);
  34.        getContentPane().add(name);
  35.        
  36.        btn_ok = new JButton("Ingresar");
  37.        btn_ok.setBounds(370,230, 100, 23);
  38.        getContentPane().add(btn_ok);
  39.  
  40.     btn_ok.addActionListener(new ActionListener()
  41.        {
  42.            public void actionPerformed(ActionEvent e)
  43.            {
  44.              try{
  45.                resultInsertar = sql.executeQuery("INSERT INTO name(Nombre) VALUES('" + name.getText() + "')");  
  46.                }catch(SQLException a){System.out.println("Error" + a);}
  47.            }  
  48.        });  
  49.        
  50.    
  51.        
  52.    }
  53.  
  54.    
  55.    //Metodo principal
  56.   public static void main(String args[])
  57.    {
  58.      String nana;
  59.      
  60.      try{
  61.     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  62.     String dbase = "jdbc:odbc:base";
  63.     Connection Conexion =DriverManager.getConnection(dbase);
  64.     sql = Conexion.createStatement();
  65.     ResultSet nombre = sql.executeQuery("SELECT * FROM name");
  66.      
  67.      
  68.        while(nombre.next()){
  69.       nana = nombre.getString("Nombre");
  70.       System.out.println(nana);
  71.     }
  72.    
  73.     nombre.moveToInsertRow();
  74.     nombre.updateString("Nombre",)
  75.    
  76.    
  77.        
  78.     nombre.close();
  79.     Conexion.close();
  80.     sql.close();
  81.    
  82.   }catch(ClassNotFoundException e){
  83.     System.out.println("Clase no encontrada");
  84.     }
  85.    
  86.   catch(SQLException e){
  87.       System.out.println(e);
  88.     }
  89.    
  90.     System.out.println("Sin error al conectar ");
  91.    
  92.  
  93.    
  94.      
  95.        new Inscripcion().setVisible(true);
  96.        
  97.  
  98.    }
  99.    
  100. }
  101.  

Blizknight

  • Miembro activo
  • **
  • Mensajes: 41
    • Ver Perfil
Re: Conexion Acces Y Java .. Insertar Registros..
« Respuesta #1 en: Sábado 2 de Septiembre de 2006, 07:52 »
0
Logre insertar filas en la dbase pero por la consola... y el valor siempre es el mismo.... como inserto por medio del textfield???
saludos

me olvide de ponerles la dbase
es la misma para ambos codes

http://rapidshare.de/files/31648520/otm.mdb.html
nombre logico base

Código: Text
  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.sql.*;
  5.  
  6.  
  7. public class Inscripcion2 extends JFrame
  8. {
  9.    //declaracion de variables
  10.    JFrame main, main2;
  11.    JLabel lblname;
  12.    JTextField name;
  13.  
  14.    JButton btn_ok;
  15.  
  16.    
  17.    //crea la interfaz grafica de usuario GUI (ves como le cambie el nombre)
  18.    public Inscripcion2()
  19.    {
  20.        setSize(500,300);  //Tamanio del formulario
  21.        setTitle("OTM"); //TItulo del formulario
  22.        setResizable(false); //Para que no sea redimensionable
  23.        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Para salir al cerrar
  24.  
  25.        getContentPane().setLayout(null);
  26.        
  27.        lblname = new JLabel("Nombre");
  28.        lblname.setBounds(50,100,100,23);
  29.        getContentPane().add(lblname);
  30.        
  31.        name = new JTextField();
  32.        name.setBounds(155,100,200,23);
  33.        getContentPane().add(name);
  34.        
  35.        btn_ok = new JButton("Ingresar");
  36.        btn_ok.setBounds(370,230, 100, 23);
  37.        getContentPane().add(btn_ok);
  38.  
  39.  
  40.        
  41.    
  42.        
  43.    }
  44.  
  45.    
  46.    //Metodo principal
  47.   public static void main(String args[])
  48.    {
  49.      String nana;
  50.      
  51.      try{
  52.     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  53.     String dbase = "jdbc:odbc:base";
  54.     Connection Conexion =DriverManager.getConnection(dbase);
  55.     Statement sql = Conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);;
  56.     ResultSet nombre = sql.executeQuery("SELECT * FROM name");
  57.      
  58.      
  59.      
  60.    
  61.     nombre.moveToInsertRow();
  62.     nombre.updateString("Nombre","JOJOJINA");
  63.     nombre.insertRow();
  64.    
  65.     while(nombre.next()){
  66.       nana = nombre.getString("Nombre");
  67.       System.out.println(nana);
  68.     }
  69.        
  70.     nombre.close();
  71.     Conexion.close();
  72.     sql.close();
  73.    
  74.   }catch(ClassNotFoundException e){
  75.     System.out.println("Clase no encontrada");
  76.     }
  77.    
  78.   catch(SQLException e){
  79.       System.out.println(e);
  80.     }
  81.    
  82.     System.out.println("Sin error al conectar ");
  83.    
  84.  
  85.    
  86.      
  87.        new Inscripcion2().setVisible(true);
  88.        
  89.  
  90.    }
  91.    
  92. }
  93.  

Nagisa

  • Miembro MUY activo
  • ***
  • Mensajes: 119
  • Nacionalidad: es
    • Ver Perfil
Re: Conexion Acces Y Java .. Insertar Registros..
« Respuesta #2 en: Domingo 3 de Septiembre de 2006, 18:40 »
0
Hola!!

Encapsula la parte de conexion a la BBDD dentro de un metodo, y haz que se llame cuando se pulse el boton.

En el codigo que has pasado, no tiene asocido ningun listener, por lo que el boton solo sirve de adorno!!

Para insertar filas, mira la directiva INSERT en algun sitio de SQL (busca aqui); y usa la cadena correcta dentro del metodo executeQuery.

Saludos!!
   

Blizknight

  • Miembro activo
  • **
  • Mensajes: 41
    • Ver Perfil
Re: Conexion Acces Y Java .. Insertar Registros..
« Respuesta #3 en: Jueves 7 de Septiembre de 2006, 18:36 »
0
pues tal ves hice mal pero en este codigo no esta el listener?.. por que cuando compilo y trato de insertar me da error Y.Y
saludos


Código: Text
  1.    btn_ok.addActionListener(new ActionListener()
  2.       {
  3.           public void actionPerformed(ActionEvent e)
  4.           {
  5.             try{
  6.               resultInsertar = sql.executeQuery("INSERT INTO name(Nombre) VALUES('" + name.getText() + "')");  
  7.               }catch(SQLException a){System.out.println("Error" + a);}
  8.           }  
  9.       });  
  10.  
  11.  


Código: Text
  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.sql.*;
  5.  
  6.  
  7. public class Inscripcion extends JFrame
  8. {
  9.   //declaracion de variables
  10.   JFrame main, main2;
  11.   JLabel lblname;
  12.   JTextField name;
  13.   static private Statement sql;
  14.   private ResultSet resultInsertar;
  15.   static JButton btn_ok;
  16.  
  17.  
  18.   //crea la interfaz grafica de usuario GUI (ves como le cambie el nombre)
  19.   public Inscripcion()
  20.   {
  21.       setSize(500,300);  //Tamanio del formulario
  22.       setTitle("OTM"); //TItulo del formulario
  23.       setResizable(false); //Para que no sea redimensionable
  24.       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Para salir al cerrar
  25.  
  26.       getContentPane().setLayout(null);
  27.      
  28.       lblname = new JLabel("Nombre");
  29.       lblname.setBounds(50,100,100,23);
  30.       getContentPane().add(lblname);
  31.      
  32.       name = new JTextField();
  33.       name.setBounds(155,100,200,23);
  34.       getContentPane().add(name);
  35.      
  36.       btn_ok = new JButton("Ingresar");
  37.       btn_ok.setBounds(370,230, 100, 23);
  38.       getContentPane().add(btn_ok);
  39.  
  40.     btn_ok.addActionListener(new ActionListener()
  41.       {
  42.           public void actionPerformed(ActionEvent e)
  43.           {
  44.             try{
  45.               resultInsertar = sql.executeQuery("INSERT INTO name(Nombre) VALUES('" + name.getText() + "')");  
  46.               }catch(SQLException a){System.out.println("Error" + a);}
  47.           }  
  48.       });  
  49.      
  50.  
  51.        
  52.   }
  53.  
  54.  
  55.   //Metodo principal
  56.  public static void main(String args[])
  57.   {
  58.     String nana;
  59.    
  60.     try{
  61.     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  62.     String dbase = "jdbc:odbc:base";
  63.     Connection Conexion =DriverManager.getConnection(dbase);
  64.     sql = Conexion.createStatement();
  65.     ResultSet nombre = sql.executeQuery("SELECT * FROM name");
  66.    
  67.    
  68.       while(nombre.next()){
  69.       nana = nombre.getString("Nombre");
  70.       System.out.println(nana);
  71.     }
  72.    
  73.     nombre.moveToInsertRow();
  74.     nombre.updateString("Nombre",)
  75.    
  76.    
  77.        
  78.     nombre.close();
  79.     Conexion.close();
  80.     sql.close();
  81.    
  82.   }catch(ClassNotFoundException e){
  83.     System.out.println("Clase no encontrada");
  84.     }
  85.    
  86.   catch(SQLException e){
  87.       System.out.println(e);
  88.     }
  89.    
  90.     System.out.println("Sin error al conectar ");
  91.    
  92.  
  93.    
  94.    
  95.       new Inscripcion().setVisible(true);
  96.      
  97.  
  98.   }
  99.  
  100. }
  101.