They sent me a job to do and everything works correctly, what happens is that in the 'case3' I don't know why it doesn't update the data that I pass to it correctly and in the phpmyadmin it continues to appear blank, with all the other functions of the menu I don't know It happens and I don't get any type of error. Would someone know what is happening?
menu = scanner.nextInt();
switch(menu){
case 1:
stmt.execute("CREATE TABLE IF NOT EXISTS superheroe (id INT AUTO_INCREMENT, PRIMARY KEY(ID),"
+ " nombre VARCHAR(20),"
+ " descripcion VARCHAR(250),"
+ " fuego INT(5),"
+ " agua INT(5),"
+ " viento INT(5))");
break;
case 2:
System.out.println("Ponga el nombre del superheroe");
nombre = scanner.next();
stmt.executeUpdate("INSERT INTO SuperHeroe VALUES(null, '" + nombre + "' , 'Descripcion', 0, 0, 0)");
break;
case 3:
System.out.println("Introduza el nombre del superheroe");
nombre = scanner.next();
System.out.println("Modificar descripcion");
descripcion = scanner.nextLine();
scanner.nextLine();
stmt.executeUpdate("UPDATE SuperHeroe SET descripcion = '"+descripcion+"' WHERE nombre = '"+nombre+"'");
break;
case 4:
System.out.println("Introduza el nombre del superheroe");
nombre = scanner.next();
System.out.println("Introduzca del 0 al 5 el nivel de fuego");
nivelFuego = scanner.nextInt();
stmt.executeUpdate("UPDATE SuperHeroe SET fuego = "+nivelFuego+" WHERE nombre = '"+nombre+"'");
System.out.println("Introduzca del 0 al 5 el nivel de agua");
nivelAgua = scanner.nextInt();
stmt.executeUpdate("UPDATE SuperHeroe SET agua = "+nivelAgua+" WHERE nombre = '"+nombre+"'");
System.out.println("Introduzca del 0 al 5 el nivel de viento");
nivelViento = scanner.nextInt();
stmt.executeUpdate("UPDATE SuperHeroe SET viento = "+nivelViento+" WHERE nombre = '"+nombre+"'");
break;
case 5:
System.out.println("Introduza el nombre del superheroe");
nombre = scanner.next();
rs = stmt.executeQuery("SELECT * FROM SuperHeroe WHERE nombre = '"+nombre+"' ");
rs.next();
do{
System.out.println(rs.getString("id") + ": nombre " +
rs.getString("nombre") + "| descripcion " + rs.getString("descripcion") +
"| fuego: " + rs.getString("fuego") + "| agua: " + rs.getString("agua") +
"| viento: " + rs.getString("viento"));
System.out.println("");
}while(rs.next());
break;
so I have it before executing case 3
This is how I have it after using case 3 (I put "I'm batman" but it doesn't appear)
In the console I use case 1 to create a table, case 2 I insert batman but in case 3 I want to put a description, it doesn't do anything or it shows an error, it leaves it blank, do you still see something strange that I put? I need there to be spaces, it has to be a brief description, in my case I wanted to put I am batman. case 4 if it works fine, it updates the power level correctly
The reason is that the operation
UPDATE
will update the value of an existing record in the database. In this case you have no value in the table.For example, if you try to carry out this operation, you indicate that it updates the value of the "fire" field when the name is "Frany",
if there is no record with the name "Frany" in the table, no operation will be performed.
Regarding the fact that the field is not updated
descripcion
even though the record with the name exists but when writing a description it is not recorded, you must usescanner.next()
to obtain the value ofdescripcion
and in this way correctly create the query since usingscanner.nextLine()
an empty string will be obtained and your field will be updated with this value incorrectly, change to usescanner.next()
:The above for a word but if what you want is to get a phrase and store it in the description variable , define a delimiter that in this case would be a new line "\n" using
scanner.useDelimiter("\n");
this would be the code: