Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/03/2016, 11:45
Alvarocq
 
Fecha de Ingreso: abril-2014
Mensajes: 8
Antigüedad: 10 años
Puntos: 1
Pregunta Autonumerico en DB DERBY

Hola,

Verán eh estado creando una tabla con un ID autonumerico (algo muy comun), la cuestión es que lo hago sobre una sentencia SQL implementada en JAVA, en principio la tabla se crea correctamente... el problema surge al insertar los datos desde otro método, y pues lo gracioso es que almacena los datos correctamente, excepto el ID autonumerico por decir:

ID | Nombre | Apellidos
1 | Pedro | perez
101 | pablo | lopez
201 | .... | ....

Ojala alguien de Uds. tenga alguna idea de que es lo que puede estar pasando.

un saludo.
Gracias

PD: La herramienta IDE que uso es NetBeans, por ultimo no estoy creando una conexión típica de localhost.... etc, si no simplemente creo una base de datos para luego usar el proyecto como un APP Embedded.


Código PHP:
package BBDD;


import java.sql.*;
import javax.swing.JOptionPane;

public class 
Base_Datos {

    private 
Connection conexion;

    private final 
String Driver_Client;
    private final 
String Driver_Embedded;
    
    private final 
String URL;
    private final 
String nombre_bd;   

    private final 
String usuarioBD;
    private final 
String passwordBD;

    private 
ResultSet Result;
    private 
PreparedStatement Sentencia;

    public 
Base_Datos() {

        
this.Driver_Client "org.apache.derby.jdbc.ClientDriver";
        
this.Driver_Embedded "org.apache.derby.jdbc.EmbeddedDriver";
        
this.URL "jdbc:derby:.\\DataBase\\";
        
this.nombre_bd "MiBaseDatos";
        

        
this.usuarioBD "base";
        
this.passwordBD "datos";

        
this.conexion null;
        
this.Result null;
        
this.Sentencia null;

    }

    public 
boolean Conectar() {

        try {
            Class.
forName(Driver_Embedded);
            try {
                
this.conexion DriverManager.getConnection(this.URL this.nombre_bd ";user=" this.usuarioBD ";password=" this.passwordBD);
            } catch (
SQLException e) {
                
JOptionPane.showMessageDialog(null"Error al realizar la conexion BBDD " e,"Peligro",JOptionPane.ERROR_MESSAGE);
                return 
false;
            }
        } catch (
ClassNotFoundException ex) {
            
JOptionPane.showMessageDialog(null"Error al realizar la conexion DRIVER " ex,"Peligro",JOptionPane.ERROR_MESSAGE);
            return 
false;
        }

        return 
true;
    }

    public 
void CrearBD() {
    
        try {
            Class.
forName(Driver_Embedded);
            try {
                
this.conexion DriverManager.getConnection(this.URL this.nombre_bd ";create=true;user=" this.usuarioBD ";password=" this.passwordBD);
                    if (
this.conexion != null) {
                        
JOptionPane.showMessageDialog(null"Base de Datos Creada correctamente""Informacion"JOptionPane.INFORMATION_MESSAGE);
                    }
            } catch (
SQLException e) {
                
JOptionPane.showMessageDialog(null"Error al crear BBDD " e,"Peligro",JOptionPane.ERROR_MESSAGE);
            }
        } catch (
ClassNotFoundException ex) {
            
JOptionPane.showMessageDialog(null"Error al crear DRIVER " ex,"Peligro",JOptionPane.ERROR_MESSAGE);
        }   

    }

    public 
void desconectar() {

        try {
            
this.conexion.close();
        } catch (
SQLException ex) {
            
System.out.println(ex.getMessage());
        }
        
    }

    public 
void CrearTablas(){
    

      
String TBL_Profesores =
                           
"CREATE TABLE Profesores ("+
  
"ID_PROFESOR INT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"+
  
"Nombre VARCHAR(255) NOT NULL,"+
  
"Apellidos VARCHAR(255) NOT NULL,"+
  
"Despacho INT NOT NULL,"+
  
"Correo VARCHAR(255) NOT NULL,"+
  
"CONSTRAINT PK_ID_PROFESOR PRIMARY KEY (ID_PROFESOR) )";
                            
     
        try {
            
this.Sentencia this.conexion.prepareStatement(TBL_Profesores);
            
this.Sentencia.executeUpdate();
        }catch(
SQLException e){
            
JOptionPane.showMessageDialog(null"Error al crear Tabla SQL\n" e,"Peligro",JOptionPane.ERROR_MESSAGE);
        }
finally{
            if(
this.conexion != null){                
            try{
                
                    
this.Sentencia.close();
                    
this.conexion.close();
                    
            } catch(
Exception e){
                     
JOptionPane.showMessageDialog(null"Error al crear Tabla EXCEPTION\n" e,"Peligro",JOptionPane.ERROR_MESSAGE);
            }
            }
        }
    
    }
    
       public 
void Consulta(String Tabla){
        
int i;
        try {
            
this.Sentencia this.conexion.prepareStatement("SELECT * FROM "+Tabla);
            
this.Result this.Sentencia.executeQuery();
            
            for(
i=1;i<=this.Result.getMetaData().getColumnCount();i++){
                
System.out.print(this.Result.getMetaData().getColumnName(i)+"\t");                
            }
            
            while(
this.Result.next()){
                
System.out.println("");
                for (
i=1;i<=this.Result.getMetaData().getColumnCount();i++){                    
                    
System.out.print(this.Result.getString(i)+"\t");                
                }
                
            }
            
        }

        catch (
SQLException e){
            
JOptionPane.showMessageDialog(null,"Error Consulta "+e);
        }
        
finally{
            if(
this.conexion != null){                
                try{
                    
this.Result.close();
                    
this.Sentencia.close();
                    
                }
                catch(
Exception e){
                    
System.out.println(e);
                }
            }
        }
        
    }


       public 
void InsertarDatos(){
           try {        
        
this.Sentencia this.conexion.prepareStatement(" INSERT INTO Profesores (Nombre, Apellidos, Despacho, Correo) VALUES (?,?,?,?)");
          
            
this.Sentencia.setString(1,"Cristddhian");
               
            
this.Sentencia.setString(2,"Maccccgndo");
              
            
this.Sentencia.setInt(33221);
                  
            
this.Sentencia.setString(4,"[email protected]");
            
        
this.Sentencia.executeUpdate();
        }
        catch(
Exception  e){
            
JOptionPane.showMessageDialog(null,"Error 1 : \n "+e);      
        }
        
finally {
                try {
                    
this.Sentencia.close();       
                    
                } 
                catch (
SQLException e) {
                    
JOptionPane.showMessageDialog(null,"Error 2: \n "+e);      
                }
        }
       }

Código PHP:
/*
 * 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 p_fingrado;


import BBDD.*;


import java.sql.*;

/**
 *
 * @author kevin
 */
public class P_FinGrado {

    
/**
     * @param args the command line arguments
     */
    
public static void main(String[] argsthrows ClassNotFoundException {
     
        
        
Base_Datos bbdd = new Base_Datos();
      
// bbdd.CrearBD();
       
if(bbdd.Conectar()){
         
//bbdd.CrearTablas();
        
        
bbdd.InsertarDatos();
      
bbdd.Consulta("Profesores");
      
    
        } 
        
bbdd.desconectar();
      
     
       
       
    }