Problem with preparedStatement

0

I'm trying to execute the following commands:

stmt=con.prepareStatement("CREATE DATABASE ?");
stmt.setString(1, nombreBase);
stmt.executeUpdate();

Doing about select or insert works but doing so with these commands does not allow me to do so throws me a syntax error. Why is this happening?

    
asked by Leo T 16.01.2017 в 18:30
source

2 answers

0

You can not send a name to the database using the binds, you must generate a sentence and send it to the prepareStatement , something like that

String query = "CREATE DATABASE "+nombreBase;

stmt=con.prepareStatement(query); stmt.executeUpdate();

Note: Remember that prepared statements must be an SQL data manipulation language instruction, such as INSERT, UPDATE, or DELETE; Or an SQL statement that returns nothing. PreparedStatement

    
answered by 16.01.2017 в 18:38
0

It's a little workaround, but you could try to create a PL:

PROCEDURE crearBD(p_nombre_tabla VARCHAR2(20)) IS
BEGIN
    //llamada dinámica a CREATE DATABASE p_nombre_tabla
END;
/

And from Java:

CallableStatement cstmt = con.prepareCall("{call crearBD(?)}")) {
    cstmt.setString(1,"nombreBD");
    cstmt.execute();
}
    
answered by 17.01.2017 в 11:57