I have the following code, where its function is to create the database (which will be received from a controller) and it executes a file .sql
within the created database so that it imports tables and procedures:
public void Restaurar_BD_SQL(CredencialesMySql cMysql, String NomBd,String ActBack) throws SQLException {
try {
/* Crea la base de datos */
Connection conx;
conx = ConectarMySQL(cMysql);
Statement stmt = conx.createStatement();
stmt.execute("create database `" + NomBd + "`");
System.out.println("Se creó BD " + NomBd);
Process p = Runtime.getRuntime().exec("Q:/MySQL Server 8.0/bin/mysql -h " + cMysql.getNomServ() + " -u " + cMysql.getUsuario() + " -p" + cMysql.getClave() + " " + NomBd + "");
OutputStream os = p.getOutputStream();
FileInputStream fis = new FileInputStream(ActBack);
byte[] buffer = new byte[1000];
int leido = fis.read(buffer);
while (leido > 0) {
os.write(buffer, 0 , leido);
leido = fis.read(buffer);
}
os.flush();
fis.close();
os.close();
System.out.println("Fin del Proceso");
} catch (IOException ex) {
// Logger.getLogger(ejecutaSQL.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex);
}
}
And this is the controller that sends the parameter to the function that I just mentioned:
//llama a la conexión del servidor (cMysql), crea la base de datos ("xnomBD") y ejecuta el `.sql` (se especifica la ruta del SQL).
EjeSQL.Restaurar_BD_SQL(cMysql, xnomBD, "Q:\\sql\\Back_DEMO.sql");
//si logra conectarse para crear la DB, lanza 200 OK.
response.put("Mensaje", "Base de datos creada correctamente.");
What I need is that in that path that I define in the controller, it is a relative path and not absolute, that is, not calling the exact path of the file (from the root). I require this as my project will be uploaded to the cloud and it will never read the file path there.
I put the file .sql
in my project and tried to define my path like this in my local, but it doesn't read it:
inputstream is = new FileInputStream("/Back_DEMO.sql");
I would greatly appreciate your help.
by default, for the purpose of making inserts to DB Spring-Boot loads
data.sql
and/ordata-${platform}.sql
from the resources foldersrc/main/resources
You can change the name of the script to load by:
and the platform by
to load the schema definition also accepts
schema.sql
(which is actually read beforedata.sql
).You can find more about it in the spring-boot documentation