Connect Java to MySQl

0
  

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.");
     }

   }
    
asked by Enrique Acosta 21.11.2018 в 07:28
source

1 answer

0

Assuming you have added the jdbc driver to the library, you would need to add the import of this.

For this you need to create a driver variable like this:

String driverJDBC= "com.mysql.jdbc.Driver";

And then inport the driver before trying to connect, so that:

Class.forName(driverJDBC);

If we put these two steps into your code, it would look something like this:

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;
            String driverJDBC= "com.mysql.jdbc.Driver";
            System.out.println("Usuario");
            user=key.readLine();
            System.out.println("contraseña");
            password=key.readLine();

            try {
                Class.forName(driverJDBC);
                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.");
        }

        }

I hope this helps you.

    
answered by 21.11.2018 в 09:42