How to insert data and create tables in Java Derby JDCB

0

I'm trying to run a program for employee entry checks I use the Derby driver in eclipse oxygen. Reading I came across some information and it was very similar to php and prepared statements, my problem is that when running my code the database was created, but I can not write it:

 PreparedStatement tableCrt = connection.prepareStatement("CREATE 
 TABLE IF NOT EXISTS dbEmpleados(id SERIAL NOT NULL PRIMARY 
 KEY,nombre varchar(225) NOT NULL ,password varchar(225),fechareg 
 varchar(27))");
 tableCrt.executeUpdate();
 tableCrt.close();

I'm really lost in Java and I do not know how to continue, this is the script for my connection.

   import java.sql.Connection;
   import java.sql.SQLException;
   import java.sql.DriverManager;
   import java.sql.PreparedStatement;

public class tableCreation {
private static final String DRIVER = 
"org.apache.derby.jdbc.EmbeddedDriver"; 
private static final String JDBC_URL = "jdbc:derby:EvCo- 
RegHuella;create=true";
Connection conn;

public tableCreation() {

    try {
        //tomamos la conexion y le pasamos la url
        this.conn = DriverManager.getConnection(JDBC_URL);
        //si this.conn NO es nulo....
        if (this.conn != null) { 
            //SECCION EDITADA ------------------
            PreparedStatement ps = this.conn.prepareStatement("TABLE IF NOT EXISTS dbEmpleados ID int NOT NULL AUTO_INCREMENT, Nombre varchar(255) NOT NULL,Apellidos varchar(255) NOT NULL, Puesto varchar(255) NOT NULL,FechaReg varchar(255) NOT NULL, PRIMARY KEY (ID),");
            ps.executeUpdate();
            ps.close();





        }


        //este es el catch del try nos va a tirar una SQLException en caso que falle la conexion
    }catch(SQLException e) {


        System.out.print("Fallo la conexion :( comprueba sintaxis o bien si el url esta correcto");

    } 

}
}

In the main activity just call:

     tableCreation conectNcreate = new tableCreation() ;

As I said at the beginning, the database was created correctly (I see it even in my directory) what I do not see is that information is added to it, the table is not created nor can I add users or a password, in front of a lot of people thanks.

    
asked by Kevin Mora 14.05.2018 в 19:19
source

1 answer

1

Neither the "if not exist" nor the "auto increment" form part of the syntax to create tables via Derby.
I pass the league Derby syntax to create tables:

link

To use something similar to the "auto increment", I pass this league that is a sub part of the previous league that I added you.

link

Now that you mention that you can not add information, but you are not showing the code you use to add information.

The table you want to create can be created with this command:

java.sql.PreparedStatement ps =
                this.conn.prepareStatement("CREATE TABLE APP.dbEmpleados (ID int GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), Nombre varchar(255) NOT NULL,Apellidos varchar(255) NOT NULL, Puesto varchar(255) NOT NULL,FechaReg varchar(255) NOT NULL, PRIMARY KEY (ID))");
        ps.executeUpdate();

Note that the name of the table I put APP.dbEmployees, because I created it in the schema APP, you can change the scheme you use.

Greetings.

    
answered by 15.05.2018 / 00:57
source