I want to make a connection to a database where I can add data by means of a keyboard to an already created table, but it shows me an error and I do not know how to do it, I'm really learning and it takes pd help. if you can pass me the changes and the losses of these data would be much appreciated xd
package hi;
import java.io.*;
import java.sql.*;
public class Hi {
import java.sql.*;
import java.io.*;
public class Mostrar {
public static void main(String[] args) throws SQLException, IOException{
BufferedReader key= new BufferedReader (new InputStreamReader(System.in));
String user, password;
System.out.println("Usuario");
user=key.readLine();
System.out.println("contraseña");
password=key.readLine();
try {
Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/agenda", user, password);
Statement st = conexion.createStatement();
st.executeUpdate("DROP TABLE IF EXISTS personal;");
st.executeUpdate("CREATE TABLE personal ('Identificador' int(11) NOT NULL AUTO_INCREMENT, 'Nombre' varchar(50) NOT NULL, 'Apellidos' varchar(50) NOT NULL, 'Telefono' varchar(9) DEFAULT NULL, 'Email' varchar(60) DEFAULT NULL, PRIMARY KEY ('Identificador'));");
st.executeUpdate("INSERT INTO personal ('Identificador', 'Nombre', 'Apellidos', 'Telefono', 'Email') VALUES (1, 'José', 'Martínez López', '968112233', '[email protected]'), (2, 'María', 'Gómez Muñoz', '911876876', '[email protected]'), (3, 'Juan', 'Sánchez Fernández', '922111333', '[email protected]'), (4, 'Ana', 'Murcia Rodríguez', '950999888', '[email protected]');");
ResultSet rs = st.executeQuery("SELECT * FROM personal;");
if (rs != null) {
System.out.println("El listado de persona es el siguiente:");
while (rs.next()) {
System.out.println(" ID: " + rs.getObject("Identificador"));
System.out.println(" Nombre completo: " + rs.getObject("Nombre") + " " + rs.getObject("Apellidos"));
System.out.println(" Contacto: " + rs.getObject("Telefono") + " " + rs.getObject("Email"));
System.out.println("- ");
}
rs.close();
}
st.close();
}
catch(SQLException s)
{
System.out.println("Error: SQL.");
System.out.println("SQLException: " + s.getMessage());
}
catch(Exception s)
{
System.out.println("Error: Varios.");
System.out.println("SQLException: " + s.getMessage());
}
System.out.println("FIN DE EJECUCIÓN.");
}
}